IIS returns 404 for ASP.NET MVC3 file (.cshtml)

22,897

Solution 1

Yay! After several days of struggling I've found it! :))

One way to make it work on IIS 7.0 is to enable runAllManagedModulesForAllRequests:

<modules runAllManagedModulesForAllRequests="true" />

(Although, on IIS 7.5 it works even with the default value runAllManagedModulesForAllRequests="false".)

Don't ask me why:) Maybe someone can explain that.

Solution 2

Not sure if this is your particular problem, but it sounds like ASP.NET Web Pages isn't running (MVC 3 and up depend on Web Pages for Razor and other things). Sometimes things can get installed in orders that mess up some configuration of ASP.NET and IIS, which in turn could make it so Web Pages isn't running and no cshtml file will get executed.

Try running aspnet_regiis /i to clean it all up.

Solution 3

There is an hotfix for IIS7.0 available at: http://support.microsoft.com/kb/980368

I installed it today on windows 2008 SP2 (which requires an OS restart). The behavior in IIS 7.0 is now similar than IIS 7.5

It fixed the problem on my side / without forcing:

<modules runAllManagedModulesForAllRequests="true" />

This is a better approach than running all modules for any request (even on css/images/...) which would cause unnecessary overload.

Share:
22,897
Abimael López
Author by

Abimael López

Updated on July 10, 2022

Comments

  • Abimael López
    Abimael López almost 2 years

    I have standard ASP.NET 4 application. Inside this application there are also few razor (e.g. test.cshtml) files. ASP.NET application works however if I point browser to /test file (without extension). IIS (static file handler) returns 404.

    The same application and even the standalone .cshtml files are working on local machine (with IIS7.5/Win7) and a server with (IIS7.5/Win2008R2), however it does not work the server with IIS7.0/Win2008.

    MVC3 is installed on both machines, app pools on both machines are .NET 4.0 integrated.

    How the .cshtml handlers works? When the IIS decides to use static file handler and when the .cshtml handler? What else should I check?

    EDIT:

    Clarification - I am not actually using whole MVC framework, it is just like plain-old .ASPX or PHP file. No controllers, no routes, no 'views'. I am using it for a simple script in addition to old ASP.NET application.

    I think it is not a programming issue, since it works on local computer, but rather a configuration issue - the IIS on the Win2008 server might not be configured properly.

    EDIT2:

    (Machines with IIS7.5 works, only machine with IIS7.0 does not work. But I am not sure whether it is a problem of IIS version.)

    EDIT3:

    On my local computer where it works, I have not set any routing. TheCodeKing and Keith suggested that I need to have a routing somewhere. Maybe yes, but it is not in my application. It might be set by default somewhere in IIS configuration.

    Do you have any hints where this 'implicit' routing could be found?

    EDIT4:

    Note: On all computers there is HttpForbiddenHandler for *.cshtml, since you cannot download the cshtml file. And there is also the TransferRequestHandler for *.. (Both are enabled.)

    EDIT5:

    I have found that on the servers, where it is working, I need to have any .cshtml file in the web root. If I remove the .cshtml from the web root and try to invoke any from a subdirectory it will not work.

    EDIT6:

    I tried that on another two machines: one with IIS 7.5 and another with IIS 7.0. On IIS 7.5 it works (plain install no configuration), but on IIS 7.0 it does not work.

    How to make it work on IIS 7.0?

  • Abimael López
    Abimael López over 12 years
    /test is not a directory. It should invoke test.cshtml. Moreover it would return 403 (not 404).
  • Abimael López
    Abimael López over 12 years
    I do not need to setup routes, controllers, nor to have a Views folder. I can use .cshtml file like an old .aspx or php file.
  • Abimael López
    Abimael López over 12 years
    Why it works without routing on my local computer and one other machine, where is the same code (ASP.NET application and few standalone .cshtml scripts)? (Just one server has 404 problems.)
  • TheCodeKing
    TheCodeKing over 12 years
    So have you setup routing? How are you mapping the extension-less urls to .cshtml? Maybe you could show some code? What versions of IIS work, which don't? Are they all the same?
  • Abimael López
    Abimael López over 12 years
    No routing at all. Try to create a simple ASP.NET application in IIS a place test.cshtml file inside (e.g. put "hi" inside). Point browser to that file (without .cshtml extension).
  • TheCodeKing
    TheCodeKing over 12 years
    If I create a new ASP.NET forms app, create test.cshtml and request /test I get a 404. If I try /test.cshtml I naturally get this type of file cannot be served. We need to know how you have set up routing for the .cshtml. This is the key to your issue. You mentioned a simple script?
  • Abimael López
    Abimael López over 12 years
    With the script I mean test.cshtml file. Content could be just "The time is @DateTime.Now.". See EDIT3.
  • Abimael López
    Abimael López over 12 years
    Common ASP.NET web pages are running ok. For sure, I have run aspnet_regiss /i (for .NET 4), but it does not help (since it just registers ASP.NET, I have also reinstalled ASP.NET MVC3.
  • tugberk
    tugberk over 12 years
    Yeah, it is also explained here :) west-wind.com/weblog/posts/2011/Mar/27/…
  • Abimael López
    Abimael López over 11 years
    Thank you for the information. I no longer have any Windows Server 2008 with IIS 7.0, so someone else has to confirm that.
  • cusman
    cusman almost 11 years
    More specifically C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis -i
  • Dave Black
    Dave Black almost 7 years
    @Keith setting up your development environment to use IIS Express does not match your web server. You're going to be running IIS on your web server - NOT IIS Express
  • Keith
    Keith almost 7 years
    @DaveBlack This comment was from 6 years ago when VS2010 was all the rage, and Cassini was the default. Lots of problems (such as with web.config) that weren't found on Cassini, but were on your Production IIS server, were MUCH more likely to be replicated in IIS Express.