user6386826
user6386826

Reputation: 151

How to exclude some action methods from returnUrl

I'm using returnUrl to return to the last requested page when logging in after being logged out due to inactivity. In itself this works fine, unless the user does not requests a page, but some other action. What I would like is that in case the first action after SessionTimeOut is for example "addItem", the returnUrl is set as "controller/index" instead of "controller/addItem".

After SessionTimeOut the application does not automatically redirect to the login page. The redirect happens as soon as the user makes a new request after SessionTimeOut (as requested by client). So when the user clicks a menu item, the returnUrl is set to the requested page. Great so far. The issue is though when the user click for example the addItem button that the returnUrl is set as the action method linked to that button. I don't understand how I can exclude this kind of methods from returnUrl.

This is a very scaled down example of the application, to give an idea of how it hangs together.

MainController:

public ActionResult MainPage(){
   return View();
}

public ActionResult addItem(){
   --- Do Something ---
}

public ActionResult SecondPage(){
   return View();
}

Login method:

public ActionResult Login([Binde(Include = "UserName, Password")]LoginModel model, string returnUrl)
{
   if(Modelstate.IsValid)
   {
      --- Do Validation ---
      return Redirect(returnUrl);
   }
}

In case the user is on the MainPage and makes after SessionTimeOut the request for "SecondPage", the returnUrl should be and is "controller/SecondPage".(OK)

In case the suer is on the MainPage and makes after SessionTimeOut the request for "addItem", returnUrl should be "controller/MainPage" and not "controller/addItem"

Upvotes: 0

Views: 296

Answers (1)

A. Nadjar
A. Nadjar

Reputation: 2543

One quick solution is to put Action Filters into effect. As you might know, an action filter is an attribute that you can apply to a controller action -- or an entire controller -- that modifies the way in which the action is executed. More details, see documentation here

In this case, you need to override OnActionExecuting method, is called before a controller action is executed, so that prior to execution, you can decide to redirect or not based on the return value.

I provide you with a simple example :

    public class myCheckUrlActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            List<string> avoidList = new List<string> { "addItem", "removeItem"};

            foreach(string i in avoidList)
            {
                if(filterContext.HttpContext.Request.Url.Query.Contains(i))
                {
                    filterContext.Result = new RedirectResult("/Home/MainPage") ;
                    break;
                }
            }

        }
    }

Now, Let's apply this filter to all action methods inside your Home Controller, though you may apply it individually to only certain action methods.

[myCheckUrlActionFilter]
public class HomeController : Controller
{ .... }

Access to HttpContext inside the body of the filter, enables to extract session, and even current controller and action name. Therefore, you can check session state here in addition returnUrl existence to further decide on redirect. So, customize it as you need.

Hope this helps.

Upvotes: 1

Related Questions