[英]Add a query string(s) at end of url using routes in ASP.NET MVC3
I want to add query string(s) to the end of an url using routes. 我想使用路由将查询字符串添加到URL的末尾。 How do I do this in Global.asax ? 我如何在Global.asax中执行此操作?
routes.MapRoute(
"Detail",
"{controller}/{action}/{id}/{name}",
new
{
action = "Detail",
name = UrlParameter.Optional,
// it is possible to add here query string(s) ?
},
new[] { "MyProject.Controllers" }
);
For example, the actual url contains: 例如,实际网址包含:
www.mysite.com/MyController/Detail/4/MyValue
but I want to generate something like: 但我想生成类似的东西:
www.mysite.com/MyController/Detail/4/MyValue?ref=test&other=something
When you generate action URLs, you can pass in additional route values, like this: 生成操作URL时,可以传入其他路由值,如下所示:
@Url.Action("Detail", "MyController",
new { id = 4, @ref = "test", other = "something" })
The ref
and other
parameters, which are not defined in the route template of the Detail
route, will be appended as query string parameters. ref
和other
参数(未在Detail
路由的路由模板中定义)将作为查询字符串参数附加。
MVC.NET automatically binds parameters from query string to your controller action input. MVC.NET自动将查询字符串中的参数绑定到控制器操作输入。
Example of your controller action would be like: 控制器操作的示例如下:
public ActionResult Detail(string id, string name, string test, string other){
}
Also, you can generate the link using a code like the following: 此外,您可以使用如下代码生成链接:
UrlHelper helper = new UrlHelper(HttpContext.Current.Request.RequestContext);
var url = helper.RouteUrl("Detail", new {id="testid", name="testname", ref="testref", other="testother"});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.