创建具有asp.net MVC和RouteUrl网址 [英] Creating urls with asp.net MVC and RouteUrl
本文介绍了创建具有asp.net MVC和RouteUrl网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得当前的URL和一个额外的参数附加到URL(例如?ID = 1)
I would like to get the current URL and append an additional parameter to the url (for example ?id=1)
我已经定义了一个路线:
I have defined a route:
routes.MapRoute(
"GigDayListings", // Route name
"gig/list/{year}/{month}/{day}", // URL with parameters
new { controller = "Gig", action = "List" } // Parameter defaults
);
In my view I have a helper that executes the following code:
// Add page index
_helper.ViewContext.RouteData.Values["id"] = 1;
// Return link
var urlHelper = new UrlHelper(_helper.ViewContext);
return urlHelper.RouteUrl( _helper.ViewContext.RouteData.Values);
然而,这并不工作。
However this doesnt work.
如果我原来的URL是:
演出/列表/ 2008/11月1日
If my original URL was : gig/list/2008/11/01
我得到
演出/列表/年= 2008&放大器;一个月= 11和天= 01和ID = 1
gig/list/?year=2008&month=11&day=01&id=1
我想的网址是:
控制器/动作/ 2008/11/012 ID = 1
I would like the url to be: controller/action/2008/11/01?id=1
我在做什么错了?
推荐答案
的规则的顺序,使培训就业处。尝试插入此规则为第一位。
The order of the rules makes sence. Try to insert this rule as first.
此外,如果不要忘记需要定义约束 - 这将带来更好的规则匹配:
Also dont forget to define constraints if needed - it will results in better rule matching:
routes.MapRoute(
"GigDayListings", // Route name
"gig/list/{year}/{month}/{day}", // URL with parameters
new { controller = "Gig", action = "List" }, // Parameter defaults
new
{
year = @"^[0-9]+$",
month = @"^[0-9]+$",
day = @"^[0-9]+$"
} // Constraints
);
这篇关于创建具有asp.net MVC和RouteUrl网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文