0

I'm using a filter that checks the user's browser/version upon arrival to the site. If they use an unsupported browser, I save the URL they intended to reach into a ViewData called "RequestedURL" and redirect to a view telling them their browser is old. This view gives the user the ability to proceed by clicking a link. The URL of this link is being populated by the ViewData attribute of "RequestedUrl" that was set in the filter.

Filter:

/// <summary>
/// If the user has a browser we don't support, this will present them with a page that tells them they have an old browser.  This check is done only when they first visit the site.  A cookie also prevents unnecessary future checks, so this won't slow the app down.
/// </summary>
public class WarnAboutUnsupportedBrowserAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var request = filterContext.HttpContext.Request;
        //this will be true when it's their first visit to the site (will happen again if they clear cookies)
        if (request.UrlReferrer == null && request.Cookies["browserChecked"] == null)
        {
            //give old IE users a warning the first time
            if ((request.Browser.Browser.Trim().ToUpperInvariant().Equals("IE") && request.Browser.MajorVersion <= 7) ||
                (request.Browser.Browser.Trim().ToUpperInvariant().Equals("Chrome") && request.Browser.MajorVersion <= 22) ||
                (request.Browser.Browser.Trim().ToUpperInvariant().Equals("Mozilla") && request.Browser.MajorVersion <= 16) ||
                (request.Browser.Browser.Trim().ToUpperInvariant().Equals("Safari") && request.Browser.MajorVersion <= 4))
            {
                filterContext.Controller.ViewData["RequestedUrl"] = request.Url.ToString();

                filterContext.Result = new ViewResult { ViewName = "UnsupportedBrowserWarning" };
            }

            filterContext.HttpContext.Response.AppendCookie(new HttpCookie("browserChecked", "true"));
        }

    }
}

View reference to the ViewData:

<a href="@ViewData["RequestedUrl"] ">Thanks for letting me know.</a> 

Most Urls work fine. The problem comes when the user enters a URL that has a parameter in it. For example:

    [WarnAboutUnsupportedBrowser]
    public ActionResult Index(string providerkey)

If the Url the user entered is "../Controller/Foo/providerkey", the Url that populates in the view is "Controller/Foo" with the missing parameter that is required to access the page.

How can I make sure that the URL in the view is the entire URL the user originally entered?

  • I don't see anything here that would cause your parameters to be stripped. Are the parameters populated if you remove the filter? – Ant P Jun 03 '13 at 20:24
  • @AntP Without the filter, the view with the ViewData RequestedUrl is never touched, so yes, the parameter is there in the untouched URL and the Controller method acts normally. – user1437804 Jun 03 '13 at 20:43

0 Answers0