How do I get rid of Home in ASP.Net MVC?

◇◆丶佛笑我妖孽 提交于 2019-12-03 15:57:39

问题


I know this site is written using ASP.Net MVC and I do not see "/Home" in the url. This proves to me that it can be done. What special route and do I need?


回答1:


Just change "Home" to an empty string.

routes.MapRoute(
    "Home",
    "",
    new { action = Index, controller = Home }
);



回答2:


If you're running on IIS 7, you can simply delete the Default.aspx file that comes with ASP.NET MVC (assuming you're running on Preview 3 or higher). That file was needed due to an issue with Cassini that was fixed in .NET 3.5 SP1. For more details check out:

http://haacked.com/archive/2008/04/10/upcoming-changes-in-routing.aspx and http://haacked.com/archive/2008/05/12/sp1-beta-and-its-effect-on-mvc.aspx




回答3:


I actually like having all of my home controller methods to be at the root of the site. Like this: /about, /contact, etc. I guess I'm picky. I use a simple route constraint to do it. Here is my blog post with a code sample.




回答4:


I'd add

routes.MapRoute("NoIndex", "{action}", new { controller = "Home", action = "Index" });

in RouteConfig.cs




回答5:


This is what I did to get rid of Home. It will treat all routes with only one specifier as Home/Action and any with two as Controller/Action. The downside is now controller has to have an explicit index (/Controller != /Controller/Index), but it might help you or others.

routes.MapRoute(
    "Default",
    "{action}",
    new { controller = "Home", action = "Index" }
);

routes.MapRoute(
    "Actions",
    "{controller}/{action}",
    new { }
);



回答6:


In IIS 7, you can simply delete the Default.aspx file that comes with ASP.NET MVC (assuming you're running on Preview 3 or higher). That file was needed due to an issue with Cassini that was fixed in .NET 3.5 SP1.

For more details check out:

Upcoming Changes In Routing and .NET 3.5 SP1 Beta and Its Effect on MVC



来源:https://stackoverflow.com/questions/15470/how-do-i-get-rid-of-home-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!