Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect to action method from base controller

I have initialized method in my base controller class which is called whenever any action method is executed. On every action method, I want to check my session and if it is null it should redirect to the login page.

 public class BaseController : Controller
{
    protected IDataRepository _appData = new DataRepository();

    protected override void Initialize(RequestContext requestContext)
    {
        base.Initialize(requestContext);

        if (SessionFactory.CurrentAdminUser == null)
        {
            RedirectToLogin();
        }
    }
 }
public ActionResult RedirectToLogin()
    {
        return RedirectToAction("AdminLogin", "Admin");
    }

it's calling this method but not redirecting to admin login method and keeps execution on and call method which is in flow so error will come.

In short i want to check whenever my application session gets null its should rediect to login page and its not convenient to check on all methods.please suggest me some good way.

like image 517
DharaPPatel Avatar asked Jun 24 '13 12:06

DharaPPatel


People also ask

How do I redirect a controller to another action?

Use this: return RedirectToAction("LogIn", "Account", new { area = "" }); This will redirect to the LogIn action in the Account controller in the "global" area.

How do you redirect an action method?

Redirects to the specified action using the action name. Redirects to the specified action using the action name and route values. Redirects to the specified action using the action name and controller name. Redirects to the specified action using the action name and route dictionary.

Which method is used to redirect to another action method?

To redirect the user to another action method from the controller action method, we can use RedirectToAction method. Above action method will simply redirect the user to Create action method.

How do I redirect OnActionExecuting in base controller?

4 Answers. Show activity on this post. public override void OnActionExecuting(ActionExecutingContext filterContext) { ... if (needToRedirect) { ... filterContext. Result = new RedirectResult(url); return; } ... }


Video Answer


1 Answers

You're calling RedirectToLogin() which in turn simply returns a RedirectToActionResult that is not being used and does not affect the flow of the action.

Try this instead:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting();

    if (SessionFactory.CurrentAdminUser == null)
        filterContext.Result = new RedirectResult(Url.Action("AdminLogin", "Admin"));
}

Alternatively, if you insist on overriding Initialize:

protected override void Initialize(RequestContext requestContext)
{
    base.Initialize(requestContext);

    if (SessionFactory.CurrentAdminUser == null)
    {
        requestContext.HttpContext.Response.Clear();
        requestContext.HttpContext.Response.Redirect(Url.Action("AdminLogin", "Admin"));
        requestContext.HttpContext.Response.End();
    }
}

Also, check the [Authorize] filter, it may better suit your needs. See here.

like image 94
haim770 Avatar answered Sep 20 '22 12:09

haim770