简体   繁体   English

ASP.Net MVC中的区域路由问题

[英]Area Routing Issues in ASP.Net MVC

It seems as if area routing is an issue for me. 似乎区域路由对我来说是个问题。 Here is my problem, I have an application with one area named Administration, I have a controller named DepartmentController. 这是我的问题,我有一个名为“管理”的应用程序,有一个名为“ DepartmentController”的控制器。 Now I need to create a route that will generate a link like http://www.example.com/Administration/Setup/Department . 现在,我需要创建一条路由,该路由将生成一个类似于http://www.example.com/Administration/Setup/Department的链接。

Here is what I have done. 这是我所做的。

In the Route.Config file of the application I added a custom route like so: 在应用程序的Route.Config文件中,我添加了一个自定义路由,如下所示:

routes.MapRoute(
            name: "DeptSetup",
            url: "Setup/Department/{action}/{id}",
            defaults: new { controller = "Department", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "Administration.Controllers" }
        );

With that I got The resource cannot be found. 有了,我得到找不到资源。

I tried the AdmininstrationAreaRegistration like so: 我像这样尝试了AdmininstrationAreaRegistration:

  context.MapRoute(
                name: "DeptSetup",
                url: "Setup/Department/{action}/{id}",
                defaults: new { controller = "Department", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] { "Administration.Controllers" }
            );

All didnt work. 所有没有工作。 Pls point me in the right direction. 请指出正确的方向。

Thanks 谢谢

You are missing the area part in the route URL template. 您在路线URL模板中缺少区域部分。 Using AdmininstrationAreaRegistration is a right way as areas should be responsible for registering their own routes, but the route itself should look like: 使用AdmininstrationAreaRegistration是正确的方法,因为区域应该负责注册自己的路线,但是路线本身应如下所示:

url: "Administration/Setup/Department/{action}/{id}"

you should use Area registration ( AdmininstrationAreaRegistration ) class and should add Administrator (area name) to your url template: 您应该使用区域注册AdmininstrationAreaRegistration )类,并应将Administrator (区域名称)添加到您的url模板中:

context.MapRoute(
            name: "DeptSetup",
            url: "Administrator/Setup/{controller}/{action}/{id}",
            defaults: new { controller = "Department", action = "Index", id = UrlParameter.Optional }
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM