가장 쉬운 일이 IMO 액션 필터 속성을 만드는 것입니다. 그런 다음 컨트롤러에 태그를 붙이거나 MVC3의 전역 필터에 추가 할 수 있습니다.
다음은 속성의 : IE6에 대한
/// <summary>
/// If the user has IE6, this will present them with a page that tells them they have a crappy old browser. It gives them options to upgrade but they can also
/// choose to proceed anyway. 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 WarnAboutIE6Attribute : 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().EqualsExact("IE") && request.Browser.MajorVersion <= 6)
{
filterContext.Controller.ViewData["RequestedUrl"] = request.Url.ToString();
filterContext.Result = new ViewResult { ViewName = "InternetExplorerOldWarning" };
}
filterContext.HttpContext.Response.AppendCookie(new HttpCookie("browserChecked", "true"));
}
}
}
이 속성을 확인하고, 존재 있다면, 그것은 당신이 만들 수있는 "InternetExplorerOldWarning"보기를 렌더링합니다. 이 경고는 쿠키를 사용하여 한 번만 표시됩니다. 물론 원하는대로 조정할 수 있습니다. 필자는 다른 브라우저를 업데이트하거나 다운로드 할 수있는 링크를 제공했다. 나는 또한 그들에게 IE6을 계속 사용할 기회를 주었다. 그것을 밖으로 체크하십시오 :
<h3>
Your Internet Explorer is Outdated</h3>
<div class="warning">Your version of Internet Explorer is a bit too old and unfortunately won't work well with this site.</div>
<p>Have no fear. You have options and in just a few minutes you can be rocking out in our app:</p>
<ul>
<li>If you have FireFox, Safari, or Google Chrome already on your computer use one of them for Takeoff instead.</li>
<li>Upgrade to the <a href="http://www.microsoft.com/windows/internet-explorer/worldwide-sites.aspx">latest Internet Explorer.</a> You can download and install right away. Even Microsoft recommends you do this.</li>
<li>Download an Internet Explorer alternative. We recommend <a href="http://www.mozilla.com/en-US/firefox/firefox.html">FireFox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.com/chrome">Google Chrome</a>. Choose one or all because each is great!</li>
</ul>
<p>This warning page will only show once. If you really want to use Takeoff with your current Internet Explorer, we won't stop you. But beware, it will probably look like garbage!</p>
<p>Whatever dude, I want to <a href="@ViewData["RequestedUrl"] ">my old, insecure, scary, dangerous version</a> of Internet Explorer.</p>
</div>
매우 깨끗한 용액. 고마워. – Gopinath
전체 코드는 필요 없지만 브라우저 정보 ('request.Browser.Browser' 및'request.Browser.MajorVersion')에 액세스하는 것이 핵심이었습니다. 잘 했어. – nrod