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
Executing Ssis 2012 Package That Has Script Components from External Application
How to Get the Last Day of a Month
Service Hangs Up at Waitforexit After Calling Batch File
Newtonsoft.JSON Cannot Convert Model with Typeconverter Attribute
Web API Put Request Generates an Http 405 Method Not Allowed Error
Listen to Changes of Dependency Property
How Does Inheritance Work for Attributes
Deserializing into a List Without a Container Element in Xml
Why Does the Order of Alternatives Matter in Regex
Merge Two (Or More) Lists into One, in C# .Net
Why Does C# Xmldocument.Loadxml(String) Fail When an Xml Header Is Included
Setting the User-Agent Header for a Webclient Request
How Does JSON Deserialization in C# Work