Here's an improved version of @stusherwin's answer with support for ValidateAntiForgeryToken and MVC realms .
Your POST actions probably have a ValidateAntiForgeryToken attribute to prevent CSRF attacks. In this case, the ValidateAntiForgeryToken filter will always be executed first, since it is an authorization filter. Therefore, we need to make the HttpPostOrRedirectAttribute an authorization filter. Otherwise, an exception will be thrown that the anti-fake token was not found.
Another improvement is the addition of MVC redirection
public class HttpPostOrRedirectAttribute : FilterAttribute, IAuthorizationFilter { public string RedirectAction { get; set; } public string RedirectController { get; set; } public string RedirectArea { get; set; } public string[] ParametersToPassWithRedirect { get; set; } public HttpPostOrRedirectAttribute(string redirectAction) : this(redirectAction, null, new string[] { }) { } public HttpPostOrRedirectAttribute(string redirectAction, string[] parametersToPassWithRedirect) : this(redirectAction, null, parametersToPassWithRedirect) { } public HttpPostOrRedirectAttribute(string redirectAction, string redirectController, string[] parametersToPassWithRedirect) { RedirectAction = redirectAction; RedirectController = redirectController; ParametersToPassWithRedirect = parametersToPassWithRedirect; } public HttpPostOrRedirectAttribute(string redirectAction, string redirectController, string redirectArea) { RedirectAction = redirectAction; RedirectController = redirectController; RedirectArea = redirectArea; } public HttpPostOrRedirectAttribute(string redirectAction, string redirectController, string redirectArea, string[] parametersToPassWithRedirect) { RedirectAction = redirectAction; RedirectController = redirectController; RedirectArea = redirectArea; ParametersToPassWithRedirect = parametersToPassWithRedirect; } public void OnAuthorization(AuthorizationContext filterContext) { if (filterContext.HttpContext.Request.HttpMethod == "POST") return; string redirectUrl = GetRedirectUrl(filterContext.RequestContext); filterContext.Controller.TempData["Warning"] = "Your action could not be completed as your" + " session had expired. Please try again."; filterContext.Result = new RedirectResult(redirectUrl); } public string GetRedirectUrl(RequestContext context) { RouteValueDictionary routeValues = new RouteValueDictionary(); foreach (string parameter in ParametersToPassWithRedirect) { if (context.RouteData.Values.ContainsKey(parameter)) routeValues.Add(parameter, context.RouteData.Values[parameter]); } if (RedirectArea.IsNotEmpty()) routeValues.Add("area", RedirectArea); string controller = RedirectController ?? context.RouteData.Values["controller"].ToString(); UrlHelper urlHelper = new UrlHelper(context); return urlHelper.Action(RedirectAction, controller, routeValues); } }
Here is an example of using it together with the ValidateAntiForgeryToken attribute and redirecting to the admin area:
[HttpPostOrRedirect("Display", "User", "Admin", new[] { "id", "param1"}, Order = 0)] [ValidateAntiForgeryToken(Order = 1)] public ActionResult Delete(User user, int param1, string param2) { ... }