1

i use many css and javascript in my project which not support Only by IE9 browser,

so i create two verson for all view, First version for IE browser that does not have css and javascript, and second version for other browser that does all css and javascript.

i want if user browser is IE9 (only Version 9) then display firts view else display second View.

Joe herman
  • 134
  • 6

2 Answers2

2

you must use Custom DisplayMode in mvc and User-agent in browsers.

any browser have a unique user-agent that internet explorer 9 is "MSIE 9".

if view name is First.cshtml,

create view that is name: First.IE9.cshtml

and in global.asax

protected void Application_Start(){
//other code

DisplayModeProvider.Instance.Modes.Insert(0,new DefaultDisplayMode("IE9")
{
    ContextCondition=context=> context.request.UserAgent.Contains("MSIE 9")
});
}
Morteza Jangjoo
  • 1,780
  • 2
  • 13
  • 25
1

In your Controller you could inspect the UserAgent property of the Request.

HttpContext.Current.Request.UserAgent

Full code example:

private const string IEUserAgent = "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)";

public ActionResult Index()
{
    string userAgent = HttpContext.Current.Request.UserAgent;

    if (userAgent == IEUserAgent) 
    {
       return View("IE9View");
    }

    return View();
}

You may want to encapsulate the UserAgent string(s) in a constants file which would serve a more appropriate place rather than at the Controller level.

An alternative approach would be to use a DisplayModeProvider within the Global.asax

DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("IE9")  
{  
    ContextCondition = (ctx => ctx.GetOverriddenUserAgent()  
    .IndexOf("MSIE 9", StringComparison.OrdinalIgnoreCase) > 0)     
});  

You would then create a Index.IE9.cshtml file within your View section of your application containing the markup to be displayed to users utilising Internet Explorer 9.

Darren
  • 68,902
  • 24
  • 138
  • 144