find a location for property in a new city

Tuesday, 22 December 2015

Stop processing OPTIONS requests for CORS in ASP.NET Web API

I was attempting to allow some particular origins to access my ASP.NET Web API from a client side single page application. I was using the EnableCorsAttribute that comes with the Microsoft.AspNet.WebApi.Cors NuGet package.

I managed to set up CORS using the following code in my WebApiConfig:

var origins = ConfigurationManager.AppSettings["AllowedOrigins"];
var cors = new EnableCorsAttribute(origins, "accept,content-type,origin,customId", "GET,POST,PUT");
config.EnableCors(cors);

There is quite a lot to CORS but essentially, (some browsers) send a pre-flight request recognised with its HTTP method OPTIONS. This basically asks the application who is allowed to access this URL with the attempted headers and HTTP method. Your Web API will respond saying which origins are allowed or if there are any errors. The browser then decides if it is one of those allowed origins and sends the request if it is.

The problem I found is that on this initial OPTIONS request my IoC container, Unity, was constructing a whole dependency chain of classes. Some of which access the database and some check HTTP headers. This was throwing an error since bits were missing from the HTTP headers that would be with normal requests and unnecessarily hitting the database. So really, I wanted to stop these requests in their tracks whilst making sure they did their intended pre-flight work.

The best way I found to do this was to ignore routes based on an HTTP constraint for "OPTIONS". Basically shove this in your routing:

var constraints = new { httpMethod = new HttpMethodConstraint(HttpMethod.Options) };
config.Routes.IgnoreRoute("OPTIONS", "{*pathInfo}", constraints);

More info on Enabling CORS in Web API.

Follow britishdev on Twitter

12 comments:

  1. Good post but I was wondering if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Appreciate it!buy female viagra

    ReplyDelete
  2. That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.


    SAP training in Chennai

    ReplyDelete
  3. Beautiful post just there, and i've bookmarked this blog too & keep up the great job.
    Friv

    ReplyDelete
  4. When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile.
    Friv2Games, Kizi 6 Games, Yepi Games, Unblocked Craft, Friv, Y8Y8co, Y3 Game, Frozen Games, Y8Y8, Kizi, tetris battle, Friv, Y8Y8 co

    ReplyDelete

  5. Thanks for posting this useful content, Good to know about new things here, Let me share this, . Hadoop training in pune

    ReplyDelete
  6. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.

    happy wheels | monkey go happy|  unblocked games

    ReplyDelete
  7. wheels happy wheels happy Total Jerkface Total Jerkface Happy old man demo happy wheels game
    happywheelsgame.in||happy Total Jerkface happy Total Jerkface
    happywheelsdemo.in||happy wheels unblocked happy wheels demo total jerkface
    Play card games http://spidersolitaire.co.in || spider solitaire

    ReplyDelete
  8. Thank you for this valuable information. The information you shared is very interesting. Brave Technologies is an leading erp software development company in chennai.
    enterprise resource planning in chennai | web design company in chennai

    ReplyDelete
  9. I often get emotionally involved. Like when I read your post, I can cry, laugh, sad, funny, depending on your written words. I think you are a sensitive person.
    www.facebookbaixargratis.com.br

    ReplyDelete
  10. This article is really fantastic and thanks for sharing the valuable post.
    potaup.com

    ReplyDelete
  11. I like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot. I hope to have many more entries or so from you.
    Very interesting blog.
    ataribreakout.org

    ReplyDelete