Matthew Verstraete
Matthew Verstraete

Reputation: 6781

Figuring out what link a user clicked in ASP.Net MVC

I have 4 links a user can click with each one's destination being different but I have a Filter applied to each ActionResult in the Controller that runs a few checks and redirects them to a page with a required form. I am trying to figure out a way to capture what link was clicked and store it in Session so when the user lands on the page the filter redirects to I can create a link back the page s/he was intending to go to. I tried Request.UrlReferrer but that simple catches the URL for the page the user was on when s/he clicked the link.

Controller:

[NetGalFilter]
public ActionResult Transactions()
{
    return View();
}

Filter:

public class NetGalFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var Controller = filterContext.Controller as Sprague.RTS.WebUI.Controllers.BaseController;

        if (HttpContext.Current.Session["HasConfirmed"] == null && Controller.TerminalUserData.IsNetGallonTerminal)
        {
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "DataEntry", action = "ConversionFactors" }));
        }

        base.OnActionExecuting(filterContext);
    }
}

Upvotes: 1

Views: 1270

Answers (1)

peter
peter

Reputation: 15089

You can use the HttpContext in filterContext. There you have the Request property which is of type HttpRequestBase and has the RawUrl to get the exact URL from the browser as well as other properties to get the parsed Action and Controllers.

Upvotes: 1

Related Questions