How do I get the current Url from within a FilterAttribute?

quakkels picture quakkels · May 25, 2012 · Viewed 28.1k times · Source

I am writing an Authorize filter attribute adn I'm having trouble figuring out how to get the current url as a string so I can pass it as a parameter to the LogOn action. The goal is that if a user successfully logs on, they will be redirected to the page they were originally trying to access.

public override void OnAuthorization(AuthorizeContext filterContext)
{
    base.OnAuthorization(filterContext);

    ... my auth code ...
    bool isAuth ;
    ... my auth code ...

    if(!isAuth)
    {
        filterContext.Result = new RedirectToRouteResult(
            new RouteValueDictionary { 
                { "Area", "" }, 
                { "Controller", "Account" }, 
                { "Action", "LogOn" },
                { "RedirectUrl", "/Url/String/For/Currnt/Request" } // how do I get this?
            }
        );
    }
}

How do I get the full string Url from the current request?

Answer

rboarman picture rboarman · May 25, 2012

Try:

var url = filterContext.HttpContext.Request.Url;