I'm trying to execute a Redirect from a method attribute. It seems to work:
public class MyAttribute: ActionFilterAttribute {
[..]
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
[..]
filterContext.HttpContext.Response.Redirect(urlToRedirectTo, true);
[..]
The only problem is that the redirect is executed after the end of the method it's attached to, while i'd like the redirect to prevent the execution of the method.
Any help? Thanks
RedirectToAction(String, String, RouteValueDictionary) Redirects to the specified action using the action name, controller name, and route values.
You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is: return RedirectToAction("Index", model); Then in your Index method, return the view you want.
You can prevent execution of the action method by assigning an ActionResult to filterContext.Result. For example:
filterContext.Result = new RedirectResult(urlToRedirectTo);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With