har*_*don 5 .net c# asp.net-mvc-3
使用ASP.Net MVC,我发布了一个动作(然后我将数据库地址添加到数据库表等)
我需要重新定向到引荐来源网址,但还需要在引荐来源网址的查询字符串中添加内容.可以从许多地方调用此操作,因此我无法重定向到当前控制器中的操作.
如何重定向到引用者并向查询字符串添加内容(请记住引用者可能已经具有我需要保留的查询字符串值).
[HttpPost]
public ActionResult MyAction(MyModel model)
{
//Do stuff.
return new RedirectResult(Request.UrlReferrer.ToString()); // + query string value?
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
Jér*_*and 10
使用UriBuilder和HttpUtility.ParseQueryString:
[HttpPost]
public ActionResult MyAction(MyModel model)
{
//Do stuff.
UriBuilder uriBuilder = new UriBuilder(Request.UrlReferrer);
NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query);
query.Add("myparam", "something");
uriBuilder.Query = query.ToString();
return new RedirectResult(uriBuilder.Uri);
}
Run Code Online (Sandbox Code Playgroud)