2009-09-16 5 views
12

他のすべてのWeb開発者と同様に、私はIE6で作業するために自分のサイトコードをハックすることに不満を感じています。 IE 6をインストールし、IE 7+またはFirefoxにアップグレードするよう丁寧に尋ねてください。IE6ユーザーがASP.NET MVCでアップグレードするように要求する特別なページを表示する方法

IE6ユーザーを検出し、ASP.NET MVCのアップグレードの詳細を示す特別なページを表示する方法を教えてください。

サーバー側のスクリプトでこれを処理するのは良い考えですか?またはこれを処理するためにjQueryのようなクライアントサイドスクリプトを使用することをお勧めしますか?

答えて

20

最も簡単な方法は、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」ビューをレンダリングします。この警告はCookieを使用することで1回のみ表示されます。もちろん、あなたはそれを微調整することもできます。私の見解では、他のブラウザを更新またはダウンロードするためのリンクを提供しました。私も彼らに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> 
+1

非常にきれいな溶液です。どうもありがとう。 – Gopinath

+0

完全なコードは必要なく、ブラウザの情報( 'request.Browser.Browser'と' request.Browser.MajorVersion')にアクセスするのがポイントでした。よくやった。 – nrod

1

IE 6とはまったく異なるページを表示するのは少し厳しいIMHOですが、ブロック/リダイレクトしない限り、サーバー側でこれを検証する必要はありません。

丁寧」あなたは、クライアント側のブラウザを検証し、アップグレードするためにメッセージを思い出させる/警告を示すことを意味します。人々は stoplivinginthepast.com have build a standard logic to do thisに基づいて conditional commentsに基づいています(あなたはリンク先ページの上部にメッセージを表示することをお勧めします)。

http://www.stoplivinginthepast.com/get-the-code/ http://www.stoplivinginthepast.com/wp-content/uploads/2009/02/warninggrab.jpg

画像提供:http://www.stoplivinginthepast.com/

2

特にIE6に異なる非機能ページを提供するために恐ろしいの練習になります。まず最初に、あなたがイギリスにいれば、あなたのサイトを使っているユーザーの20〜25%を実際に止めたくない場合(あなたの状況に応じて)、数秒間DDAにぶつかる可能性が高い。

多くの人が仕事場でIE6を使用することを余儀なくされています。不必要にそれらを吐き出しても、ビジネス上の意味はありません。

つまり、サイトをピクセルに見えるようにする理由はありません。 Request.UserAgentを使用してIE6サーバー側を使用していることを検出でき、ホームページの上部(またはすべてのページの上部)に邪魔にならないメッセージを表示して、ブラウザが非常に古く、もはやそれをサポートしていません。次に、特定のIE6スタイルシート(非常にカットダウン)を提供するか、IE6のレンダリングの問題がサイトを使用できなくするほど重大ではない場合は、それらについて気にする必要はありません。

最近私はインターネットで仕事をしていますが、私はIE6をサポートするために特別料金を請求しています。

13

あなたは、コーディングからの検出を行うことがあります。

// ASP.net MVC C# example 
if (Request.Browser.Browser == "IE" && Request.Browser.Version.ConvertTo<float>() < 7.0) 
{ 
    // output message to urge user to upgrade to latest IE browser 
} 
0

私はIE8以下のトラブルを抱えてこの答えに出くわしました。私はユーザーにIE9以降のユーザーを求めていましたが、IE9の互換モードはIE7として表示されました。

スティーブ・ポッターズの回答に追加して、次のリンクに触発された - http://social.msdn.microsoft.com/Forums/vstudio/en-US/ae715fd2-1ddd-46f7-8c26-9aed6b2103f1/how-to-detect-compatibility-mode-in-ie-any-version?forum=netfxjscript

は、私はそれが誰かの役に立てば幸いに追加

public class WarnAboutIeAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     var request = filterContext.HttpContext.Request; 
     var isIe9Compatible = false; 

     if (request.UrlReferrer == null) 
     { 
      if (request.Browser.Browser.Trim().ToUpperInvariant().Equals("IE") && request.Browser.MajorVersion <= 8) 
      { 
       var useragent = request.Headers.GetValues("User-Agent"); 
       if (useragent != null) isIe9Compatible = useragent[0].Contains("Trident/5.0"); 
       if (!isIe9Compatible) filterContext.Result = new ViewResult {ViewName = "_InternetExplorerOldWarning"}; 
      } 
     } 
    } 
} 

に私のコードを変更しました。

関連する問題