ASP.NET MVC 2 RC2路由 - 使用ActionLink的指到一个更高的级别时如何清除低级别的价值观? [英] ASP.NET MVC 2 RC2 Routing - How to clear low-level values when using ActionLink to refer to a higher level?

查看:101
本文介绍了ASP.NET MVC 2 RC2路由 - 使用ActionLink的指到一个更高的级别时如何清除低级别的价值观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[注:我使用ASP.NET MVC2 RC2]

我有这样的网址:

/客户/ 123 /命令/ 456 /项目/指数

/customers/123/orders/456/items/index

/客户/ 123 /命令/ 456 /项/ 789 /编辑

/customers/123/orders/456/items/789/edit

我的路由表中列出了最具体的路由第一,所以我有:

My routing table lists the most-specific routes first, so I've got:

// customers/123/orders/456/items/789/edit
routes.MapRoute(
    "item", // Route name
    "customers/{customerId}/orders/{orderId}/items/{itemId}/{action}", // URL with parameters
    new { controller = "Items", action = "Details" }, // Parameter defaults
    new { customerId = @"\d+", orderId = @"\d+", itemId = @"\d+" } // Constraints
);

// customers/123/orders/456/items/index
routes.MapRoute(
    "items", // Route name
    "customers/{customerId}/orders/{orderId}/items/{action}", // URL with parameters
    new { controller = "Items", action = "Index" }, // Parameter defaults
    new { customerId = @"\d+", orderId = @"\d+" } // Constraints
);

当我在该项目的编辑页的时候,我想恢复到索引页面的链接。所以,我用:

When I'm in the item "Edit" page, I want a link back up to the "Index" page. So, I use:

ActionLink("Back to Index", "index")

但是,因为有一个环境项目ID,这导致在URL:

However, because there's an ambient item ID, this results in the URL:

/客户/ 123 /命令/ 456 /项/ 789 /指数

/customers/123/orders/456/items/789/index

...而我希望它忘不了的项目ID,只需使用:

...whereas I want it to "forget" the item ID and just use:

/客户/ 123 /命令/ 456 /项目/指数

/customers/123/orders/456/items/index

我试过重写项目ID如下:

I've tried overriding the item ID like so:

ActionLink("Back to Index", "index", new { itemId=string.empty })

...但是,这完全不是那么回事。是什么,让我的是:

...but that doesn't quite work. What that gives me is:

/客户/ 123 /命令/ 456 /项目?的itemId = 789

/customers/123/orders/456/items?itemId=789

我怎样才能说服 ActionLink的忘记的项目ID?

How can I persuade ActionLink to "forget" the item ID?

修改:固定的问题 - 我指的是秩序,我的意思是项

EDIT: fixed question - I was referring to "order" where I meant "item".

修改:添加了为什么的itemId =的String.Empty不起作用细节

EDIT: added detail of why itemId=string.empty doesn't work.

推荐答案

在您的约束,你可以删除你不需要任何routeValues​​。

In your constraint you can delete any routeValues you do not need.

values.Remove("itemId");

然后创建一个从IRouteConstraint继承的类。

Then create a class that inherits from IRouteConstraint.

然后在比赛方法清除路线值并返回true(表示路由相匹配)。

Then in the Match method clear your route value and return true (indicates the route matches).

public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{

    values.Remove("itemId");    
    return true;
}

再在Global.asax.cs中的变化

then in Global.asax.cs change

new { customerId = @"\d+", orderId = @"\d+" }

new { customerId = @"\d+", orderId = @"\d+", custom=new MyNewConstraint() }

这篇关于ASP.NET MVC 2 RC2路由 - 使用ActionLink的指到一个更高的级别时如何清除低级别的价值观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆