ASP.NET MVC默认网址查看 [英] ASP.NET MVC Default URL View
问题描述
我想设置我的申请,区域内我的MVC应用程序视图的默认网址。该地区被称为通用时,控制器的首页,并认为首页
I'm trying to set the Default URL of my MVC application to a view within an area of my application. The area is called "Common", the controller "Home" and the view "Index".
我试着在web.config中的形式部分设置defaultUrl为〜/通用/首页/指数,但没有成功。
I've tried setting the defaultUrl in the forms section of web.config to "~/Common/Home/Index" with no success.
我也试过映射在Global.asax中一个新的路线,这样的:
I've also tried mapping a new route in global.asax, thus:
routes.MapRoute(
"Area",
"{area}/{controller}/{action}/{id}",
new { area = "Common", controller = "Home", action = "Index", id = "" }
);
再次无果。
推荐答案
您只列出的路线工作,如果他们明确打出来的网址:
The route you listed only works if they explicitly type out the URL:
yoursite.com/{area}/{controller}/{action}/{id}
什么这条路线说的是:
What that route says is:
如果我得到了一个有效的的请求{地区}
,一个有效的 {控制器}
在这一领域和一个有效的 {行动}
在控制器上,然后将其路由那里。
If I get a request that has a valid {area}
, a valid {controller}
in that area, and a valid {action}
in that controller, then route it there.
您需要的是默认的控制器如果他们只是访问您的网站, yoursite.com
:
What you want is to default to that controller if they just visit your site, yoursite.com
:
routes.MapRoute(
"Area",
"",
new { area = "Common", controller = "Home", action = "Index" }
);
它说的是,如果他们不附加任何 http://yoursite.com
然后路由到以下动作:通用/首页/指数
What this says is that if they don't append anything to http://yoursite.com
then to route it to the following action: Common/Home/Index
另外,把它放在你的路由表的顶端。
Also, put it at the top of your routes table.
可以确保你还让MVC知道注册在应用程序中有几个方面:
Makes sure you're also letting MVC know to register the areas you have in the application:
放入以下的的Application_Start
方法在的Global.asax.cs
文件:
Put the following in your Application_Start
method in the Global.asax.cs
file:
AreaRegistration.RegisterAllAreas();
这篇关于ASP.NET MVC默认网址查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!