asp.net-mvc ASP.NET MVC 不提供默认文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2422542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:11:44  来源:igfitidea点击:

ASP.NET MVC not serving default document

asp.net-mvc

提问by Jon Cahill

I have an ASP.NET MVC application where the default page should be index.html which is an actual file on disk.

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

I can browse to the file using www.mydomain.com/index.html so I know it will be served and exists but if I use www.mydomain.com I get a 404.

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

I have ensured that the default document is correctly set in IIS7 and I have even gone so far as to commented out all the routes defined in my global.asax to ensure I don't have a route causing this problem.

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

So to summarize:

所以总结一下:

  • I have an index.html file on disk and IIS7 is set to use index.html as the default document.
  • If I remove my ASP.NET MVC application and leave the index.html file is served as the default document as expected.
  • If I publish my ASP.NET MVC application then the index.html doesn't get served by default document.
  • 我在磁盘上有一个 index.html 文件,IIS7 设置为使用 index.html 作为默认文档。
  • 如果我删除我的 ASP.NET MVC 应用程序并保留 index.html 文件作为预期的默认文档。
  • 如果我发布我的 ASP.NET MVC 应用程序,则默认文档不会提供 index.html。

Does anyone know how to get ASP.NET MVC to serve the default document?

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

回答by pius

ASP.Net MVC routing is controlled by the Global.asax/ Global.asax.csfiles. The out-of-the-box routing looks like this:

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
        );
}

When no path is specified, ie. www.domain.tld/, the route is the empty string, "". Therefore the routing looks for a controller with that name. In other words, it looks for a controller with no name at all. When it finds no such controller it serves up a 404 NOT FOUNDerror page.

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

To solve the problem, either map that path to something meaningful or else ignore that route entirely, passing control over to the index.html file:

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

routes.IgnoreRoute("");

回答by user398753

I found a way around this. If you want index.html to be in the root of your MVC application (i.e next to your controller/model/view/appdata etc folders), you can do this:

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

Say you have home.html, aboutus.htmland contactus.html.

说你有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.htmlaboutus.htmlindex.htmlare now at the same level as my CSPROJ file.

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

回答by Mabdullah

Sorry for resurrecting this mummy, but i don't believe this issue was ever a default document issue. In fact you probably don't want to have a default document set as many of the other answerers have stated.

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

Had this problem as well, a similar problem. the cause of my issue was that the Application Pool for the site was set to use .NET Framework v2 and should have been set to v4. once I changed that it loaded correctly.

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

回答by Luke Smith

I had a similar problem with a WebForms application. In your web.config make sure the resourceType attribute of the StaticFile handler under system.webServer is set to Either.

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

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

回答by tarn

You could ignore the route in your MVC application and let IIS serve it.

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

routes.IgnoreRoute("index.html")
etc

回答by griegs

I suspect you added index.html yourself as that extension would be unknown to the mvc framework.

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

Your default index page in mvc is //domain/home/index and is physically index.aspx.

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

If you call your domain using //domain then the routing engine will assume /home/index.aspx and not index.html.

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