我正在使用过滤器,在到达网站时检查用户的浏览器/版本。如果他们使用不受支持的浏览器,我会将他们想要访问的URL保存到名为“RequestedURL”的ViewData中,并重定向到一个视图,告诉他们他们的浏览器是旧的。此视图使用户能够通过单击链接继续。此链接的URL由过滤器中设置的“RequestedUrl”的ViewData属性填充。
过滤器:
/// <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"));
}
}
}
查看对ViewData的引用:
<a href="@ViewData["RequestedUrl"] ">Thanks for letting me know.</a>
大多数网址都运行正常。当用户输入其中包含参数的URL时,就会出现问题。例如:
[WarnAboutUnsupportedBrowser]
public ActionResult Index(string providerkey)
如果用户输入的Url是“../Controller/Foo/providerkey”,则视图中填充的Url是“Controller / Foo”,其中包含访问该页面所需的缺失参数。
如何确保视图中的网址是用户最初输入的整个网址?