我有一个ASP.NET MVC应用程序,默认页面应该是index.html,它是磁盘上的实际文件。
我可以使用www.mydomain.com/index.html浏览到该文件,因此我知道它将被提供并存在,但如果我使用www.mydomain.com,我会得到404.
我确保在IIS7中正确设置了默认文档,我甚至已经注释掉了我的global.asax中定义的所有路由,以确保我没有导致此问题的路由。
总结一下:
有谁知道如何让ASP.NET MVC提供默认文档?
答案 0 :(得分:28)
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("");
答案 1 :(得分:2)
我找到了解决这个问题的方法。如果您希望index.html位于MVC应用程序的根目录中(即您的控制器/模型/视图/ appdata等文件夹旁边),则可以执行以下操作:
假设您有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
现在与我的CSPROJ文件处于同一级别。
答案 2 :(得分:2)
我遇到了与WebForms应用程序类似的问题。在web.config中,确保system.webServer下的StaticFile处理程序的resourceType属性设置为Either。
<add name="StaticFile" path="*" verb="*" type="" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" scriptProcessor="" resourceType="Either" ...
答案 3 :(得分:2)
很抱歉复活这个木乃伊,但我不相信这个问题曾经是一个默认文件问题。事实上,您可能不希望像其他许多回答者所说的那样设置默认文档。
也有这个问题,类似的问题。我的问题的原因是该站点的应用程序池设置为使用.NET Framework v2,应该已设置为v4。一旦我改变它正确加载。
答案 4 :(得分:1)
您可以忽略MVC应用程序中的路由,并让IIS为其提供服务。
routes.IgnoreRoute("index.html")
etc
答案 5 :(得分:-3)
我怀疑你自己添加了index.html,因为mvc框架不知道该扩展名。
mvc中的默认索引页是// domain / home / index,物理上是index.aspx。
如果使用//域名调用域名,则路由引擎将假定为/home/index.aspx而不是index.html。