问题描述
有没有人能够得到ASP.NET MVC 2的地区工作?
Has anyone been able to get the Areas in ASP.NET MVC 2 to work?
我创建了一个名为安全的新领域,并放置一个新的控制器,任命HomeController的。然后我创建了一个新的家庭/的Index.aspx视图。然而,当我浏览到的,它提供了404资源无法找到。 给出了同样的错误。
I created a new Area called "Secure" and placed a new controller in it named HomeController. I then Created a new Home/Index.aspx view. However, when I browse to http://localhost/myapp/Secure/ it gives a 404 resource cannot be found. http://localhost/myapp/Secure/Home gives the same error.
我区登记看起来是这样的:
My area registration looks like this:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Secure_default",
"Secure/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
我也试过这样的:
I also tried this:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Secure_default",
"Secure/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
谢谢,
贾斯汀
Thanks,Justin
推荐答案
当然,我有地区与ASP.NET MVC 2的工作。
Sure, i've got Areas working with ASP.NET MVC 2.
它的人很难调试路由随着SO,最简单的方法是你使用的。它会告诉你路线(和地区)正在被解决什么特定的URL。非常方便。
Its hard for people to debug routes over SO, the easiest way is for you to use Phil Haacks Route Debugger. It'll tell you what routes (and areas) are being resolved for a particular URL. Extremely handy.
不过我带刺,试着改变你的路线,这样的:
However ill take a stab, try changing your route to this:
context.MapRoute(
"Secure_default",
"Secure",
new { action = "Index", controller = "Home", id = UrlParameter.Optional }
);
的URL(< yourhost> /安全
)会发现你的区域安全
,但不知道是哪个控制器,你到手的请求,因为你没有指定一个默认值控制器
在你的地区航线。
The url (<yourhost>/Secure
) will find your area Secure
but not know which controller you hand the request to, as you have not specified a default value for controller
in your areas route.
下面是我的设置:
Areas
Albums
Controllers
AlbumsController.cs (namespace Web.Areas.Albums.Controllers)
Models
Views
Albums
Index.aspxx
AlbumsAreaRegistration.cs
context.MapRoute(
"Albums_Default",
"Albums",
new { controller = "Albums", action = "Index", id = UrlParameter.Optional)
的网址:触发我的AlbumsController中的指数行动在我的相册区域。
The URL: http://localhost/Albums triggers the "Index" action of my "AlbumsController" in my "Albums" area.
什么是你的结构是什么样子?
What does you structure look like?
这篇关于ASP.NET MVC 2区404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!