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

41 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
    Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.

      Delete
  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
  12. Every good is always in my sight, and if I know how to choose fun or not is the new thing to say. Come with unblocked, abcya or titanfall or xbox one. have fun.
    .xbox one | ABCya 5 games

    ReplyDelete
  13. Awesome review, I'm certain you're getting an extraordinary reaction.
    Amazing and intriguing article. Extraordinary things you've generally imparted to us. Much appreciated. Simply keep making this kind out of post. total jerkface

    ReplyDelete
  14. Interesting post! This is really helpful for me. I like it! Thanks for sharing!

    suchmaschinenmarketing lüdenscheid

    ReplyDelete
  15. Well, it went the way I need you on the phone a long moment ago. I’m glad I could help. You can Also check my site happy wheels total jerkface

    ReplyDelete
  16. https://www.skygoapk.com/total-jerkface-happy-wheels-unblocked/

    ReplyDelete
  17. Well, it went the way I need you on the phone a long moment ago. I’m glad I could help. You can Also check my site unblocked games

    ReplyDelete
  18. i love this Blog
    check this same
    http://leoshipper.com/

    ReplyDelete
  19. Nice blog too informative. Looking and reading your points it’s so impressive. Doing more blog like this.Web Development Company in Chennai

    ReplyDelete
  20. A very important article! Sure your next post will be as great as this. And if you want to find best premium wordpress templates for any kind of business go here https://wordpresstemplates.ch/

    ReplyDelete
  21. Thank you for your contribution during the last time
    Friv4
    Friv 2 Games
    Yepi Games

    ReplyDelete
  22. Here you can dowload drivers and find useful information about them.
    https://hqdownloads.net

    ReplyDelete
  23. Want to be cool like me to spend my day off? Go on the link there you can have a good time and earn some extra money beneficial casino on net I wish good luck

    ReplyDelete
  24. Packers and Movers Bangalore as a Services providing company can make all the difference to your ###Home Relocation experience. Bangalore based Company which offers versatile solutions, Right team that easily reduce the stress associated with a ✔✔✔Household Shifting, ***Vehicle Transportation. we help things run smoothly and reduce breakages and offer you seamless, Affordable, Reliable Shifting Services, Compare Shifting Charges @
    Packers And Movers Bangalore

    ReplyDelete
  25. Packers and Movers Chennai Give Safe and Reliable ***Household Shifting Services in Chennai with Reasonable ###Packers and Movers Price Quotation. We Provide Household Shifting, Office Relocation, ✔✔✔Local and Domestic Transportation Services, Affordable and Reliable Shifting Service Charges @
    Packers And Movers Chennai

    ReplyDelete
  26. You are sharing informative post keep blogging, Thanks you. I have a common issue for my this website eyeglass lens replacement

    ReplyDelete
  27. Motifz Designer Lawn. 1, 2 & 3 Piece Unstitched Premium Embroidered Lawn 2019, Premium Lawn, Lawn 2019, Best lawn collection, Motifz, Best Lawn in Pakistan, Shipping worldwide. Stitching option available.

    ReplyDelete
  28. Buy Premium lawn Online for Women's at best prices available at Motifz. ✓ Latest Fashion, ✓ Trendy Designs ➤ Shop Now!

    ReplyDelete
  29. Check the Bihar Board 10th Result 2019 Details here. The Bihar School Education Board (BSEB) has announced the Bihar Board Class 10 Results 2018 on June 26 at 4.30 pm. The Board had earlier announced the Bihar Board Class 12 Result on June 6, 2018.

    MP Board 10th result 2019 will be available in the month of May. MPBSE conducts the 10th class examinations in the month of March.

    Are your looking for Karnataka SSLC result 2019? Students from the Karnataka Board appeared for the examinations are in seek of their upcoming result.

    The West Bengal Board of Secondary Education (WBBSE) will declare the West Bengal Board 10th Result 2019. As of now there is no official news on the release of the madhyamik Result 2019.

    ReplyDelete