<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="https://shazwazza.com/rss/xslt"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Shazwazza</title>
    <link>https://shazwazza.com/</link>
    <description>My blog which is pretty much just all about coding</description>
    <generator>Articulate, blogging built on Umbraco</generator>
    <image>
      <url>/media/0libq25y/frog.png?rmode=max&amp;v=1da0e911f4e6890</url>
      <title>Shazwazza</title>
      <link>https://shazwazza.com/</link>
    </image>
    <item>
      <guid isPermaLink="false">1285</guid>
      <link>https://shazwazza.com/post/need-to-remove-an-auto-routed-controller-in-umbraco/</link>
      <category>Umbraco</category>
      <title>Need to remove an auto-routed controller in Umbraco?</title>
      <description>&lt;p&gt;Umbraco will auto-route some controllers automatically. These controllers are any MVC &lt;em&gt;SurfaceControllers&lt;/em&gt; or WebApi &lt;em&gt;UmbracoApiController &lt;/em&gt;types discovered during startup. There might be some cases where you just don’t want these controllers to be routed at all, maybe a package installs a controller that you’d rather not have routable or maybe you want to control if your own plugin controllers are auto-routed based on some configuration.&lt;/p&gt;&lt;p&gt;The good news is that this is quite easy by just removing these routes during startup. There’s various ways you could do this but I’ve shown below one of the ways to interrogate the routes that have been created to remove the ones you don’t want:&lt;/p&gt;&lt;h3&gt;Version 8&lt;/h3&gt;




&lt;pre class="lang-csharp"&gt;&lt;code&gt;
//This is required to ensure this composer runs after
//Umbraco's WebFinalComposer which is the component
//that creates all of the routes during startup    
[ComposeAfter(typeof(WebFinalComposer))]
public class MyComposer : ComponentComposer&amp;lt;MyComponent&amp;gt;{ }

//The component that runs after WebFinalComponent
//during startup to modify the routes
public class MyComponent : IComponent
{
    public void Initialize()
    {
        //list the routes you want removed, in this example
        //this will remove the built in Umbraco UmbRegisterController
        //and the TagsController from being routed.
        var removeRoutes = new[]
        {
            "/surface/umbregister",
            "/api/tags"
        };

        foreach (var route in RouteTable.Routes.OfType&lt;route&gt;().ToList())
        {
            if (removeRoutes.Any(r =&amp;gt; route.Url.InvariantContains(r)))
                RouteTable.Routes.Remove(route);
        }
    }

    public void Terminate() { }
}&lt;/route&gt;&lt;/code&gt;
&lt;/pre&gt;





&lt;h3&gt;Version 7&lt;/h3&gt;

&lt;pre class="lang-csharp"&gt;&lt;code&gt;public class MyStartupHandler : ApplicationEventHandler
{
    protected override void ApplicationStarted(
        UmbracoApplicationBase umbracoApplication,
        ApplicationContext applicationContext)
    {

        //list the routes you want removed, in this example
        //this will remove the built in Umbraco UmbRegisterController
        //and the TagsController from being routed.
        var removeRoutes = new[]
        {
            "/surface/umbregister",
            "/api/tags"
        };

        foreach(var route in RouteTable.Routes.OfType&amp;lt;Route&amp;gt;().ToList())
        {
            if (removeRoutes.Any(r =&amp;gt; route.Url.InvariantContains(r)))
                RouteTable.Routes.Remove(route);
        }
    }
}
&lt;/code&gt;
&lt;/pre&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:09:21 Z</pubDate>
      <a10:updated>2023-03-23T15:09:21Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1236</guid>
      <link>https://shazwazza.com/post/using-iexceptionlogger-with-per-controller-icontrollerconfiguration-in-webapi/</link>
      <category>ASP.Net</category>
      <title>Using IExceptionLogger with per-controller IControllerConfiguration in WebApi</title>
      <description>&lt;p&gt;There’s plenty of examples online about implementing a custom &lt;a href="https://docs.microsoft.com/en-us/aspnet/web-api/overview/error-handling/web-api-global-error-handling"&gt;IExceptionLogger globally in WebApi&lt;/a&gt; and I’m sure they all work as expected but what if you don’t want to register it globally? In that case you’d use an instance of &lt;em&gt;IControllerConfiguration&lt;/em&gt; to do a &lt;a href="https://blogs.msdn.microsoft.com/jmstall/2012/05/11/per-controller-configuration-in-webapi/"&gt;“per-controller”&lt;/a&gt; configuration and in theory you could just use this syntax:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-csharp"&gt;
public class MyExceptionLoggerConfigurationAttribute : Attribute, IControllerConfiguration
{
	public virtual void Initialize(
		HttpControllerSettings controllerSettings, 
		HttpControllerDescriptor controllerDescriptor)
	{
		controllerSettings.Services.Add(
			typeof(IExceptionLogger), 
			new UnhandledExceptionLogger());
	}	
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That seems pretty simple, and then you attribute whatever controller you want to use this new &lt;em&gt;IExceptionLogger&lt;/em&gt; … &lt;strong&gt;nope&lt;/strong&gt;!&lt;/p&gt;
&lt;p&gt;As it turns out, the custom &lt;em&gt;UnhandledExceptionLogger&lt;/em&gt; instance will never get executed unless there is also an &lt;em&gt;ExceptionFilter&lt;/em&gt; applied to the controller (even if it doesn’t do anything)! I can’t find documentation on this anywhere but it’s definitely the case. So to make the above work, we can just replace &lt;em&gt;Attribute&lt;/em&gt; with &lt;em&gt;ExceptionFilterAttribute&lt;/em&gt; like:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-csharp"&gt;
public class MyExceptionLoggerConfigurationAttribute : ExceptionFilterAttribute, IControllerConfiguration
{
	public virtual void Initialize(
		HttpControllerSettings controllerSettings, 
		HttpControllerDescriptor controllerDescriptor)
	{
		controllerSettings.Services.Add(
			typeof(IExceptionLogger), 
			new UnhandledExceptionLogger());
	}	
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And voila! this works.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:35 Z</pubDate>
      <a10:updated>2023-03-23T15:08:35Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1191</guid>
      <link>https://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-2/</link>
      <category>ASP.Net</category>
      <title>ASP.Net 5 Re-learning a few things (part 2)</title>
      <description>&lt;p&gt;This is part 2 of a series of posts about some fundamental changes in ASP.Net 5 that we’ll need to re-learn (or un-learn!)&lt;/p&gt; &lt;p&gt;Part 1: &lt;a title="http://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-1/" href="http://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-1/"&gt;http://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-1/&lt;/a&gt;&lt;/p&gt; &lt;h2&gt;System.Web&lt;/h2&gt; &lt;p&gt;This probably isn’t new news to most people since it’s really one of the fundamental shifts for ASP.Net 5 – There won’t be a System.Web DLL. Everything that you’ll install in your website will come as different, smaller, separate libraries. For example, if you want to serve static files, you’d reference the &lt;em&gt;Microsoft.AspNet.StaticFiles&lt;/em&gt; package, if you want to use MVC, you’d include the &lt;em&gt;Microsoft.AspNet.Mvc &lt;/em&gt;package&lt;em&gt;.&lt;/em&gt; &lt;/p&gt; &lt;p&gt;ASP.Net 5: “&lt;a href="http://www.asp.net/vnext/overview/aspnet-vnext/aspnet-5-overview" target="_blank"&gt;consists of modular components with minimal overhead, so you retain flexibility while constructing your solutions&lt;/a&gt;”&lt;/p&gt; &lt;h2&gt;Web Forms&lt;/h2&gt; &lt;p&gt;Gone! YAY! :-)&lt;/p&gt; &lt;p&gt;Web Forms will still be a part of .Net as part of the main framework in System.Web, just not part of ASP.Net 5. &lt;/p&gt; &lt;h2&gt;HttpModules &lt;/h2&gt; &lt;p&gt;An HttpModule simply doesn’t exist in Asp.Net 5, but of course there is a new/better way to achieve this functionality, it’s called Middleware. In an HttpModule, you had to execute code based on the various &lt;a href="http://msdn.microsoft.com/en-us/library/bb470252.aspx" target="_blank"&gt;stages&lt;/a&gt; of a request, things such as &lt;em&gt;AuthenticateRequest, AuthorizeRequest, PostResolveRequestCache&lt;/em&gt;, and other slightly confusingly named events. This is no longer the case with Middleware, things just make sense now … everything is simply a linear execution of your code. You can have multiple middleware’s defined to execute in your application and each one is registered explicitly in your Startup.cs file. As a developer, you are in full control of what get’s executed and in what order instead of not knowing which HttpModules are executing and not really in control of their order of execution. Middleware can simply modify a request and continue calling the next one in the chain, or it can just terminate the pipeline and return a result. &lt;/p&gt; &lt;p&gt;There’s loads of examples in the source for middleware, ranging from the &lt;a href="https://github.com/aspnet/StaticFiles/blob/dev/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs" target="_blank"&gt;static file middleware&lt;/a&gt; to &lt;a href="https://github.com/aspnet/Security/blob/master/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationMiddleware.cs" target="_blank"&gt;cookie authentication middleware&lt;/a&gt;,&amp;nbsp; etc…&lt;/p&gt; &lt;p&gt;And &lt;a href="http://whereslou.com/2014/05/28/asp-net-moving-parts-ibuilder/" target="_blank"&gt;here’s a good article&lt;/a&gt; that explains middeware registration and the flow of control.&lt;/p&gt; &lt;h2&gt;HttpHandlers&lt;/h2&gt; &lt;p&gt;HttpHandlers are also a thing of the past. All they really were was a request handler that was based on a specific request path. MVC (which now also includes WebApi) has got this covered. If you really wanted, you could create middleware for this type of functionality as well but unless you require something extraordinarily complex that MVC cannot do (and it can do a lot!), I’d recommend just sticking with MVC.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:15 Z</pubDate>
      <a10:updated>2023-03-23T15:08:15Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1197</guid>
      <link>https://shazwazza.com/post/isolated-webapi-attribute-routing/</link>
      <category>ASP.Net</category>
      <title>Isolated WebApi attribute routing</title>
      <description>&lt;p&gt;Attribute routing in ASP.Net WebApi is great and makes routing your controllers quite a bit more elegant than writing routes manually. However one problem I have with it is that it is either “on” or “off” at an application level.  There is no way for a library developer to tell ASP.Net to create routes based on attributes for specific controllers or assemblies without forcing the consumer of that library to enable Attribute Routing for the whole application. In many cases this might not matter, but if you are creating a package or library of that contains it’s own API routes, you probably don’t want to interfere with a developers’ normal application setup. There should be no reason why they need to be forced to turn on attribute routing in order for your product to work, and similarly they might not want your routes automatically enabled.&lt;/p&gt;
&lt;p&gt;The good news is that this is possible. With a bit of code, you can route your own controllers with attribute routing and be able to turn them on or off without affecting the default application attribute routes. A full implementation of this has been created for the &lt;a href="https://github.com/umbraco/UmbracoRestApi" target="_blank"&gt;Umbraco RestApi project&lt;/a&gt; so I’ll reference that source in this post for the following code examples.&lt;/p&gt;
&lt;h2&gt;Show me the code&lt;/h2&gt;
&lt;p&gt;They key to getting this to work is: &lt;em&gt;IDirectRouteProvider, IDirectRouteFactory&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The first thing we need is a custom &lt;em&gt;IDirectRouteFactory &lt;/em&gt;which is actually a custom attribute. I’ve called this &lt;em&gt;CustomRouteAttribute&lt;/em&gt;  but you could call it whatever you want.&lt;/p&gt;
&lt;pre class="csharpcode"&gt;[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CustomRouteAttribute : Attribute, IDirectRouteFactory&lt;/pre&gt;
&lt;p&gt;This custom attribute just wraps the default WebApi &lt;em&gt;RouteAttribute’s IDirectRouteFactory &lt;/em&gt;implementation so we don’t have to re-write any code for that.&lt;/p&gt;
&lt;p&gt;(&lt;a href="https://github.com/umbraco/UmbracoRestApi/blob/master/src/Umbraco.RestApi/Routing/CustomRouteAttribute.cs" target="_blank"&gt;see full implementation here&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;Next we’ll create a custom &lt;em&gt;IDirectRouteProvider:&lt;/em&gt;&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// This is used to lookup our CustomRouteAttribute instead of the normal RouteAttribute so that &lt;/span&gt;
&lt;span class="rem"&gt;/// we can use the CustomRouteAttribute instead of the RouteAttribute on our controlles so the normal&lt;/span&gt;
&lt;span class="rem"&gt;/// MapHttpAttributeRoutes method doesn't try to route our controllers - since the point of this is&lt;/span&gt;
&lt;span class="rem"&gt;/// to be able to map our controller routes with attribute routing explicitly without interfering&lt;/span&gt;
&lt;span class="rem"&gt;/// with default application routes.&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CustomRouteAttributeDirectRouteProvider : DefaultDirectRouteProvider
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; _inherit;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; CustomRouteAttributeDirectRouteProvider(&lt;span class="kwrd"&gt;bool&lt;/span&gt; inherit = &lt;span class="kwrd"&gt;false&lt;/span&gt;)
    {
        _inherit = inherit;
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; IReadOnlyList&amp;lt;IDirectRouteFactory&amp;gt; GetActionRouteFactories(HttpActionDescriptor actionDescriptor)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; actionDescriptor.GetCustomAttributes&amp;lt;CustomRouteAttribute&amp;gt;(inherit: _inherit);
    }
}&lt;/pre&gt;
&lt;p&gt;So far this is all pretty straight forward so far but here’s where things start to get interesting. Because we only want to create routes for specific controllers, we need to use a custom &lt;em&gt;IHttpControllerTypeResolver. &lt;/em&gt;However, since the &lt;em&gt;HttpConfiguration&lt;/em&gt; instance only contains a single reference to the &lt;em&gt;IHttpControllerTypeResolver &lt;/em&gt;we need to do some hacking. The route creation process for attribute routing happens during the &lt;em&gt;HttpConfiguration&lt;/em&gt; initialization so we need to create an isolated instance of &lt;em&gt;HttpConfiguration&lt;/em&gt;, set it up with the services we want to use, initialize it to create our custom routes and assign those custom routes back to the main application’s &lt;em&gt;HttpConfiguration&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Up first, we create a custom &lt;em&gt;IHttpControllerTypeResolver&lt;/em&gt; to only resolve the controller we’re looking for:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SpecificControllerTypeResolver : IHttpControllerTypeResolver
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; IEnumerable&amp;lt;Type&amp;gt; _controllerTypes;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; SpecificControllerTypeResolver(IEnumerable&amp;lt;Type&amp;gt; controllerTypes)
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (controllerTypes == &lt;span class="kwrd"&gt;null&lt;/span&gt;) &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="str"&gt;"controllerTypes"&lt;/span&gt;);
        _controllerTypes = controllerTypes;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; ICollection&amp;lt;Type&amp;gt; GetControllerTypes(IAssembliesResolver assembliesResolver)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; _controllerTypes.ToList();
    }
}&lt;/pre&gt;
&lt;p&gt;Before we look at initializing a separate instance of &lt;em&gt;HttpConfiguration&lt;/em&gt;, lets look at the code you’d use to enable all of this in your startup code:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;//config = the main application HttpConfiguration instance&lt;/span&gt;
config.MapControllerAttributeRoutes(
    routeNamePrefix: &lt;span class="str"&gt;"MyRoutes-"&lt;/span&gt;,
    &lt;span class="rem"&gt;//Map these explicit controllers in the order they appear&lt;/span&gt;
    controllerTypes: &lt;span class="kwrd"&gt;new&lt;/span&gt;[]
    {                    
        &lt;span class="kwrd"&gt;typeof&lt;/span&gt; (MyProductController),
        &lt;span class="kwrd"&gt;typeof&lt;/span&gt; (MyStoreController)
    });&lt;/pre&gt;
&lt;p&gt;The above code will enable custom attribute routing for the 2 specific controllers. These controllers will be routed with attribute routing but instead of using the standard &lt;em&gt;[Route]&lt;/em&gt; attribute, you’d use our custom &lt;em&gt;[CustomRoute]&lt;/em&gt; attribute. The &lt;em&gt;MapControllerAttributeRoutes &lt;/em&gt;extension method is where all of the magic happens, here’s what it does:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Iterates over each controller type&lt;/li&gt;
&lt;li&gt;Creates an instance of HttpConfiguration&lt;/li&gt;
&lt;li&gt;Sets it’s IHttpControllerTypeResolver instance to SpecificControllerTypeResolver for the current controller iteration (The reason an instance of HttpConfiguration is created for each controller is to ensure that the routes are created in the order of which they are specified in the above code snippet)&lt;/li&gt;
&lt;li&gt;Initialize the HttpConfiguration instance to create the custom attribute routes&lt;/li&gt;
&lt;li&gt;Copy these routes back to the main application’s HttpConfguration route table&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="https://github.com/umbraco/UmbracoRestApi/blob/master/src/Umbraco.RestApi/Routing/RouteExtensions.cs" target="_blank"&gt;You can see the full implementation of this extension method here&lt;/a&gt; which includes code comments and more details on what it’s doing.  The actual implementation of this method also allows for some additional parameters and callbacks so that each of these routes could be customized if required when they are created.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;There is obviously a bit of code involved to achieve this and there could very well be a simpler way, however this implementation does work rather well and offers quite a lot of flexibility. I’d certainly be interested to hear if other developers have figured this out and what their solutions were.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:15 Z</pubDate>
      <a10:updated>2023-03-23T15:08:15Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1166</guid>
      <link>https://shazwazza.com/post/multiple-webapi-controllers-with-the-same-name-but-different-namespaces/</link>
      <category>ASP.Net</category>
      <title>Multiple WebApi controllers with the same name but different namespaces</title>
      <description>&lt;p&gt;&lt;a href="https://twitter.com/warrenbuckley" target="_blank"&gt;Warren&lt;/a&gt; recently reported &lt;a href="http://issues.umbraco.org/issue/U4-5151" target="_blank"&gt;this issue&lt;/a&gt; on Umbraco which prohibits WebApi from routing to two different paths that specify the same controller name but different namespaces. This type of thing is fully supported in MVC but not in WebApi for some reason.&lt;/p&gt; &lt;p&gt;Here’s a quick example, suppose we have two controllers:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; Test1
{
    [PluginController(&lt;span class="str"&gt;"Test1"&lt;/span&gt;)]
    [IsBackOffice]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ConfigController : UmbracoApiController
    {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; GetStuff()
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; 9876;
        }
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; Test2
{
    [PluginController(&lt;span class="str"&gt;"Test2"&lt;/span&gt;)]
    [IsBackOffice]    
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ConfigController : UmbracoApiController
    {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; GetStuff()
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; 1234;
        }
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;These controller definitions will create routes to the following paths respectively:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;/umbraco/backoffice/test1/config/getstuff
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;/umbraco/backoffice/test2/config/getstuff &lt;/em&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;When these routes are created, the “Namespaces” data token is specified on the route, just like what is done in MVC, however in WebApi that needs to be done manually. Example:&lt;/p&gt;&lt;pre class="csharpcode"&gt;var r = routes.MapHttpRoute(
    name: &lt;span class="str"&gt;"DefaultApi"&lt;/span&gt;,
    routeTemplate: &lt;span class="str"&gt;"api/{controller}/{id}"&lt;/span&gt;,
    defaults: &lt;span class="kwrd"&gt;new&lt;/span&gt; { id = RouteParameter.Optional }
);
r.DataTokens[&lt;span class="str"&gt;"Namespaces"&lt;/span&gt;] = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] {&lt;span class="str"&gt;"Foo"&lt;/span&gt;};&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;but if you navigate to either of these paths you’ll end up with a message like:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Multiple types were found that match the controller named 'Config'. This can happen if the route that services this request ('umbraco/backoffice/Test2/Config/{action}/{id}') found multiple controllers defined with the same name but differing namespaces, which is not supported. The request for 'Config' has found the following matching controllers: Test1.ConfigController Test2.ConfigController&lt;/p&gt;&lt;/blockquote&gt;
&lt;h2&gt;Custom IHttpControllerSelector&lt;/h2&gt;
&lt;p&gt;To achieve what we want, we need to create a custom IHttpControllerSelector. I’ve created this in the Umbraco core to solve the issue and the source can be found &lt;a href="https://github.com/umbraco/Umbraco-CMS/blob/7.1.5/src/Umbraco.Web/WebApi/NamespaceHttpControllerSelector.cs" target="_blank"&gt;HERE&lt;/a&gt;. The implementation is pretty straight forward – it relies on the default WebApi controller selector for everything unless a “Namespaces” data token is detected in the route and more than one controller type was found for the current controller name in the app domain. &lt;/p&gt;
&lt;p&gt;There’s some posts out there that elude to the possibility of this being supported in WebApi in the future but as of the latest source code for the &lt;a href="http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Http/Dispatcher/DefaultHttpControllerSelector.cs" target="_blank"&gt;DefaultHttpControllerSelector&lt;/a&gt;, it appears that the functionality is not yet there. &lt;/p&gt;
&lt;p&gt;If you need this functionality though, this implementation is working and pretty simple. To register this selector just use this code on startup:&lt;/p&gt;&lt;pre class="csharpcode"&gt;GlobalConfiguration.Configuration.Services.Replace(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IHttpControllerSelector),
    &lt;span class="kwrd"&gt;new&lt;/span&gt; NamespaceHttpControllerSelector(GlobalConfiguration.Configuration));&lt;/pre&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:14 Z</pubDate>
      <a10:updated>2023-03-23T15:08:14Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1227</guid>
      <link>https://shazwazza.com/post/webapi-per-controller-configuration/</link>
      <category>ASP.Net</category>
      <title>WebApi per controller configuration</title>
      <description>&lt;p&gt;This is more of a blog post about what not to do :)&lt;/p&gt; &lt;p&gt;At first glance, it would seem relatively painless to change your WebApi controller’s configuration, I’d assume most people would do what I initially did. Say for example you wanted to have your controller only support JSON, here’s what I initially tried (DO NOT DO THIS):&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Initialize(HttpControllerContext controllerContext)
{
    &lt;span class="kwrd"&gt;base&lt;/span&gt;.Initialize(controllerContext);
    var toRemove = controllerContext.Configuration.Formatters
        .Where(t =&amp;gt; (t &lt;span class="kwrd"&gt;is&lt;/span&gt; JsonMediaTypeFormatter) == &lt;span class="kwrd"&gt;false&lt;/span&gt;).ToList();
    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var r &lt;span class="kwrd"&gt;in&lt;/span&gt; toRemove)
    {
        controllerContext.Configuration.Formatters.Remove(r);
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;Simple right, just override initialize in your controller and change the current controllerContext’s configuration…. WRONG :(&lt;/p&gt;
&lt;p&gt;What this is actually doing is modifying the &lt;strong&gt;global&lt;/strong&gt; WebApi configuration though it’s not clear that this is the case. Unfortunately the actual &lt;em&gt;Configuration&lt;/em&gt; property on the &lt;em&gt;controllerContext&lt;/em&gt; instance is assigned to the global one. I’m assuming the WebApi team has done this for a reason but I’m not sure what that is; as seen above it’s very easy to change the global WebApi configuration at runtime. Seems to me like it might have been a better idea to clone the global configuration instance and assign that to each HttpControllerContext object. &lt;/p&gt;
&lt;p&gt;The correct way to specify per controller custom configuration in WebApi is to use the &lt;em&gt;IControllerConfiguration&lt;/em&gt; interface. You can read all about &lt;a href="http://blogs.msdn.com/b/jmstall/archive/2012/05/11/per-controller-configuration-in-webapi.aspx" target="_blank"&gt;here&lt;/a&gt; and it is fairly simple but it does seem like you have to jump through a few hoops for something that initially seems very straight forward.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1292</guid>
      <link>https://shazwazza.com/post/uploading-files-and-json-data-in-the-same-request-with-angular-js/</link>
      <category>Web Development</category>
      <title>Uploading files and JSON data in the same request with Angular JS</title>
      <description>&lt;p&gt;I decided to write a quick blog post about this because much of the documentation and examples about this seems to be a bit scattered. What this achieves is the ability to upload any number of files with any other type of data in &lt;strong&gt;one&lt;/strong&gt; request. For this example we’ll send up JSON data along with some files.&lt;/p&gt; &lt;h2&gt;File upload directive&lt;/h2&gt; &lt;p&gt;First we’ll create a simple custom file upload angular directive&lt;/p&gt; &lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; height: 261px; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 420px; width: 97.99%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; height: 243px; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;app.directive(&lt;span style="color: #006080"&gt;'fileUpload'&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; () {&lt;br&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; {&lt;br&gt;        scope: &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;,        &lt;span style="color: #008000"&gt;//create a new scope&lt;/span&gt;&lt;br&gt;        link: &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (scope, el, attrs) {&lt;br&gt;            el.bind(&lt;span style="color: #006080"&gt;'change'&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;event&lt;/span&gt;) {&lt;br&gt;                &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; files = &lt;span style="color: #0000ff"&gt;event&lt;/span&gt;.target.files;&lt;br&gt;                &lt;span style="color: #008000"&gt;//iterate files since 'multiple' may be specified on the element&lt;/span&gt;&lt;br&gt;                &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;var&lt;/span&gt; i = 0;i&amp;lt;files.length;i++) {&lt;br&gt;                    &lt;span style="color: #008000"&gt;//emit event upward&lt;/span&gt;&lt;br&gt;                    scope.$emit(&lt;span style="color: #006080"&gt;"fileSelected"&lt;/span&gt;, { file: files[i] });&lt;br&gt;                }                                       &lt;br&gt;            });&lt;br&gt;        }&lt;br&gt;    };&lt;br&gt;});&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;The usage of this is simple:&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;input&lt;/span&gt; &lt;span style="color: #ff0000"&gt;type&lt;/span&gt;&lt;span style="color: #0000ff"&gt;="file"&lt;/span&gt; &lt;span style="color: #ff0000"&gt;file-upload&lt;/span&gt; &lt;span style="color: #ff0000"&gt;multiple&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;The ‘multiple’ parameter indicates that the user can select multiple files to upload which this example fully supports.&lt;/p&gt;
&lt;p&gt;In the directive we ensure a new scope is created and then listen for changes made to the file input element. When changes are detected with emit an event to all ancestor scopes (upward) with the file object as a parameter.&lt;/p&gt;
&lt;h2&gt;Mark-up &amp;amp; the controller&lt;/h2&gt;
&lt;p&gt;Next we’ll create a controller to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create a model to bind to 
&lt;li&gt;Create a collection of files 
&lt;li&gt;Consume this event so we can assign the files to&amp;nbsp; the collection 
&lt;li&gt;Create a method to post it all to the server&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;NOTE: I’ve put all this functionality in this controller for brevity, in most cases you’d have a separate factory to handle posting the data&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;With the controller in place, the mark-up might look like this (and will display the file names of all of the files selected):&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;div&lt;/span&gt; &lt;span style="color: #ff0000"&gt;ng-controller&lt;/span&gt;&lt;span style="color: #0000ff"&gt;="Ctrl"&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;input&lt;/span&gt; &lt;span style="color: #ff0000"&gt;type&lt;/span&gt;&lt;span style="color: #0000ff"&gt;="file"&lt;/span&gt; &lt;span style="color: #ff0000"&gt;file-upload&lt;/span&gt; &lt;span style="color: #ff0000"&gt;multiple&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ul&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;li&lt;/span&gt; &lt;span style="color: #ff0000"&gt;ng-repeat&lt;/span&gt;&lt;span style="color: #0000ff"&gt;="file in files"&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;{{file.name}}&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;li&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ul&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;div&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;The controller code below contains some important comments relating to how the data gets posted up to the server, namely the ‘Content-Type’ header as the value that needs to be set is a bit quirky. &lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 1000px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;function&lt;/span&gt; Ctrl($scope, $http) {&lt;br&gt;&lt;br&gt;    &lt;span style="color: #008000"&gt;//a simple model to bind to and send to the server&lt;/span&gt;&lt;br&gt;    $scope.model = {&lt;br&gt;        name: &lt;span style="color: #006080"&gt;""&lt;/span&gt;,&lt;br&gt;        comments: &lt;span style="color: #006080"&gt;""&lt;/span&gt;&lt;br&gt;    };&lt;br&gt;&lt;br&gt;    &lt;span style="color: #008000"&gt;//an array of files selected&lt;/span&gt;&lt;br&gt;    $scope.files = [];&lt;br&gt;&lt;br&gt;    &lt;span style="color: #008000"&gt;//listen for the file selected event&lt;/span&gt;&lt;br&gt;    $scope.$on(&lt;span style="color: #006080"&gt;"fileSelected"&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;event&lt;/span&gt;, args) {&lt;br&gt;        $scope.$apply(&lt;span style="color: #0000ff"&gt;function&lt;/span&gt; () {            &lt;br&gt;            &lt;span style="color: #008000"&gt;//add the file object to the scope's files collection&lt;/span&gt;&lt;br&gt;            $scope.files.push(args.file);&lt;br&gt;        });&lt;br&gt;    });&lt;br&gt;    &lt;br&gt;    &lt;span style="color: #008000"&gt;//the save method&lt;/span&gt;&lt;br&gt;    $scope.save = &lt;span style="color: #0000ff"&gt;function&lt;/span&gt;() {&lt;br&gt;        $http({&lt;br&gt;            method: &lt;span style="color: #006080"&gt;'POST'&lt;/span&gt;,&lt;br&gt;            url: &lt;span style="color: #006080"&gt;"/Api/PostStuff"&lt;/span&gt;,&lt;br&gt;            &lt;span style="color: #008000"&gt;//IMPORTANT!!! You might think this should be set to 'multipart/form-data' &lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// but this is not true because when we are sending up files the request &lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// needs to include a 'boundary' parameter which identifies the boundary &lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// name between parts in this multi-part request and setting the Content-type &lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// manually will not set this boundary parameter. For whatever reason, &lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// setting the Content-type to 'false' will force the request to automatically&lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// populate the headers properly including the boundary parameter.&lt;/span&gt;&lt;br&gt;            headers: { &lt;span style="color: #006080"&gt;'Content-Type'&lt;/span&gt;: &lt;span style="color: #0000ff"&gt;false&lt;/span&gt; },&lt;br&gt;            &lt;span style="color: #008000"&gt;//This method will allow us to change how the data is sent up to the server&lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// for which we'll need to encapsulate the model data in 'FormData'&lt;/span&gt;&lt;br&gt;            transformRequest: &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (data) {&lt;br&gt;                &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; formData = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FormData();&lt;br&gt;                &lt;span style="color: #008000"&gt;//need to convert our json object to a string version of json otherwise&lt;/span&gt;&lt;br&gt;                &lt;span style="color: #008000"&gt;// the browser will do a 'toString()' on the object which will result &lt;/span&gt;&lt;br&gt;                &lt;span style="color: #008000"&gt;// in the value '[Object object]' on the server.&lt;/span&gt;&lt;br&gt;                formData.append(&lt;span style="color: #006080"&gt;"model"&lt;/span&gt;, angular.toJson(data.model));&lt;br&gt;                &lt;span style="color: #008000"&gt;//now add all of the assigned files&lt;/span&gt;&lt;br&gt;                &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;var&lt;/span&gt; i = 0; i &amp;lt; data.files; i++) {&lt;br&gt;                    &lt;span style="color: #008000"&gt;//add each file to the form data and iteratively name them&lt;/span&gt;&lt;br&gt;                    formData.append(&lt;span style="color: #006080"&gt;"file"&lt;/span&gt; + i, data.files[i]);&lt;br&gt;                }&lt;br&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; formData;&lt;br&gt;            },&lt;br&gt;            &lt;span style="color: #008000"&gt;//Create an object that contains the model and files which will be transformed&lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// in the above transformRequest method&lt;/span&gt;&lt;br&gt;            data: { model: $scope.model, files: $scope.files }&lt;br&gt;        }).&lt;br&gt;        success(&lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (data, status, headers, config) {&lt;br&gt;            alert(&lt;span style="color: #006080"&gt;"success!"&lt;/span&gt;);&lt;br&gt;        }).&lt;br&gt;        error(&lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (data, status, headers, config) {&lt;br&gt;            alert(&lt;span style="color: #006080"&gt;"failed!"&lt;/span&gt;);&lt;br&gt;        });&lt;br&gt;    };&lt;br&gt;};&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;h2&gt;Handling the data server-side&lt;/h2&gt;
&lt;p&gt;This example shows how to handle the data on the server side using ASP.Net WebAPI, I’m sure it’s reasonably easy to do on other server-side platforms too.&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 500px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;p&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; async Task&amp;lt;HttpResponseMessage&amp;gt; PostStuff()&lt;br&gt;{&lt;br&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!Request.Content.IsMimeMultipartContent())&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; HttpResponseException(HttpStatusCode.UnsupportedMediaType);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    var root = HttpContext.Current.Server.MapPath(&lt;span style="color: #006080"&gt;"~/App_Data/Temp/FileUploads"&lt;/span&gt;);&lt;br&gt;    Directory.CreateDirectory(root);&lt;br&gt;    var provider = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MultipartFormDataStreamProvider(root);&lt;br&gt;    var result = await Request.Content.ReadAsMultipartAsync(provider);&lt;br&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (result.FormData[&lt;span style="color: #006080"&gt;"model"&lt;/span&gt;] == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; HttpResponseException(HttpStatusCode.BadRequest);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    var model = result.FormData[&lt;span style="color: #006080"&gt;"model"&lt;/span&gt;];&lt;br&gt;    &lt;span style="color: #008000"&gt;//TODO: Do something with the json model which is currently a string&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #008000"&gt;&lt;/span&gt;&lt;br&gt;&lt;br&gt;    &lt;span style="color: #008000"&gt;//get the files&lt;/span&gt;&lt;br&gt;    &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var file &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; result.FileData)&lt;br&gt;    {                &lt;br&gt;        &lt;span style="color: #008000"&gt;//TODO: Do something with each uploaded file&lt;/span&gt;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; Request.CreateResponse(HttpStatusCode.OK, &lt;span style="color: #006080"&gt;"success!"&lt;/span&gt;);&lt;br&gt;}&lt;/p&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
  </channel>
</rss>