find a location for property in a new city

Friday, 4 June 2010

Don't use runAllManagedModulesForAllRequests="true" when getting your MVC routing to work

It seems to be common advice to make your modules section of your web.config say <modules runAllManagedModulesForAllRequests="true">. In fact this is quite a drastic thing to do to solve the routing problem and has global effects that could CAUSE ERRORS.

You need a module that goes by the name of UrlRoutingModule-4.0 to be running through IIS. Now, since your MVC URLs are likely to end without .aspx these will not be picked up by IIS and run through the intergrated pipeline and therefore you will end up with 404 not found errors. I struggled with this when I was getting started until I found the <modules runAllManagedModulesForAllRequests="true"> workaround.

This highly recommended fix can cause other problems. These problems come in the form of making all your registered HTTP modules run on every request, not just managed requests (e.g. .aspx). This means modules will run on ever .jpg .gif .css .html .pdf etc.

This is:
  1. a waste of resources if this wasn't the intended use of your other modules
  2. a potential for errors from new unexpected behaviour.

Better solution

Fine, so the ranting about <modules runAllManagedModulesForAllRequests="true"> is over. What is a better solution?

In the modules section of your web.config, you can add the UrlRoutingModule-4.0 module in with a blank precondition meaning it will run on all requests. You will probably need to remove it first since it is most likely already registered at machine level. So make your web.config look like this:
<modules>
  <remove name="UrlRoutingModule-4.0" />
  <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
  <!-- any other modules you want to run in MVC e.g. FormsAuthentication, Roles etc. -->
</modules>

Note: the modules element does NOT contain the runAllManagedModulesForAllRequests="true" attribute because it is evil!

Follow britishdev on Twitter

110 comments:

  1. Thank you! I've been fighting this issue for several days, and now have it working correctly.

    ReplyDelete
  2. even better solution

    http://support.microsoft.com/kb/980368

    ReplyDelete
    Replies
    1. that worked for me! need to sing that link out from the rooftops, have been pulling my hair out for hours

      Delete
    2. Same here. No need to specify modules manually if you install the hotfix.

      Delete
    3. awesome ..i was struggling with this from few days...

      Delete
    4. awesome ..i was struggling with this from few days...

      Delete
  3. Hi!

    Removing the runAllManagedModulesForAllRequests="true" flag or setting it to false has one pitfall: Some of the global.asax event handlers stop working for MVC requests.

    With your configuration in a new MVC application project Application_Start and Application_End events are no problem, but Application_BeginRequest, Application_EndRequest, Session_Start etc. are firing for an added ~/Test.aspx, but not for ~/Home/Index which is an MVC controller and action. As soon as I add the runAllManagedModulesForAllRequests="true" flag all event handlers are firing for MVC actions too.

    ReplyDelete
  4. Thanks for your comment. Although, please note line 4 in my code: <!-- any other modules you want to run in MVC e.g. FormsAuthentication, Roles etc. -->

    It is true that some modules will not be fired on MVC pages without the runAllManagedModulesForAllRequests="true" but this is why those modules should be added into added into the modules section (with preCondition="").

    Setting runAllManagedModulesForAllRequests="true" will run ALL modules under the sun in EVERY request under the sun (e.g. css files). Using runAllManagedModulesForAllRequests="true" is a kind of brute false 'just make it work' code thats stinks and I believe should be avoided. In my case it was also causing errors.

    I agree with what you are saying I just believe that not using that property is best if you can

    ReplyDelete
  5. I don't understand the comment in line 4.

    Are you saying that any module you want to run in MVC need to be after the UrlRoutingModule line in web.config, or are you saying that they need to also set preCondition="" and deal with getting called on every request?

    I THINK that you're saying that they need preCondition=""?

    ReplyDelete
  6. Correct. I wasn't suggesting an order, I was saying the modules needed in MVC will need preCondition=""

    ReplyDelete
  7. Some Application events does not get invoked in Global.asax after I set runAllManagedModulesForAllRequests to false. For example this one:
    Application_AuthorizeRequest.

    I've added these modules to web.config:

    ReplyDelete
    Replies
    1. Can you give an example of how to add these modules to web.config for events in global.asax that don't fire anymore?

      Delete
  8. Thanks for the useful info!

    ReplyDelete
  9. Hi,

    Could you explain why on 20 windows boxes that are identical to each other in every way possible, our MVC application gives 404 errors on 3 of them unless we add "runAllManagedModules=true" to the web.config?

    We've spent an entire day trying to figure this out.

    ReplyDelete
  10. Have you checked your root config files. I.e. machine.config and web.config (the root web.config - the one that is in the same place as the machine.config not the one in the web application)

    ReplyDelete
  11. Hi, I am recently just trying what you suggest and I have added the module to the web.config and machine.config files of my framework folder.

    I have also added them too my web.config in my solution.

    I still get the same messages, I have restarted my pc several times to ensure it was not an issue with that.

    Thanks!

    ReplyDelete
  12. Thank you so much - this solved my problem - I spent about 6 hours trying to fix this! Yay!

    ReplyDelete
  13. was very helpful thanks

    ReplyDelete
  14. Or you could try this Microsoft Hotfix - http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=5272

    extensionless URLs worked fine on our dev IIS 7.5 server without needing anything at all in the modules section, but didn't work on our live server. After installing the above hotfix, they 'just worked' on the live server as well.

    ReplyDelete
  15. hi experts,
    I am facing a very strange problem. If I use,





    My all session variables & Forms Authentications User Identity stopped working. I am getting "Object reference not set to an instance of an object." error if I use session variables & authentication Identity.
    But If I use "", my problem solved.

    Any idea ??

    ReplyDelete
    Replies
    1. Hi,, Anuj.. I m facing the error.. did u manage to find the solution?

      Thanks in Advance.


      Regards,
      Vani

      Delete
  16. One thing to note - this method wasn't firing the EndRequest event on my project (.Net 4). This caused us a lot of bother due to some 'tidy up' stuff that was happening once the request had gone.

    Ended up putting a handler mapping in for XML instead (in system.webserver > handlers):





    Cheers,

    Paul

    ReplyDelete
  17. Thank You, I've been pulling my hairs out for ours too :)

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. Adding the specific module needed worked for us. We had the "evil" runAllManagedModulesForAllRequests="true" attribute, but didn't want to use it because of the performance concerns (and should you just run all really?). Simply adding the correct module, and removing that evil attribute solved the problem.




    ReplyDelete
  20. Hi!

    By removing the runAllManagedModulesForAllRequests from the module didn't worked for me.I'm still facing the issue of 404, Handler: StaticFile, Error Code: 0x80070002

    Please suggests
    Thanks

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. Hi!
    I just finished my very first MVC 5 app and published it in a IIS 7 running under a Windows 2008 Server.

    I'm getting that 404 error. I've already included those lines in my web.config but it still not working. Any ideas?

    Thanks.

    ReplyDelete
  23. This blog is so useful. Nice to read and share.
    IELTS classes in Kuwait

    ReplyDelete
  24. Hi, i solved the problem with this article, but my active directory doesnt work yet, anyone can help me?

    ReplyDelete
  25. Wonderful directory ideas that can help to boost our own web site creating, after i creating web site I will recall these points as well as help make some really good creating.Voip Telephone Systems

    ReplyDelete
  26. I think you have problems with dll file msvcr120.dll. Do not panic! I have solution - download dll file here http://fix4dll.com/msvcr120_dll and install it. Then restart your computer.

    ReplyDelete
  27. Thank you - I've been pulling my hair out for hours! This is exactly what I needed.

    ReplyDelete

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

    ReplyDelete
  29. Worked brilliantly, thank you so much for sharing!

    ReplyDelete
  30. Thank you for this valuable information. The information you shared is very interesting. Get your business to the next level in simple steps. We provides lowest price of erp software Software for our clients.
    erp software in chennai | erp providers in chennai | online events registration

    ReplyDelete


  31. The blog or and best that is extremely useful to keep I can share the ideas
    of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
    tanki online | 2048 game|

    ReplyDelete
  32. I have webconfig file added to refresh ie browser style sheet not call. please help me. thanks in advance

    ReplyDelete

  33. Hi i am from dodo packaging.Wonderful Blog! I should need to thank for the undertakings you have made in creating this post. I am confiding in a comparable best work from do my coursework uk later on as well. I expected to thank you for this destinations! Thankful for sharing.
    custom product packaging

    ReplyDelete
  34. Plus Printers is all set to give you the best custom soap box in the market of the United States of America! You will get everything under one roof!

    ReplyDelete
  35. hi,my problem is solve using this but my save functional not worked

    ReplyDelete
  36. From Lipstick Boxes.My idea of taking part in these competitions is not exactly being the winner. There is an experience factor you acquire from these competitions that will be so
    valuable in writing books. That is the main concern of mine.
    Hairspray boxes

    ReplyDelete

  37. From dodo packaging
    I Like the effort you have put in this, appreciate it for all the great articles. Thanks for sharing
    kraft boxes

    ReplyDelete
  38. The Custom Pillow Boxes are the favorite among the companies to use it for their premium and special gifting products. All over the world, it has an importance to the United States of America also emphasizes on these boxes!

    ReplyDelete
  39. It includes applications, reminders, notepads and an MP3 or MP4. So, it is more of a choice than a trend. Smart Watches in Pakistan are not that common and people are still unaware of many features to look up before purchasing a smart watch.

    ReplyDelete
  40. Thank you so much for such an wonderful blog. I’m really thankful to you for this informative article.
    audio engineering course

    ReplyDelete
  41. Superb blog comment, thanks for share this valuable information.
    Jeans Supplier in Delhi

    ReplyDelete
  42. I got valuable information by your blog, thank you so much for sharing with us.
    Flex Board Printing in Delhi

    ReplyDelete
  43. Great with all of the valuable information you have Keep up the good work you are doing well.
    website development company in delhi

    ReplyDelete
  44. Superb valuable information by your blog comment keep sharing, thank you so much for share us.
    Motorcycle Tours in India

    ReplyDelete
  45. Very Nice valuable information by this blog keep sharing, thank you for sharing us.
    Lifestyle Magazine India

    ReplyDelete
  46. Very great helpful information for me, by this blog keep sharing, thank you for sharing with us.
    Sea Freight Company in India

    ReplyDelete
  47. Hi! Thanks for sharing this very interesting article
    Check out our products: Solenoid valve (van điện từ) , butterfly valve (van bướm), balance valve (van cân bằng).


    ReplyDelete
  48. awesome ..i was struggling with this from few days...

    custom Packaging Boxes usa

    ReplyDelete
  49. I read it because I found the post interesting. See my services, too:
    guide thinking maths (hướng dẫn học toán tư duy),
    Baby learns to count numbers(bé học đếm số),
    Math thinking for children 4-6 years old ( toán tư duy cho trẻ 4 - 6 tuổi).

    ReplyDelete
  50. This is great article. Finally a solution. I'm working for a ERP software company in Chennai.I will check this out today and let you know the update.

    ReplyDelete
  51. Bài viết bạn rất hay:



    Chúng tôi là đơn vị cung cấp các sản phẩm chất lượng như:



    lều xông hơi



    lều xông hơi sau sinh



    lều xông hơi giá bao nhiêu




    ReplyDelete
  52. Bài viết bạn rất hay:



    Chúng tôi là đơn vị cung cấp các sản phẩm chất lượng như:



    Giảo cổ lam



    giảo cổ lam giải độc gan



    giảo cổ lam giảm béo

    ReplyDelete
  53. Bài viết bạn rất hay:



    Chúng tôi là đơn vị cung cấp các sản phẩm chất lượng như:



    lều xông hơi



    lều xông hơi sau sinh



    lều xông hơi giá bao nhiêu




    ReplyDelete
  54. I read out your blog and I got a lot of valuable information. Visit Oge Infosystem for the Creative and Ecommerce Website Designing in Delhi.
    SEO Service in Delhi

    ReplyDelete
  55. Your blog was really impressive and meaningful. Thank you so much for sharing with us. Visit Y&H Cargo for Shipping and Freight Forwarding.
    Logistics Company in India

    ReplyDelete
  56. I really like your blog. thank you for sharing with us.

    Lawn Collection 2019

    ReplyDelete
  57. En rigtig interessant og interessant artikel. Takket være den delte administrator. Ønsker du altid lykkes

    lưới chống chuột

    cửa lưới dạng xếp

    cửa lưới tự cuốn

    cửa lưới chống muỗi hà nội

    ReplyDelete
  58. Bài viết rất hay: Chúng tôi chuyên cung cấp các sản phẩm chất lượng sau:



    bồn massage



    bon ngam chan



    máy massage chân



    Cảm ơn các bạn!

    ReplyDelete
  59. I am really happy to say this I am deeply read your article, I am searching like this type valuable information, it’s really helpful for me, I am happy to found it, thank you so much for share this blog, great work, keep sharing like this type of article, thank you so much for read my comment, if any one searching website designing company in India please visit my website
    Website Designing Company in Delhi

    ReplyDelete
  60. 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
  61. Thanks for posting keep updating. We are Excellent company for
    Website Development Company in Delhi

    ReplyDelete
  62. All of the above information is very attractive to me. Thank you very much for sharing this interesting information.

    Giảo cổ lam

    giảo cổ lam 5 lá

    giảo cổ lam 7 lá

    giảo cổ lam khô

    ReplyDelete
  63. Custom Boxes presents brilliant and extraordinary packaging arrangements in
    spite of the fact that these packaging boxes UK are available for customers with countless customization alternatives.

    ReplyDelete
  64. BBC Cargo Shipping services in Dubai had started its operations, established by the Experienced Emarati Management who have a great experience in Freights Fields, and in a few Months, it became one of the leading International Freight Forwarding and Shipping Agents in the Middle East and around the World.

    ReplyDelete
  65. Bài viết rất hay: Chúng tôi có các dịch vụ in ấn chuyên nghiệp sau:



    in bao bì vỏ hộp

    in hộp bìa cứng

    ReplyDelete
  66. There is 3 major business card thickness. That the Business Card Printing Co. using as per client requirements.The first one is 14PT (Point Cardboard Stock). The Second one is 16PT (Point Cardboard Stock). The third one is 18PT (Point Cardboard Stock) you can choose one
    of them. The Standard size business cards are mostly printed on 16PT thickness stock with reasonable prices. I will definitely recommend you to the standard business cards paper stock. Here are some business card paper types. Youcan also use business card boxes to hold your cards on the office tables.

    ReplyDelete
  67. The Custom boxes is One-stop shop for custom printed boxes and custom boxes packaging wholesale that fit to your budget.

    ReplyDelete
  68. Brands Routers majorly use 192.168.l78.1 Admin Login password in their Modem devices to setting up the network connection. If you are accessing your admin page by typing 192.168.l78.1 login into your web address bar, then you are must be using your Routers from Countries like Germany, Netherlands, Switzerland, Austria, Namibia & neighbourings. Most commonly, FRITZ!Box use the 192.168.178.1 as the default IP Address for their routers.

    ReplyDelete
  69. The Custom boxes is One-stop shop for custom printed boxes and custom boxes packaging wholesale that fit to your budget.

    ReplyDelete
  70. Thank you, for your valuable Information , so that users can spend more time in reading and go deep into the website.

    Erp Software Development Company in chennai

    ReplyDelete
  71. Correct. I wasn't suggesting an order, I was saying the modules needed in MVC will need preCondition


    ReplyDelete

  72. Thank you, for your valuable Information

    ReplyDelete
  73. Are you searching for 10ml Bottle Boxes? you're on the right place to induce
    top of the range custom printed 10 bottle boxes. Custom Packaging Co. is giving you bulk cardboard boxes from
    the last ten years. we have a tendency to a square measure dealing all told types of custom bottle boxes in custom styles, styles, shapes, and colors.
    Custom Bottle Boxes has to style as per your company stigmatization. Our
    production team forever able to assist you together 15ml Bottle Boxes with your customization.

    ReplyDelete