ASP.NET MVC 默认 URL 视图
我正在尝试将 MVC 应用程序的默认 URL 设置为应用程序区域内的视图。该区域称为“Common”,控制器为“Home”,视图为“Index”。
我尝试将 web.config 的表单部分中的 defaultUrl 设置为“~/Common/Home/Index”,但没有成功。
我还尝试在 global.asax 中映射新路线,因此:
routes.MapRoute(
"Area",
"{area}/{controller}/{action}/{id}",
new { area = "Common", controller = "Home", action = "Index", id = "" }
);
再次无济于事。
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".
I've tried setting the defaultUrl in the forms section of web.config to "~/Common/Home/Index" with no success.
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 = "" }
);
Again, to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您列出的路由仅在明确输入 URL 时才有效:
该路由的含义是:
如果我收到具有有效
{area}
、有效{controller} 位于该区域,并且有效的
{action}
位于该控制器中,然后将其路由到那里。您想要的是,如果他们只访问您的网站
yoursite.com
,则默认为该控制器:这表示如果他们不向
http://yoursite.com 附加任何内容
然后将其路由到以下操作:Common/Home/Index
另外,将其放在路由表的顶部。
确保您还让 MVC 知道注册应用程序中的区域:
将以下内容放入
Global.asax.cs
文件的Application_Start
方法中:The route you listed only works if they explicitly type out the URL:
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.What you want is to default to that controller if they just visit your site,
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.
Makes sure you're also letting MVC know to register the areas you have in the application:
Put the following in your
Application_Start
method in theGlobal.asax.cs
file:你需要做的是:
从global.asax.cs中删除默认路由
更新 Common 区域中的 SecurityAreaRegistration.cs
添加以下路由映射:
What you have to do is:
Remove Default Route from global.asax.cs
Update SecurityAreaRegistration.cs in area Common
Add following route mapping:
你正在做的事情看起来是正确的。如果我不得不猜测,我会说这是由于您运行网站的方式而发生的。在 Visual Studio 中,如果您在按 F5 时选择了特定视图,则该视图将成为起始 Url - 尝试选择项目,然后按 F5?
What you're doing seems correct. If I had to guess I would say this is happening due to the way you are running your website. In Visual Studio, if you have a specific view selected when you hit F5 that view will be the starting Url - try selecting the Project and then hitting F5?
在 Global.asax 中
删除 .MapRoute
并使其看起来像
在该区域的 yourareaAreaRegistration.cs 下(如果您通过 VS 添加该区域,则它就在那里)
In the Global.asax
delete the .MapRoute
and make it look like
then under the area's yourareaAreaRegistration.cs (its there if you added the area through VS)