破折号路线值 [英] Route value with dashes
问题描述
我有这样的路线:
routes.MapRoute(
新闻,
新闻/ {ID} - {}别名,
新{控制器=新闻,行动=显示},
新
{
ID = @^ [0-9] + $
},
命名空间:新[] {Site.Controllers}
);
这条航线的网址是这样工作的:
的http://本地主机:54010 /新闻/ 6-新闻
但不工作的网址是这样的:
的http://本地主机:54010 /新闻/ 6-不错新闻
如何在我的路线值别名使用破折号?
EDITED
路线是这样的:
新闻/ {ID} _ {}别名
适用于这两个网址:
的http://本地主机:54010 /新闻/ 6_news
HTTP://本地主机:54010 /新闻/ 6_nice新闻
问题是与你的格局:新闻/ {ID} - {化名}
,因为定线制是解析模式贪婪。
于是网址的http://本地主机:54010 /新闻/ 6-新闻
生成以下标记:
ID = 6,别名=新闻
但的http://本地主机:54010 /新闻/ 6-漂亮的新闻
生成以下标记:
ID = 6-不错,别名=新闻
以及 ID = 6-不错
标记将失败你的路由约束实现 @^ [0-9] + $。
所以你会得到404
现在有那么你有以下几种选择方式来配置MVC的这种行为:
- 使用的东西比其他破折号。正如你指出结合破折号和连字符的作品。
- 以弗莱姆方法,并分析你的控制器动作里面的ID和别名内
- 您可以创建自定义
路线
这将重新解析。如转化ID = 6-不错,别名为新闻
到ID = 6,别名为新闻不错
我会告诉你一个原始(没有任何错误处理或良好的编码习惯!)执行选项3,让你开始。
所以,你需要从路线
继承:
公共类MyRoute:路线
{
公共MyRoute(字符串URL,
RouteValueDictionary默认值,
RouteValueDictionary的限制,
RouteValueDictionary dataTokens)
:基地(URL,默认,约束,dataTokens,新MvcRouteHandler())
{
} 保护覆盖布尔ProcessConstraint(HttpContextBase HttpContext的,
对象约束,字符串参数名称,RouteValueDictionary价值,
RouteDirection routeDirection)
{
VAR部分=((字符串)值[ID])斯普利特(' - ')。
如果(parts.Length→1)
{
值[ID] =零件[0];
值[别名] = //建立别名部分
的string.join( - ,parts.Skip(1))+ - +值[别名];
}
VAR processConstraint = base.ProcessConstraint(HttpContext的,约束
参数名称,价值,routeDirection);
返回processConstraint;
}
}
然后你只需要注册您的路线:
routes.Add(新闻报,
新MyRoute(新闻/ {ID} - {}别名,
新RouteValueDictionary(新{控制器=新闻,行动=显示})
新RouteValueDictionary(新
{
ID = @^ [0-9] + $
}),
新RouteValueDictionary()));
I have this route:
routes.MapRoute(
"News",
"News/{id}-{alias}",
new { controller = "News", action = "Show" },
new
{
id = @"^[0-9]+$"
},
namespaces: new[] { "Site.Controllers" }
);
This route working for url's like this:
http://localhost:54010/News/6-news
But not working for url's like this:
http://localhost:54010/News/6-nice-news
How use dashes in my route value "alias"?
EDITED
Route like this:
"News/{id}_{alias}"
works for both url's:
http://localhost:54010/News/6_news
http://localhost:54010/News/6_nice-news
The problem is with your pattern: News/{id}-{alias}
because the Routeing is parsing the patterns greedily.
So the url http://localhost:54010/News/6-news
generates the following tokens:
id = 6, alias = news
But the http://localhost:54010/News/6-nice-news
generates the following tokens:
id = 6-nice, alias = news
And the id = 6-nice
token will fail your routing contraint @"^[0-9]+$".
so you will get 404.
There is now way to configure this behavior of MVC so you have the following options:
- Use something else than dashes. As you noted combining dashes and hyphens works.
- Take flem approach and parse inside the id and alias inside your controller action
- You can create a custom
Route
which will take of the re-parsing. E.g transformingid = 6-nice, alias = news
toid = 6, alias = news-nice
I will show you a raw (without any error handling or good coding practices!) implementation of the option 3 to get you started.
So you need to inherit from Route
:
public class MyRoute : Route
{
public MyRoute(string url,
RouteValueDictionary defaults,
RouteValueDictionary constraints,
RouteValueDictionary dataTokens)
: base(url, defaults, constraints, dataTokens, new MvcRouteHandler())
{
}
protected override bool ProcessConstraint(HttpContextBase httpContext,
object constraint, string parameterName, RouteValueDictionary values,
RouteDirection routeDirection)
{
var parts = ((string) values["id"]).Split('-');
if (parts.Length > 1)
{
values["id"] = parts[0];
values["alias"] = // build up the alias part
string.Join("-", parts.Skip(1)) + "-" + values["alias"];
}
var processConstraint = base.ProcessConstraint(httpContext, constraint,
parameterName, values, routeDirection);
return processConstraint;
}
}
Then you just need to register your route:
routes.Add("News",
new MyRoute("News/{id}-{alias}",
new RouteValueDictionary(new {controller = "News", action = "Show"}),
new RouteValueDictionary(new
{
id = @"^[0-9]+$"
}),
new RouteValueDictionary()));
这篇关于破折号路线值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!