自定義操作的結果可以做的工作:
public class MyRedirectResult : ActionResult
{
private readonly string _actionName;
private readonly string _controllerName;
private readonly RouteValueDictionary _routeValues;
public MyRedirectResult(string actionName, string controllerName, RouteValueDictionary routeValues)
{
_actionName = actionName;
_controllerName = controllerName;
_routeValues = routeValues;
}
public override void ExecuteResult(ControllerContext context)
{
var requestUrl = context.HttpContext.Request.Url;
var url = UrlHelper.GenerateUrl(
"",
_actionName,
_controllerName,
requestUrl.Scheme,
requestUrl.Host,
null,
_routeValues,
RouteTable.Routes,
context.RequestContext,
false
);
var builder = new UriBuilder(url);
builder.Query = HttpUtility.ParseQueryString(requestUrl.Query).ToString();
context.HttpContext.Response.Redirect(builder.ToString(), false);
}
}
然後:
public ActionResult MyRedirectAction()
{
return new MyRedirectResult("MyAction", "Home", null);
}