如何做高级ASP.NET MVC(3)路由(计算动作等)?

发布于 2024-11-08 13:16:54 字数 1990 浏览 0 评论 0原文

更新

考虑以下 8 个路由,其中​​ AdministrationareacontrollerEmployeesControllerIdEmployeeId

  1. Administration/Corporate/{controller}/{Id}/Phones/{PhoneId}/Delete
    • 操作 = 删除手机
  2. 管理/公司/{controller}/{Id}/Phones/{PhoneId}/Deactivate
    • 操作 = 停用手机
  3. 管理/公司/{controller}/{Id}/Phones/{PhoneId}/Activate
    • 操作 = 激活手机
  4. 管理/公司/{controller}/{Id}/Notes/{NoteId}/Delete
    • 操作 = 删除笔记
  5. 管理/公司/{controller}/{Id}/Files/{FileId}/Delete
    • 操作 = 删除文件
  6. 管理/公司/{controller}/{Id}/Addresses/{AddressId}/Delete
    • 操作 = 删除地址
  7. 管理/公司/{controller}/{Id}/Addresses/{AddressId}/Deactivate
    • 操作 = DeactivateAddress
  8. 管理/公司/{controller}/{Id}/Addresses/{AddressId}/Activate
    • 操作 = ActivateAddress

如何将其转换为:

Administration/Corporate/{controller}/{Id}/{object}/{ObjectId}/{action} 其中对象Phones|Notes|Files|Addresses|?actionDelete|Deactivate|Activate|? >?

  1. 我需要获取对象并将其单一化(我已经有代码)。
    • object = 电话 (电话)
  2. 采取操作并将其转换(重写?)为操作 + 对象(单数)。
    • 操作 = 删除+电话 (删除电话)

我可以做第二个#2之前的所有事情,问题是我怎样才能将操作转换为路由定义中的其他内容?

这一切有可能吗?如果能将我现在拥有的 8 条路由变成 1 条,那就太好了。这个例子只使用了我的 EmployeesController,我的 CustomersController 是这个大小的两倍,所以大约有 16 条路由可以变成1。这样可以节省大量空间和代码。

无论如何,如果可能的话,我期待着建议和想法。

UPDATED

Considering the following 8 routes where Administration is area, controller is EmployeesController and Id is EmployeeId:

  1. Administration/Corporate/{controller}/{Id}/Phones/{PhoneId}/Delete
    • action = DeletePhone
  2. Administration/Corporate/{controller}/{Id}/Phones/{PhoneId}/Deactivate
    • action = DeactivatePhone
  3. Administration/Corporate/{controller}/{Id}/Phones/{PhoneId}/Activate
    • action = ActivatePhone
  4. Administration/Corporate/{controller}/{Id}/Notes/{NoteId}/Delete
    • action = DeleteNote
  5. Administration/Corporate/{controller}/{Id}/Files/{FileId}/Delete
    • action = DeleteFile
  6. Administration/Corporate/{controller}/{Id}/Addresses/{AddressId}/Delete
    • action = DeleteAddress
  7. Administration/Corporate/{controller}/{Id}/Addresses/{AddressId}/Deactivate
    • action = DeactivateAddress
  8. Administration/Corporate/{controller}/{Id}/Addresses/{AddressId}/Activate
    • action = ActivateAddress

How can I transform it into:

Administration/Corporate/{controller}/{Id}/{object}/{ObjectId}/{action} where object is Phones|Notes|Files|Addresses|? and action is Delete|Deactivate|Activate|??

  1. I need to take object and singularize it (for which I already have the code).
    • object = Phones (Phone)
  2. Take action and transform (rewrite?) it into action + object (singularized).
    • action = Delete+Phone (DeletePhone)

I can do everything up to the second #2 where the issue is how can I transform the action into something else in the route definition?

Is any of this even possible at all? It would be nice to take the 8 routes I have now and turn them into 1. And this example only uses my EmployeesController, my CustomersController is twice that size so about 16 routes that can be turned into 1. It would save a lot of space and code.

Anyway, I'm looking forward to suggestions and ideas if this is possible.

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

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

发布评论

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

评论(2

黑色毁心梦 2024-11-15 13:16:54

您可以创建自定义路由处理程序。

public class CustomRoute : RouteBase
{
    //your custom code
}

ASP.NET MVC 子域路由

You can create custom route handler.

public class CustomRoute : RouteBase
{
    //your custom code
}

ASP.NET MVC Subdomain Routing

旧竹 2024-11-15 13:16:54

我会设置一个简单的数组和方法来处理这种事情。它并不完全优雅,但应该很容易理解:

public static void RegisterRoutes()
{
    var adminRoutes = new[]
                            {
                                new [] { "Phones", "PhoneId", "Delete", "DeletePhone" },
                                // Add the rest of your routes here.
                            };

    foreach ( var adminRoute in adminRoutes )
    {
        RegisterAdminRoute( adminRoute[0], adminRoute[1], adminRoute[2], adminRoute[3] );
    }
}

public static void RegisterAdminRoute( string area, string idName, string actionName, string action )
{
    RouteTable.Routes.MapRoute
        (
            area + actionName,
            String.Format( "Administration/Corporate/{{controller}}/{{Id}}/{0}/{{{1}}}/{2}",
                            area,
                            idName,
                            actionName ),
            new { action }
        );
}

I would setup a simple array and method to handle this kind of thing. Its not exactly elegant, but it should be easy to understand:

public static void RegisterRoutes()
{
    var adminRoutes = new[]
                            {
                                new [] { "Phones", "PhoneId", "Delete", "DeletePhone" },
                                // Add the rest of your routes here.
                            };

    foreach ( var adminRoute in adminRoutes )
    {
        RegisterAdminRoute( adminRoute[0], adminRoute[1], adminRoute[2], adminRoute[3] );
    }
}

public static void RegisterAdminRoute( string area, string idName, string actionName, string action )
{
    RouteTable.Routes.MapRoute
        (
            area + actionName,
            String.Format( "Administration/Corporate/{{controller}}/{{Id}}/{0}/{{{1}}}/{2}",
                            area,
                            idName,
                            actionName ),
            new { action }
        );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文