Redirect from Action Filter Attribute

Redirect From Action Filter Attribute

Set filterContext.Result

With the route name:

filterContext.Result = new RedirectToRouteResult("SystemLogin", routeValues);

You can also do something like:

filterContext.Result = new ViewResult
{
ViewName = SharedViews.SessionLost,
ViewData = filterContext.Controller.ViewData
};

If you want to use RedirectToAction:

You could make a public RedirectToAction method on your controller (preferably on its base controller) that simply calls the protected RedirectToAction from System.Web.Mvc.Controller. Adding this method allows for a public call to your RedirectToAction from the filter.

public new RedirectToRouteResult RedirectToAction(string action, string controller)
{
return base.RedirectToAction(action, controller);
}

Then your filter would look something like:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var controller = (SomeControllerBase) filterContext.Controller;
filterContext.Result = controller.RedirectToAction("index", "home");
}

I'm trying to redirect to another action from my custom actionfilter attribute but setting the filtercontext.result does nothing

This seems to work for me... (I just left of your authorization logic)

public class MyActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
context.Result = new RedirectToActionResult("Index", "Home", null);
}
}

Applied on a controller...

public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}

[MyActionFilter]
public IActionResult About()
{
return View();
}
}

The About action now always redirects to the Index action.

Is it possible to redirect to another action using a custom action filter?

Yes, it is. Simply set the Result property of the ActionExecutingContext instance to your RedirectToActionResult. It should look something like the following:

 public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var controller = filterContext.Controller as ControllerBase;
if (context.Companies == null && controller != null)
{
filterContext.Result = controller.RedirectToAction(
actionName: "Msg",
controllerName: "Account",
new { message = "You are not allowed to register, since the company data not exist !!!" }
);
}
base.OnActionExecuting(filterContext);
}

Redirect in a .NET API action filter

Actually it is very easy. You just create HttpResponseMessage object.

public class RedirectAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
var response = actionContext.Request.CreateResponse(HttpStatusCode.Redirect);
response.Headers.Location = new Uri("https://www.stackoverflow.com");
actionContext.Response = response;
}
}

How to get filter to redirect to another action?

RedirectToAction is just a helper method to construct a RedirectToRouteResult(), so what you do is simply create a new RedirectToRouteResult() passing along a RouteValueDictionary() with values for your action.

Complete sample based on code from @Domenic in the comment below:

public class IsGuestAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Ctx.User.IsGuest)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "Home" },
{ "action", "Index" }
});
}
}
}

Redirect in custom action filter

For WebApi you can use HttpActionContext.Response Property:

public override void OnActionExecuting(HttpActionContext actionContext)
{
var response = actionContext.Request.CreateResponse(HttpStatusCode.Redirect);
response.Headers.Location = new Uri("https://www.example.com");
actionContext.Response = response;
}


Related Topics



Leave a reply



Submit