Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp.net mvc中按浏览器类型显示视图?_C#_Asp.net Mvc_Internet Explorer 9 - Fatal编程技术网

C# 如何在asp.net mvc中按浏览器类型显示视图?

C# 如何在asp.net mvc中按浏览器类型显示视图?,c#,asp.net-mvc,internet-explorer-9,C#,Asp.net Mvc,Internet Explorer 9,我在我的项目中使用了很多css和javascript,它们不仅支持IE9浏览器 所以我为所有视图创建了两个版本,第一个版本用于IE浏览器,它没有css和javascript,第二个版本用于其他浏览器,它有所有css和javascript 如果用户浏览器是IE9(仅版本9),则显示firts视图,否则显示second视图。在控制器中,您可以检查请求的UserAgent属性 完整代码示例: private const string IEUserAgent = "Mozilla/4.0 (compa

我在我的项目中使用了很多css和javascript,它们不仅支持IE9浏览器

所以我为所有视图创建了两个版本,第一个版本用于IE浏览器,它没有css和javascript,第二个版本用于其他浏览器,它有所有css和javascript


如果用户浏览器是IE9(仅版本9),则显示firts视图,否则显示second视图。

在控制器中,您可以检查请求的UserAgent属性

完整代码示例:

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();
}
您可能希望将UserAgent字符串封装在常量文件中,该文件将提供更合适的位置,而不是控制器级别

另一种方法是在Global.asax中使用

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

DisplayModeProvider.Instance.Modes.Insert(0,new DefaultDisplayMode("IE9")
{
    ContextCondition=context=> context.request.UserAgent.Contains("MSIE 9")
});
}

然后,您将在应用程序的“视图”部分中创建一个Index.IE9.cshtml文件,其中包含要使用Internet Explorer 9向用户显示的标记。

您必须在mvc中使用自定义显示模式,在浏览器中使用用户代理

任何浏览器都有一个唯一的用户代理,即internet explorer 9是“MSIE 9”

如果视图名称为First.cshtml

创建名为First.IE9.cshtml的视图

在global.asax中

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

DisplayModeProvider.Instance.Modes.Insert(0,new DefaultDisplayMode("IE9")
{
    ContextCondition=context=> context.request.UserAgent.Contains("MSIE 9")
});
}

从用户代理头开始?基于此有条件地返回视图阅读本文。我认为它对您有所帮助。