ASP.Net MVC:使用路由重写

发布于 2024-12-08 17:25:47 字数 383 浏览 1 评论 0原文

我想将以下 URI: 映射

/admin/controller/action/id

到以下内容:

Controller -> Controller
Action     -> Admin_Action

例如:

/admin/Users/Create

Controller -> Users
Action  -> Admin_Create

/admin/Users/Delete/1

Controller -> Users
Action -> Admin_Delete(1)

我可以使用路由规则实现这一点吗?

I want to map the following URI:

/admin/controller/action/id

to the following:

Controller -> Controller
Action     -> Admin_Action

For example:

/admin/Users/Create

Controller -> Users
Action  -> Admin_Create

/admin/Users/Delete/1

Controller -> Users
Action -> Admin_Delete(1)

Can I achieve that using routing rules?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别忘他 2024-12-15 17:25:47

我认为以下路线映射应该有效......

 routes.MapRoute("YourRouteName", "admin/controller/action/{id}", new { controller = "Controller", action = "Admin_Action", id = UrlParameter.Optional });

I think the following route mapping should work ...

 routes.MapRoute("YourRouteName", "admin/controller/action/{id}", new { controller = "Controller", action = "Admin_Action", id = UrlParameter.Optional });
邮友 2024-12-15 17:25:47

使用这个:

routes.MapRoute("Admin",  // Route name
  "admin/{controller}/{action}/{id}",  // URL with parameters
  new { controller = "Controller", action = "Admin_Action", id = UrlParameter.Optional }  // Parameter defaults
);

但是如果你愿意你也可以这样做
www.example.com/admin/

routes.MapRoute(
    "Default",                                              
    "admin/{controller}/{action}/{id}",                     
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
);

更新

routes.MapRoute(
    "AdminArea",                                              
    "admin/{controller}/{action}/{id}",                     
    new { controller = "Users", action = "Index", id = UrlParameter.Optional } 
);

Use this:

routes.MapRoute("Admin",  // Route name
  "admin/{controller}/{action}/{id}",  // URL with parameters
  new { controller = "Controller", action = "Admin_Action", id = UrlParameter.Optional }  // Parameter defaults
);

But if you want you can also do
www.example.com/admin/

routes.MapRoute(
    "Default",                                              
    "admin/{controller}/{action}/{id}",                     
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
);

Update

routes.MapRoute(
    "AdminArea",                                              
    "admin/{controller}/{action}/{id}",                     
    new { controller = "Users", action = "Index", id = UrlParameter.Optional } 
);
小忆控 2024-12-15 17:25:47

您可以使用 RouteHandler 在操作前面添加名称,正如我在 这个答案

如果您创建一个名为 AdminHandler 的 RouteHandler 来在您的操作前面添加“Admin_`,则您可以按如下方式定义您的路线:

routes.MapRoute("Admin",  // Route name 
    new Route("admin/{controller}/{action}/{id}",  
    new RouteValueDictionary(  
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }),  
        new AdminHandler() 
    ) 
);  

但是,我同意 brodie 的观点,您应该将所有管理操作放在一个单独的区域中,因为它是更好的设计,并且使安全性和维护更容易。

You can prepend names to actions using a RouteHandler, as I describe in this answer.

If you create a RouteHandler named AdminHandler to prepend "Admin_` to your actions, you can then define your route as follows:

routes.MapRoute("Admin",  // Route name 
    new Route("admin/{controller}/{action}/{id}",  
    new RouteValueDictionary(  
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }),  
        new AdminHandler() 
    ) 
);  

However, I agree with brodie that you should place all admin actions in a separate Area, as it is a better design, and makes security and maintenance easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文