在 IIS 7 和 ASP.NET MVC 上诊断 404 错误

19

我有一个使用Cassini开发和测试过的MVC应用程序。将其部署到我的GoDaddy网站上时,默认页面可以正常显示。但当我点击登录时,会出现404错误。

我在IIS 7下运行该应用程序,所以这种情况出乎意料。我的路由非常简单:

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              
            "{controller}/{action}/{id}",                           
            new { controller = "Public", action = "Index", id = "" } 
        );
        routes.MapRoute(
            "Report1",
            "Report/{action}/{start}/{end}",
            new { controller = "Report", action = "Index" }
        );
        routes.MapRoute(
            "Report2",
            "Report/{action}/{start}/{end}/{idList}",
            new { controller = "Report", action = "Index" }
        );
任何想法是什么可能正在发生或者我如何可以解决这个问题吗?
4个回答

31

你是否正在运行IIS7集成模式

IIS7的经典模式不会自动将没有扩展名的URL映射到ASP.NET(与IIS6类似)。

同时确保你的Web.config <system.webServer>标记已经正确配置。


谢谢,Mehrdad。事实证明,GoDaddy将IIS 7默认设置为经典模式。 - Stuart
3
<system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> - Christopher Edwards

24

不要使用runAllManagedModulesForAllRequests。您希望让IIS处理像图片这样的资源。

<system.webServer> <!-- Rather do NOT use this -->
  <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

相反,添加MVC路由模块

<system.webServer>
  <modules>
    <remove name="UrlRoutingModule-4.0" />
    <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
  </modules>
</system.webServer>

这个答案也可行,看起来有更高效(和优雅)的方法。 - Ross Brasseaux
2
这种方法在我的ASP.NET Web API应用程序中效果很好。该应用程序在本地运行良好,但是当部署到其他任何环境时,我只收到了任何Web API请求的404错误。添加了上面提到的路由模块位,一切都很好。类似的问题在这里:https://dev59.com/BGUp5IYBdhLWcg3wRWBD。谢谢! - Adam Weber
我曾经为使 MVC 应用程序接受 HTTP DELETE 请求而困扰不已。这个方法解决了我的问题。谢谢! - Furynation
同样的问题,如果我部署到其他环境,delete请求的方法不起作用:在iis8中可以,但在其他环境(iis7)中会出现404错误。 - ios_dotnet_superuser

14

尝试了所有方法后,我必须像这样设置我的Web配置文件才能使其正常工作。

 <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

2
哇,这真的起作用了!但是为什么我有一种感觉它会回来找我的麻烦呢? - Ross Brasseaux

1
我曾经遇到同样的问题,我上传了控制器、web.config和其他类文件,但是我忘记上传bin文件夹。
在我上传了bin文件夹之后,它就可以工作了!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接