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

时间:2023-02-13 11:21:29

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. Now I need to create a route that will generate a link like http://www.example.com/Administration/Setup/Department.

看起来区域路由对我来说是一个问题。这是我的问题,我有一个名为Administration的区域的应用程序,我有一个名为DepartmentController的控制器。现在我需要创建一个生成链接的路线,例如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

2 个解决方案

#1


0  

You are missing the area part in the route URL template. Using AdmininstrationAreaRegistration is a right way as areas should be responsible for registering their own routes, but the route itself should look like:

您缺少路径URL模板中的区域部分。使用AdmininstrationAreaRegistration是一种正确的方式,因为区域应负责注册自己的路由,但路由本身应如下所示:

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

#2


0  

you should use Area registration (AdmininstrationAreaRegistration) class and should add Administrator (area name) to your url template:

您应该使用区域注册(AdmininstrationAreaRegistration)类,并应将管理员(区域名称)添加到您的网址模板:

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

#1


0  

You are missing the area part in the route URL template. Using AdmininstrationAreaRegistration is a right way as areas should be responsible for registering their own routes, but the route itself should look like:

您缺少路径URL模板中的区域部分。使用AdmininstrationAreaRegistration是一种正确的方式,因为区域应负责注册自己的路由,但路由本身应如下所示:

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

#2


0  

you should use Area registration (AdmininstrationAreaRegistration) class and should add Administrator (area name) to your url template:

您应该使用区域注册(AdmininstrationAreaRegistration)类,并应将管理员(区域名称)添加到您的网址模板:

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