<?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">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">1179</guid>
      <link>https://shazwazza.com/post/consume-json-rest-service-with-wcf-and-dynamic-object-response/</link>
      <category>Web Development</category>
      <title>Consume Json REST service with WCF and dynamic object response</title>
      <description>&lt;div class="imported-post"&gt;This post was imported from FARMCode.org which has been discontinued. These posts now exist here as an archive. They may contain broken links and images.&lt;/div&gt;This was my first shot at WCF and though I saw how nice it could be, i failed miserably… well, I’m actually not too sure. I tried all sorts of things but no matter what couldn’t get WCF to behave and return the actual response you would get if you just consumed the service with the WebClient object. Here’s what I tried and assumed that this ‘should’ work but the result was always an empty dictionary:  &lt;p&gt;Note: this demo is based off of the @Task project management tool’s REST API.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Define the contract:&lt;/strong&gt;&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:5ae3512e-d26b-4035-8b78-8ebdffa69f1e" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 221px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000"&gt;[ServiceContract]
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;interface&lt;/span&gt;&lt;span style="color: #000000"&gt; IAtTaskService
{        
    [OperationContract]
    [WebGet(
        UriTemplate &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;/api/login?username={username}&amp;amp;password={password}&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, 
        RequestFormat &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; WebMessageFormat.Json)]
    IDictionary&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; Login(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; username, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; password);
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Run the code:&lt;/strong&gt;&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:dab1d397-0418-4354-b055-8b1518fbc77a" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 239px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; IDictionary&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; DoThis(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; username, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; pword)
{
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; (var cf &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; WebChannelFactory&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;IAtTaskService&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;(
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; Uri(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;https://mycompaniesurl.attask-ondemand.com/attask/&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;)))
    {
        var s &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; cf.CreateChannel();
        var token &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; s.Login(username, pword);
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; token;
    } 
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;The above seemed really nice to be able to consume services but the result was always empty. I tried using strongly typed objects in the result, made sure it wasn’t an HTTPS issue, used the normal WCF configuration approach, etc… but couldn’t get it to work. &lt;/p&gt;
&lt;h2&gt;The ‘Fix’&lt;/h2&gt;
&lt;p&gt;I quite like the way that WCF has UriTemplates and you can just attribute up an interface and then make strongly typed calls to access your services so i decided I’d just make that work with my own implementation. Again, perhaps I was ‘doing it wrong’ or whatever, but the below implementation is pretty cool and also lets you get JSON results as a dynamic object :)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Define the contract:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is pretty much the same as above, but we have a dynamic response&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:7ee1f6fd-2f30-4361-8f67-2226cad8e101" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 220px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000"&gt;[ServiceContract]
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;interface&lt;/span&gt;&lt;span style="color: #000000"&gt; IAtTaskService
{        
    [OperationContract]
    [WebGet(
        UriTemplate &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;/api/login?username={username}&amp;amp;password={password}&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, 
        RequestFormat &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; WebMessageFormat.Json)]
    dynamic Login(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; username, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; password);
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;h3&gt;The JsonRestService base class&lt;/h3&gt;
&lt;p&gt;I created a class that lets you specify you contract service as a generic argument and then provides you with a method called “Send” which is defined a:&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:e99f78fe-c8e3-4144-87ef-8acaad733291" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 24px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt;&lt;span style="color: #000000"&gt; dynamic Send(Func&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;T, dynamic&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; func)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;This class will manage all of the http requests and use the WCF attributes applied to your contract to generate the correct URLs and parameters (&lt;em&gt;&lt;strong&gt;source code at the bottom of this article&lt;/strong&gt;&lt;/em&gt;)&lt;/p&gt;
&lt;h3&gt;Define a standard service&lt;/h3&gt;
&lt;p&gt;So to consume the REST services, we just create a ‘real’ service class such as:&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:d69a4872-393c-4903-bdbf-ab4afd336724" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 284px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; AtTaskService : JsonRestService&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;IAtTaskService&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;, IAtTaskService
{
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; AtTaskService(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; serviceUrl)
        : &lt;/span&gt;&lt;span style="color: #0000ff"&gt;base&lt;/span&gt;&lt;span style="color: #000000"&gt;(serviceUrl)
    {
    }

    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; dynamic Login(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; username, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; password)
    {
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; Send(x &lt;/span&gt;&lt;span style="color: #000000"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; x.Login(username, password));
    }
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;As you can see, the “Login” method just calls the underlying “Send” method which uses a strongly typed delegate. The Send method then inspects the delegate and generates the correct URL with parameters based on the WCF attributes.&lt;/p&gt;
&lt;h3&gt;Using the service&lt;/h3&gt;
&lt;p&gt;So now, to use the new service we can do something like:&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:83d1461a-30a6-4d1b-86fb-a3a618e18f63" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 343px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000"&gt;[TestMethod]
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; AtAtask_Login()
{

    &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;Arrange&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    var svc &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; AtTaskService(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;https://mycompanyname.attask-ondemand.com/attask/&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;);
            
    &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;Act&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    var token &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; svc.Login(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;MyUserName&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;mypassword&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;);
            
    &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;Assert&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    Assert.IsTrue(token &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;);
    Assert.IsTrue(token.data &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;);
    Assert.IsTrue(token.data.userID &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;);        
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;Too easy!&lt;/p&gt;
&lt;p&gt;As I mentioned earlier, I would have assumed that WCF should handle this out of the box, but for the life of my I couldn’t get it to return any results. Whether it was a request issue or a parsing issue, or whatever i have no idea. In the meantime, here’s the source code for the JsonRestService class which will allow you to do the above. The source requires references to &lt;a href="http://json.codeplex.com/" target="_blank"&gt;Json.Net&lt;/a&gt; and &lt;a href="http://www.aaron-powell.com/dynamics-library" target="_blank"&gt;Aaron-powell.dynamics&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;JsonRestService source&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer: I made this today in a couple of hours, I’m sure there’s some code in here that could be refactored to be nicer&lt;/strong&gt;&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:88d9ad21-9a6f-4500-9815-53861fc44fb0" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 789px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; JsonRestService&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;T&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;
{
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; ServiceUrl { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt;; }

    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; JsonRestService(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; serviceUrl)
    {
        ServiceUrl &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; serviceUrl;
    }

    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; Queries the WCF attributes of the method being called, builds the REST URL and sends the http request
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; based on the WCF attribute parameters. When the JSON response is returned, it is changed to a dynamic object.
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="func"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #808080"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt;&lt;span style="color: #000000"&gt; dynamic Send(Func&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;T, dynamic&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; func)
    {
        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;find the method on the main type that is being called... i'm sure there's a better way to do this,
        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;but this does work.
        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;This will not work if there are methods with overloads.&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        var methodNameMatch &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; Regex.Match(func.Method.Name, &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;&amp;lt;(.*?)&amp;gt;&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;);
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #000000"&gt;!&lt;/span&gt;&lt;span style="color: #000000"&gt;methodNameMatch.Success &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000"&gt; methodNameMatch.Groups.Count &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800080"&gt;2&lt;/span&gt;&lt;span style="color: #000000"&gt;)
        {
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;throw&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; MissingMethodException(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;Could not find method &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; func.Method.Name);
        }
        var realMethodName &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; methodNameMatch.Groups[&lt;/span&gt;&lt;span style="color: #800080"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;].Value;
        var m &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000"&gt;(T).GetMethods().Where(x &lt;/span&gt;&lt;span style="color: #000000"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; x.Name &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; realMethodName).SingleOrDefault();
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (m &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;)
        {
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;throw&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; MissingMethodException(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;Could not find method&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; realMethodName &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt; on type &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt;
                                                &lt;/span&gt;&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000"&gt;(T).FullName);
        }

        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;now that we have the method, find the wcf attributes&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        var a &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; m.GetCustomAttributes(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;false&lt;/span&gt;&lt;span style="color: #000000"&gt;);
        var webGet &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; a.OfType&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;WebGetAttribute&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;().SingleOrDefault();
        var webInvoke &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; a.OfType&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;WebInvokeAttribute&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;().SingleOrDefault();
        var httpMethod &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; webGet &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;?&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;GET&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt; : webInvoke &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;?&lt;/span&gt;&lt;span style="color: #000000"&gt; webInvoke.Method : &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;.Empty;
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;.IsNullOrEmpty(httpMethod))
        {
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;throw&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; ArgumentNullException(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;The WebGet or WebInvoke attribute is missing from the method &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt;
                                            realMethodName);
        }

        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;now that we have the WCF attributes, build the REST url based on the url template and the
        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;method being called with it's parameters&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        var urlTemplate &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; webGet &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;?&lt;/span&gt;&lt;span style="color: #000000"&gt; webGet.UriTemplate : webInvoke.UriTemplate;
        var urlWithParams &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; GetUrlWithParams(urlTemplate, func);
        var url &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; ServiceUrl &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; urlWithParams;

        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;Do the web requests&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; output;
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (httpMethod &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;GET&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;)
        {
            output &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; HttpGet(url);
               
        }
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;span style="color: #000000"&gt;
        {
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;need to move the query string params to the http parameters&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;            var parts &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; url.Split(&lt;/span&gt;&lt;span style="color: #800000"&gt;'&lt;/span&gt;&lt;span style="color: #800000"&gt;?&lt;/span&gt;&lt;span style="color: #800000"&gt;'&lt;/span&gt;&lt;span style="color: #000000"&gt;);
            output &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; HttpInvoke(parts[&lt;/span&gt;&lt;span style="color: #800080"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;], httpMethod, parts.Length &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800080"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;?&lt;/span&gt;&lt;span style="color: #000000"&gt; parts[&lt;/span&gt;&lt;span style="color: #800080"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;] : &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;.Empty);
        }

        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;change the response to json and then dynamic&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        var stringReader &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; StringReader(output);
        var jReader &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; JsonTextReader(stringReader);
        var jsonSerializer &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; JsonSerializer();
        var json &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; jsonSerializer.Deserialize&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;Dictionary&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;(jReader);
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; json.AsDynamic();
    }

    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; Updates the url template with the correct parameters
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="template"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="expression"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #808080"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; GetUrlWithParams(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; template, Func&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;T, dynamic&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; expression)
    {
        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;parse the template, get the matches&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000"&gt; (var m &lt;/span&gt;&lt;span style="color: #0000ff"&gt;in&lt;/span&gt;&lt;span style="color: #000000"&gt; Regex.Matches(template, &lt;/span&gt;&lt;span style="color: #800000"&gt;@"&lt;/span&gt;&lt;span style="color: #800000"&gt;\{(.*?)\}&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;).Cast&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;Match&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;())
        {
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (m.Groups.Count &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800080"&gt;2&lt;/span&gt;&lt;span style="color: #000000"&gt;)
            {
                var m1 &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; m;
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;find the fields based on the expression(Func&amp;lt;T&amp;gt;), get their values and replace the tokens in the url template&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                var field &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; expression.Target.GetType().GetFields().Where(x &lt;/span&gt;&lt;span style="color: #000000"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; x.Name &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; m1.Groups[&lt;/span&gt;&lt;span style="color: #800080"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;].Value).Single();
                template &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; template.Replace(m.Groups[&lt;/span&gt;&lt;span style="color: #800080"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;].Value, field.GetValue(expression.Target).ToString());
            }
        }
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; template;
    }

    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; Do an Http GET
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="uri"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #808080"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; HttpGet(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; uri)
    {
        var webClient &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; WebClient();
        var data &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; webClient.DownloadString(uri);
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; data;
    }

    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; Do an Http POST/PUT/etc...
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="uri"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="method"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="parameters"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #808080"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; HttpInvoke(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; uri, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; method, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; parameters)
    {
        var webClient &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; WebClient();
        var data &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; webClient.UploadString(uri, method, parameters);
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; data;
    }
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:09 Z</pubDate>
      <a10:updated>2023-03-23T15:08:09Z</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>