Enable referrer action through action filter?

Is there a way to inject a referrer action from an action filter? Suppose I have a view that comes from action X. In the death view, I call action Y and I want to redirect action X again. (There are several actions X that trigger action Y). I thought it would be nice if I had a call referrerAction parameter and an action filter that populated it with a previous action. Is it possible?

Thanks.

+3
source share
1 answer

Here is how I do it:

  public class ReturnPointAttribute : Attribute
  {
  }

  public class BaseController: Controller
  {
      private string returnPointUrl = null;
      protected override void OnActionExecuted(ActionExecutedContext filterContext)
      {
         base.OnActionExecuted(filterContext);
         if (filterContext.ActionDescriptor.IsDefined(typeof(ReturnPointAttribute), true))
            returnPointUrl = filterContext.HttpContext.Request.Url.ToString();
      }
      public ActionResult RedirectOrReturn<T>(Expression<Action<T>> action) where T : BaseController
      {
         return returnPointUrl.IsNullOrEmpty() 
            ? MyControllerExtensions.RedirectToAction(this, action) 
            : (ActionResult)Redirect(returnPointUrl);
      }
   }

Now you mark X's actions with [ReturnPoint] and call RedirectOrReturn () if you want to go back.

UrlReferrer, , . ReturnPoint , . [ReturnPoint ( "" )] RedirectOrReturn ( "" ).

, OnActionExecuted - , , Redirect, ReturnPoint, . [ReturnPoint (Automatic = true)] ..

+2

Source: https://habr.com/ru/post/1720061/


All Articles