【问题标题】:ASP.NET MVC not serving default documentASP.NET MVC 不提供默认文档
【发布时间】:2010-03-11 04:28:51
【问题描述】:

我有一个 ASP.NET MVC 应用程序,其中默认页面应该是 index.html,它是磁盘上的实际文件。

我可以使用 www.mydomain.com/index.html 浏览到该文件,所以我知道它会被提供并存在,但如果我使用 www.mydomain.com,我会得到 404。

我已确保在 IIS7 中正确设置了默认文档,甚至还注释掉了 global.asax 中定义的所有路由,以确保我没有导致此问题的路由。

总结一下:

  • 我在磁盘上有一个 index.html 文件,并且 IIS7 设置为使用 index.html 作为默认文档。
  • 如果我删除了我的 ASP.NET MVC 应用程序并保留 index.html 文件作为预期的默认文档。
  • 如果我发布了我的 ASP.NET MVC 应用程序,那么 index.html 不会在默认文档中得到服务。

有谁知道如何让 ASP.NET MVC 为默认文档提供服务?

【问题讨论】:

  • 也许你会在 serverfault.com 上得到更好的回应
  • 这不是 IIS7 问题,而是 ASP.NET MVC 问题。不使用 ASP.NET MVC 时,默认文档会得到完美服务
  • 我真的很困惑为什么需要这样做
  • 我想要/需要这样做的原因很简单。我需要向由静态 html 文件组成的现有站点添加一些功能。我想要做的就是将使用 ASP.NET MVC 的功能添加到站点的一个区域,但保持其余部分不变。
  • 我也遇到了同样的问题。你找到解决方案了吗?

标签: asp.net-mvc


【解决方案1】:

ASP.Net MVC 路由由 Global.asax / Global.asax.cs 文件控制。开箱即用的路由如下所示:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
}

当没有指定路径时,即。 www.domain.tld/,路由是空字符串,""。因此,路由会寻找具有该名称的控制器。换句话说,它会寻找一个根本没有名字的控制器。当它找不到这样的控制器时,它会提供一个404 NOT FOUND 错误页面。

要解决这个问题,要么将该路径映射到有意义的地方,要么完全忽略该路径,将控制权交给 index.html 文件:

routes.IgnoreRoute("");

【讨论】:

  • “因此,路由会查找具有该名称的控制器。换句话说,它会查找根本没有名称的控制器。” -- 实际上,它正在寻找 Home/Index -- 这些是 MapRoute 调用中的参数默认值
【解决方案2】:

我在使用 WebForms 应用程序时遇到了类似的问题。在您的 web.config 中,确保 system.webServer 下 StaticFile 处理程序的 resourceType 属性设置为 Either。

<add name="StaticFile" path="*" verb="*" type="" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" scriptProcessor="" resourceType="Either" ...

【讨论】:

  • routes.IgnoreRoute(""); 方法对我不起作用。这做到了。谢谢。
  • 我可以将它与 Orchard 一起使用以使默认文档正常工作。添加 DefaultDocumentModule 并具有 resourceType="Either" 是关键。
【解决方案3】:

我找到了解决这个问题的方法。如果您希望 index.html 位于 MVC 应用程序的根目录中(即在您的控制器/模型/视图/appdata 等文件夹旁边),您可以这样做:

假设你有home.htmlaboutus.htmlcontactus.html

//this route matches (http://mydomain.com/somekindofstring)

routes.MapRoute(
    "SingleRootArg",
    "{id}",
    new { controller = "Home", action = "Details", id=""});

// so now that you have your route for all those random strings. 
// I had to do this for vanity urls. mydomain.com/ronburgandy  etc. however, 
// mydomain.com/index.html will also come in just how you'd expect. 

//Just an example of what you COULD do. the user would see it as root html. 

Public ActionResult Details(string id)
{

    //let me save you the trouble - favicon really comes in as a string *ANGER*
    if(String.IsNullOrEmpty(id) || id.ToLower().Contains("favicon.ico"))
        return Redirect("~/index.html");
    if(id == "aboutus.html")
        return Redirect("~/aboutus.html");
    if(id == "contactus.html")
        return Redirect("~/contactus.html");
    if(id == "index.html")
        return Redirect("~/index.html");
}

index.html aboutus.html index.html 现在与我的 CSPROJ 文件处于同一级别。

【讨论】:

    【解决方案4】:

    很抱歉让这个木乃伊复活,但我不认为这个问题曾经是默认文档问题。事实上,您可能不希望像许多其他回答者所说的那样设置默认文档。

    也有这个问题,类似的问题。我的问题的原因是该站点的应用程序池设置为使用 .NET Framework v2,并且应该设置为 v4。一旦我改变它加载正确。

    【讨论】:

      【解决方案5】:

      您可以忽略 MVC 应用程序中的路由并让 IIS 为其提供服务。

      routes.IgnoreRoute("index.html")
      etc
      

      【讨论】:

      • 不幸的是,这不适用于“默认文档”,因为用户没有在地址中键入 index.html。如果你直接去 index.html,这行得通。
      • @mc2thaH 您需要将 IIS 中的“默认文档”设置从 default.aspx 更改为 index.html。 OP说他这样做了。
      • mkchandler 是对的,即使使用默认文档集,它对我也不起作用。相反,我不得不添加 routes.IgnoreRoute("")
      【解决方案6】:

      我怀疑你自己添加了 index.html,因为 mvc 框架不知道该扩展。

      你在 mvc 中的默认索引页面是 //domain/home/index 并且物理上是 index.aspx。

      如果您使用 //domain 调用您的域,那么路由引擎将假定 /home/index.aspx 而不是 index.html。

      【讨论】:

      • 路由引擎不会路由到主控制器上的索引操作,因为正如我所说,我已经在 global.asax 中注释掉了所有路由。磁盘上有一个 index.html 文件,IIS7 设置为使用 index.html 作为默认文档,但 ASP.NET MVC 以某种方式阻止了这种情况