Microsoft ASP.Net team did a Reddit IamA session

In case you guys haven’t seen it, 3 Microsoft guys did a Reddit session where they answered questions from anyone. The questions and responses are very interesting – they talk about technologies I’ve never heard about… If you want to check it out, here it is:

http://www.reddit.com/r/IAmA/comments/1qp91h/iama_we_are_microsoft_aspnet_and_web_tools_team/

Advertisements

CORS take 2–IE edition

I successfully implemented CORS in Firefox and Chrome, but could not get it to work in IE.  No surprise right?  My issues were:

  1. No transport error message:  When JQuery builds a ajax response object, it uses a factory to get the transport object.  So in some cases IE will use an ActiveX object to make the ajax call.  But if the proper transport to use can not be determined you’ll get the “No Transport” error message.  So the workaround is to tell jquery to use CORS:
    $.support.cors = true;
  2. Access Is Denied:  This was driving me crazy!  I wrote a spike that worked fine in IE, but the exact same code would fail to run on my web site.  There were 2 reasons:
    1. Using “localhost” in the ajax url setting – IE really doesn’t like this, so it just chucks an error.  I think there is a setting somewhere to enable this to work, but I just replaced “localhost” with my IP address to work around the problem.
    2. Document Mode : I was forcing my web page to use the IE8 standards document mode.  That combination of IE10 browser mode and IE8 standards just made IE ralph all over the place.  Removing the “meta” tag from the page header so it would default to plain ol “Standards” fixed it right up.
  3. IE8 and IE9: Once I had everything working in IE10, I moved on to the other two versions.  They laughed at my attempts.  So I downloaded a JS jquery addon from MoonScript:
    https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest
    With this script included on the page, I made the ajax calls the same way but the big difference is it worked…

CORS support for web API in VisualStudio 2012

So I have 2 web services – one is a web-api service and the other is some Frankenstein.  When a CORS request comes over to the server it comes with the “OPTIONS” HTTP method, so I’m guessing there is an “options” attribute you can add to customize the request for a specific method…  Not sure why you would do that though.

Here are the notes I’ve collected from the process of adding support to both services:

ScottGu: http://weblogs.asp.net/scottgu/archive/2013/04/19/asp-net-web-api-cors-support-and-attribute-based-routing-improvements.aspx

http://aspnetwebstack.codeplex.com/wikipage?title=CORS%20support%20for%20ASP.NET%20Web%20API&referringTitle=Specs

Web API steps
My big hangup is doing this in visual studio 2012 – it doesn’t appear to be made to work out of the box in vs2012…
– Include System.Web.Cors.dll and System.Web.Http.Cors.dll – had to download the nuget package Microsoft.AspNet.WebApi.Cors.
– Enable CORS in the WebApiConfig.Register – you can then use the [EnableCors] attribute.  However the example in the codeplex example appears to be bad —  You can’t instantiate a EnableCorsAttribute object without parameters…
– To load settings from your config, you have to implement an ICorsPolicyProvider class
– Examples in the second link show how to implement it as a custom attribute
– Had to add a dependent section to the web.config:

      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>

–  Had to follow the nuget update steps in: http://stackoverflow.com/questions/18347290/webapi-odata-5-0-beta-accessing-globalconfiguration-throws-security-error

– Added one more dependent section to the web.config

      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>