ASP.NET MVC不提供默认文档

时间:2021-01-13 01:48:57

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

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

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

6 个解决方案

#1


28  

ASP.Net MVC routing is controlled by the Global.asax / Global.asax.cs files. 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 FOUND error 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("");

#2


2  

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属性设置为Either。

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

#3


2  

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应用程序的根目录中(即控制器/ model / view / appdata等文件夹旁边),则可以执行以下操作:

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

假设你有home.html,aboutus.html和contactus.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 are now at the same level as my CSPROJ file.

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

#4


2  

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。一旦我改变它正确加载。

#5


1  

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

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

routes.IgnoreRoute("index.html")
etc

#6


-3  

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.

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

#1


28  

ASP.Net MVC routing is controlled by the Global.asax / Global.asax.cs files. 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 FOUND error 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("");

#2


2  

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属性设置为Either。

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

#3


2  

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应用程序的根目录中(即控制器/ model / view / appdata等文件夹旁边),则可以执行以下操作:

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

假设你有home.html,aboutus.html和contactus.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 are now at the same level as my CSPROJ file.

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

#4


2  

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。一旦我改变它正确加载。

#5


1  

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

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

routes.IgnoreRoute("index.html")
etc

#6


-3  

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.

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