使用ID或操作名称的ASP.NET MVC路由

时间:2012-08-22 09:47:49

标签: c# asp.net-mvc asp.net-mvc-routing

我有一个ASP.Net应用程序,其中有一个名为“Customers”的区域。该区域有一个名称相同的控制器,只有一个名为Index的方法。

我定义了以下路线:

context.MapRoute(null,
    "Customers/{controller}/{action}",
    new { controller = "customers", action = "Index" }
);

这允许我导航到使用以下URL导航到我的Customers控制器上的索引方法。

  

MYDOMAIN /客户

在我的客户区域,我还有另一个叫做产品的控制器。这有许多方法可以让我使用产品实体(目前主要由Visual Studio自动生成)。

根据我目前的路线,我可以使用以下网址导航到产品控制器:

  

MyDomain / Customers / Products(显示产品的索引页面)   控制器)MyDomain / Customers / Products / Create(显示要添加的页面)   新产品)。 MyDomain / Customers / Products / Details?id = 1234(显示   ID为1234的产品

现在我想要做的是导航到详细信息页面,其中包含更友好的用户界面,例如:

  

MYDOMAIN /客户/产品/ 1234

我已定义了一条如下所示的新路线:

context.MapRoute(null,
    "Customers/Products/{id}",
    new { controller = "Products", action = "Details" }
    );

在我演示的第一条路线之前,路线被定义为。 这允许我根据需要导航到产品页面,但是我无法再导航到我的产品控制器上的其他方法。

E.G。以下网址

  

MYDOMAIN /客户/产品/创建

给我以下错误:

  

参数字典包含参数'id'的空条目   方法'System.Web.Mvc.ViewResult'的非可空类型'System.Int32'   详细信息(的Int32)'

如果我更改路线的顺序,那么我可以导航到我的产品控制器上的所有方法,但我的详细信息URL将恢复为具有查询参数的旧格式。

如果我将路线更新为:

context.MapRoute(null,
    "Customers/Products/{id}",
    new { controller = "Products", action = "Details", id = UrlParameter.Optional }
    );

然后我仍然遇到同样的问题。

有谁能告诉我如何构建我的路线以获得我想要的结果?总结:

  1. 如果我导航到“客户”区域,我希望我的网址看起来像“MyDomain / Customers”
  2. 如果我导航到产品详情页面,我希望我的网址看起来像“MyDomain / Customers / Products / 1234”。
  3. 如果我导航到任何其他产品页面,我希望我的网址看起来像'MyDomain / Customers / Products / Create'

4 个答案:

答案 0 :(得分:4)

如果ID总是一个int,那么你可以像这样在路径中添加一个约束:

context.MapRoute(null,
                 "Customers/Products/{id}",
                 new {controller = "Products", action = "Details", id = UrlParameter.Optional},
                 new {id = @"\d+"} // Constraint to only allow numbers
                );

答案 1 :(得分:1)

尝试这样的事情:

public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
        "Products_Details",
        "Customers/Products/{id}",
        new { controller="Products", action="Details" },
        new { id = @"\d+" }
    );

    context.MapRoute(
        "Customers_default",
        "Customers/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}

更新:在路线上添加了约束。

说明:第一个路由映射到您的详细信息页面,并强制用户提供具有数字格式的ID(第一个MapRoute中的最后一个参数)。如果id没有格式\d+,则它将与路由不匹配,将使用默认值。这将为您提供以下路线:

Customers\Products (index)
Customers\Products\1234 (details of 1234)
Customers\Products\Create (create page for products)
Customers\Products\Edit\1234 (edit page for 1234)

答案 2 :(得分:0)

如果您想保留现有路线,请尝试执行以下操作:

context.MapRoute(null,
     "Customers/Products/{id}", // id is optional, so Customers/Products might reasonably return all products
     new { controller = "Products", action = "Details", id = UrlParameter.Optional },
     new {id = @"\d+"} // must be numeric if present
);

context.MapRoute(null,
     "Customers/Products/{action}/{id}", // Id is optional, so this needs an action.
     new { controller = "Products", action = "Details", id = UrlParameter.Optional }
     // maybe the id is not numeric for all actions? I don't know so I won't constrain it.
);

context.MapRoute(null,
     "Customers/{controller}/{action}", // Default Customers route
     new { controller = "customers", action = "Index" }
);

前两个处理的网址为/Customers/Products/...,其中包含一个或两个其他部分,最后一个处理以/Customers/开头的其他内容

答案 3 :(得分:0)

试试这个

    context.MapRoute(
                    "Customers_Products",
                    "Customers/Products/{productId}",
                    new { controller = "Products", action = "Details", productId= UrlParameter.Optional }
                );

    context.MapRoute(null,
                    "Customers/{controller}/{action}",
                     new { controller = "customers", action = "Index" }
);