使用时区的默认MVC4路线 [英] MVC4 Default route when using areas
问题描述
我试图MVC应用程序中使用的区域,我想缺省路由将被解析到管理区域内的HomeController的,但它解析为根网站的家居控制器。
我加了管理员的HomeController的命名空间,但它仍然决心根HomeController的。
I'm trying to use areas within MVC app, I would like that the default route will be resolved to the HomeController within the admin area but it resolves to the home controller in root site. I added the namespace of the admin HomeController but it still resolved to the root HomeController.
我的路由配置:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] {"MvcApplicationAreas.Areas.Admin.Controllers"}
);
}
}
管理区域航线
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
HomeController的 - 管理区
HomeController - Admin area
namespace MvcApplicationAreas.Areas.Admin.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
任何想法,为什么它不会妥善解决?
谢谢
Any idea why it wouldn't resolve properly? Thanks
推荐答案
我测试过你的code该地区注册和它的作品,并选择合适的控制器。但是,鉴于决议认定在根文件夹的视图,即使使用正确的控制器。
I've tested your code for the area registration and it works and selects the right controller. However, the view resolution finds the view in the root folder, even if the right controller is used.
要测试,我用下面的入户指标的行动我区家居控制器:
To test, I used the following home index action in my areas home controller:
public ActionResult Index()
{
return View(model: "Admin area home controller");
}
然后我index.chstml在/浏览/首页:
Then my index.chstml in /Views/Home:
Root View: @Model
和我index.cshtml在/地区/行政/浏览/首页:
and my index.cshtml in /Areas/Admin/Views/Home:
Admin Area view: @Model
在运行时,输出是:
根视图:管理区家居控制器
Root View: Admin area home controller
所以路线使得在管理方面来看的家居控制器,但随后thew视图分辨率的推移,发现根视图,而不是管理视图。
So the route makes the home controller in the admin area run, but then thew view resolution goes on and finds the root view instead of the admin view.
那么,到底,这的确是视图选择是错误的,所以你的问题是一样的如何设置MVC 缺省路由(一个区域)。
So in the end, it is indeed the view selection that is wrong, so your problem is the same as in How to set a Default Route (To an Area) in MVC.
这篇关于使用时区的默认MVC4路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!