ASP.NET 路由传递字符串值
我试图在基于 ASP.NET MVC 的网站上创建一个页面,其中一个页面允许按用户名而不是 ID 进行选择。我本以为路线需要类似于:
routes.MapRoute(
"CustomerView", "Customer/Details/{username}",
new { username = "" }
);
routes.MapRoute(
"Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "0" }
);
但每当我使用 HTML.Actionlink 时,我都会得到 http ://mysite.com/Customer/Details?username=valuehere 。我考虑过像这样的通用路线:
routes.MapRoute(
"CustomerView", "Customer/Details/{username}",
new { controller="Customer", action = "Details", username = "" }
);
但我想当它错误地应用这两者中的哪一条路线时,它会导致更多问题。
I'm trying to have a page on an ASP.NET MVC based web site where one page allows selecting by username instead of ID. I would have thought the route needs to be something like:
routes.MapRoute(
"CustomerView", "Customer/Details/{username}",
new { username = "" }
);
routes.MapRoute(
"Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "0" }
);
but whenever I use HTML.Actionlink I get http://mysite.com/Customer/Details?username=valuehere . I considered a generic route like:
routes.MapRoute(
"CustomerView", "Customer/Details/{username}",
new { controller="Customer", action = "Details", username = "" }
);
but I imagine it would cause more problems when it mistakes which route should be applied of the two.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的客户控制器的详细信息方法是否有“用户名”参数,而不是 id 参数?
如果参数不匹配,那么它们将作为查询字符串变量附加。
Does the Details method of your Customer controller have a "username" parameter, instead of an id parameter?
If the parameters don't match then they get appended as querystring variables.
我不确定我是否完全理解这个问题...您是说您想要:
一条路由
{controller}/{action}/{username}
处理第三个令牌所在的 URL一个字符串,匹配有字符串“username”参数的操作,以及另一个路由
{controller}/{action}/{id}
处理第三个令牌是整数的 URL,匹配操作其中有一个整数“id”参数?如果是这样,请检查 MapRoute 重载,它需要 < a href="http://blogs.microsoft.co.il/blogs/bursteg/archive/2009/01/11/asp-net-mvc-route-constraints.aspx" rel="nofollow noreferrer">指定第四个参数路线限制。它应该让你做这样的事情:
这个(未经测试的)约束应该导致 {username} 路由匹配第三个标记至少包含一个非数字字符的任何内容。
当然,如果用户名完全由数字组成是合法的,那么这可能不适合您。在这种情况下,您可能需要为接受用户名而不是 ID 的每个操作创建专门的路由。
I'm not sure I fully understand the question... are you saying you want:
one route
{controller}/{action}/{username}
that handles URLs where the 3rd token is a string, matching actions where there is a string "username" argument, andanother route
{controller}/{action}/{id}
that handles URLs where the 3rd token is an integer, matching actions where there is an integer "id" argument?If so, check the out the overload for MapRoute that takes a 4th argument specifying route constraints. It should let you do something like this:
This (untested) constraint should cause the {username} route to match anything where the 3rd token contains at least one non-digit character.
Of course, if it's legal for usernames to consist entirely of digits then this may not work for you. In that case, you may need to create specialized routes for each action that accepts a username instead of an ID.
这是可行的:
但在上面的问题中,我在第二个示例中犯的错误是我的意思:
这只是意味着我必须为传递字符串值的每个实例专门声明一条路由。
This works:
but in the above question the mistake I made in the second example was I meant:
It just means I'll have to specifically declare a route for each instance where I pass a string value.