id不起作用的MVC默认路由?

时间:2013-11-30 15:19:14

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

我想链接到一个实际指定action参数的控制器。我很确定这是可能的。从这里得到了这个概念:
http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs

以下是两个链接:

https://localhost/mvcapplication1/customer/order/index/6463 (WORKS)
https://localhost/mvcapplication1/customer/order/6463 (WANT THIS)

我为此控制器指定了一个特殊的路径(Customer_orders)。

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Customer_orders",
        "Customer/Order/{orderId}",
        new { controller = "MvcApplication1.Areas.Customer.Controllers.Order", action = "Index" },
        new[] { "MvcApplication1.Areas.Customer.Controllers" }  // only map routes to controllers in the specified namespace
     );

    context.MapRoute(
        "Customer_default",
        "Customer/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new[] { "MvcApplication1.Areas.Customer.Controllers" }  // only map routes to controllers in the specified namespace
        );
}

这是我的命名空间控制器:

namespace MvcApplication1.Areas.Customer.Controllers
{
    [Authorize]
    public partial class OrderController : SupertextController
    {

我还启用了路由调试器,根据我的路由没问题。那么为什么我会得到“资源无法找到。”?特别是因为如果我在URL中添加“index”就可以了。

enter image description here

1 个答案:

答案 0 :(得分:1)

在您的路由中,controller令牌的默认值应该是您的控制器名称,没有任何名称空间且没有"Controller"后缀:

所以改变您的路线:

context.MapRoute(
    "Customer_orders",
    "Customer/Order/{orderId}",
        new { controller = "Order", action = "Index" },
        new[] { "MvcApplication1.Areas.Customer.Controllers" }  
);