asp.net mvc路由问题

发布于 2024-09-12 17:18:19 字数 460 浏览 4 评论 0原文

默认路由工作正常

mysite.com/home/about

我什至看到如何自定义它以使其更短,

所以我可以说:

mysite.com/edit/1 而不是 mysite.com/home/edit/1

但我怎样才能让它更长地处理像下面这样的 url

mysite.com/admin/user/1 // 有效

// 无效

mysite.com/admin/user/details // 不起作用 mysite.com/admin/user/1 // 有效mysite.com/admin/user/details com/admin/question/create // 不起作用

我不能只将 id 视为一个操作?我需要自定义路线吗?

我是否需要为每个表创建新的控制器,或者我可以通过管理控制器将它们全部路由,

非常感谢

the default routing works fine

mysite.com/home/about

and i even see how to customize it to make it shorter

so i can say:

mysite.com/edit/1
instead of
mysite.com/home/edit/1

but how can i make it longer to handle url like the following

mysite.com/admin/user/1 // works

mysite.com/admin/user/details // does not work

mysite.com/admin/question/create // does not work

i cant just treat the id as an action? i need a custom route?

do i need to create new controllers for each table or can i route them all through the Admin controller

thanks a lot

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

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

发布评论

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

评论(2

黯然 2024-09-19 17:18:19

正如已经提到的,您最好的选择可能是使用新的 Areas< /a> 功能

您可以在没有区域的情况下实现这种类型的路由,但随着控制器数量的增加,站点的可维护性将会降低。本质上,您所做的是将控制器名称硬编码到路由定义中,这意味着您必须为每个新的管理控制器添加新的路由映射。以下是一些示例,说明您可能希望如何在没有区域的情况下设置路线。

routes.MapRoute("AdminQuestions", // Route name
                "admin/question/{action}/{id}", // URL with parameters
                new { controller = "AdminQuestion", action = "Index" } // Parameter defaults
    );

routes.MapRoute("AdminUsers", // Route name
                "admin/user/{action}/{id}", // URL with parameters
                new { controller = "AdminUser", action = "Index" } // Parameter defaults
    );

或者,您可以通过管理控制器路由所有内容,但由于控制器操作执行多个角色,它很快就会变得非常混乱。

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

您的 AdminController 操作如下所示:

public virtual ActionResult Create(string type, int id)
{
    switch (type)
    {
        case 'question':
            // switch/case is code smell
            break;
        case 'user':
            // switch/case is code smell
            break;
        // etc
     }
}

As has been mentioned already, probably your best bet would be to use the new Areas feature

You can achieve this type of routing without Areas, but as the number of controllers gets large the maintainability of your site will diminish. Essentially what you do is to hard-code the controller name into the Route definition which means that you have to add new route mappings for each new Admin controller. Here's a few examples of how you might want to set up your routes without Areas.

routes.MapRoute("AdminQuestions", // Route name
                "admin/question/{action}/{id}", // URL with parameters
                new { controller = "AdminQuestion", action = "Index" } // Parameter defaults
    );

routes.MapRoute("AdminUsers", // Route name
                "admin/user/{action}/{id}", // URL with parameters
                new { controller = "AdminUser", action = "Index" } // Parameter defaults
    );

Alternatively you could route everything through the Admin controller, but it would quickly become very messy with your controller actions performing multiple roles.

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

With your AdminController action(s) looking like:

public virtual ActionResult Create(string type, int id)
{
    switch (type)
    {
        case 'question':
            // switch/case is code smell
            break;
        case 'user':
            // switch/case is code smell
            break;
        // etc
     }
}
转角预定愛 2024-09-19 17:18:19

将路由添加到 global.asax 相当简单。将更具体的路线放在更一般的路线之上。最典型的模式是控制器/操作/参数/参数...如果您需要更复杂的东西,您可能需要查看 MVC 区域。在上面的示例中,“mysite.com/admin/user/details”正在寻找一个名为“admin”的控制器和名为“user”的操作,之后的所有内容都是操作方法上的参数(假设典型的路由设置)

Adding routes to global.asax is fairly straight forward. Put the more specific routes above the more general routes. The most typical pattern is controller/action/parameter/parameter... If you need something more complex, you may want to look at MVC Areas.In you example above "mysite.com/admin/user/details" is looking for a controller named "admin" and an action named "user", with everything after that being parameter on the action method (assuming a typical route setup)

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