2010-03-11 99 views
21

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

我可以用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提供默认文档?

+0

也许你会得到serverfault.com – tzenes 2010-03-11 04:47:31

+0

更好的反应这不是一个IIS7问题,它是一个ASP.NET MVC的问题。默认的文件被不使用ASP.NET MVC – 2010-03-12 02:24:49

+0

时担任完美其实我很困惑为什么会需要做 – 2010-03-12 02:33:55

回答

-3

我怀疑你自己的index.html加入该扩展将是未知的MVC框架。在MVC

您的默认索引页//域名/家庭/索引和物理的Index.aspx。

如果您在使用//域,那么路由引擎将承担/home/index.aspx而不是命名为index.html域。

+0

路由引擎将不会路由到家庭控制器上的索引操作,因为正如我所说,我已将global.asax中的所有路由注释掉了。 有磁盘上的index.html文件和IIS7设置为index.html作为默认的文档使用,但ASP.NET MVC以某种方式防止这种 – 2010-03-12 02:16:13

2

我找到了解决办法。如果你想的index.html是在你的MVC应用程序的根(即旁边的控制器/模型/视图/应用程序数据等文件夹),你可以这样做:

说你有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.html现在是在同一水平作为我的csproj文件。

1

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

routes.IgnoreRoute("index.html") 
etc 
+3

不幸的是,这并不一个“默认文档”工作,因为用户不要在地址中输入index.html。如果你直接去index.html,这个工作。 – mkchandler 2010-09-10 14:38:05

+0

@ mc2thaH您需要将IIS中的“default document”设置从default.aspx更改为index.html。 OP说他做到了。 – tarn 2010-09-13 01:33:48

+1

mkchandler是正确的,即使使用默认文档集,它也不适合我。相反,我不得不添加 routes.IgnoreRoute(“”) – pius 2011-07-01 12:11:28

2

我有一个应用程序的WebForms类似的问题。在web.config中确保system.webServer下的StaticFile处理程序的resourceType属性设置为Either。

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

的'routes.IgnoreRoute(“”);'方法并没有为我工作。这样做。谢谢。 – 2013-01-25 14:57:09

+0

我可以在Orchard中使用它来获得默认文档的工作。 添加DefaultDocumentModule并让resourceType =“任一”是关键。 James 2014-07-29 10:29:15

27

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(""); 
+0

“因此,路由寻找那个名字的控制器,换句话说,它会寻找一个没有名字的控制器。“ - 实际上,它正在寻找Home/Index - 这些是MapRoute调用中的参数默认值 – 2017-03-22 19:27:01

2

对不起,复活这具木乃伊,但我不”相信这个问题曾经是一个默认的文档问题。事实上,您可能不想像其他答复者所说的那样设置默认文档集。

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