Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.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
ASP.NET:在JavaScript代码中设置C#变量_Javascript_C#_Asp.net_Web Applications - Fatal编程技术网

ASP.NET:在JavaScript代码中设置C#变量

ASP.NET:在JavaScript代码中设置C#变量,javascript,c#,asp.net,web-applications,Javascript,C#,Asp.net,Web Applications,我使用的是ASP.NET C#,在我的视图sall.cshtml中,我有一个JavaScript 用于检测用户是否正在使用Internet Explorer。 警报(“其他浏览器”)或警报(“Internet Explorer”)工作正常 问题是两个c#代码行都将被执行et: {Session[“BrowserName”]=“IE”}和{Session[“BrowserName”]=“other”} 但如果我正在使用Internet Explore,则只应执行 @{Session[“Browse

我使用的是ASP.NET C#,在我的
视图sall.cshtml
中,我有一个JavaScript
用于检测用户是否正在使用Internet Explorer。
警报(“其他浏览器”)
警报(“Internet Explorer”)工作正常

问题是两个c#代码行都将被执行et:
{Session[“BrowserName”]=“IE”}
{Session[“BrowserName”]=“other”}

但如果我正在使用Internet Explore,则只应执行
@{Session[“BrowserName”]=“IE”}

viewsAll.cshtml:

<script>
 var usera = window.navigator.userAgent;
 var ie = usera.indexOf("IE ");

 if(ie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))  // If Internet Explorer
 {    
  alert("Internet Explorer");
  $('head').append('<link href="@Url.Content("~")Content/Styles/styleForIE.css" rel="stylesheet" />');
  @{ Session["BrowserName"] = "IE";}
 }
 else{  // If Other Browser
    alert("Other Browser");
    $('head').append('<link href="@Url.Content("~")Content/Styles/styleForOther.css" rel="stylesheet" />');
    @{Session["BrowserName"] = "other";}
}
</script>

var usera=window.navigator.userAgent;
变量ie=usera.indexOf(“ie”);
如果(ie>0 | |!!navigator.userAgent.match(/Trident.*rv \:11\./)//如果Internet Explorer
{    
警报(“Internet Explorer”);
$('head')。追加('');
@{会话[“浏览器名称”]=“IE”}
}
else{//如果是其他浏览器
警报(“其他浏览器”);
$('head')。追加('');
@{会话[“浏览器名称”]=“其他”}
}

您在这里把服务器端代码和客户端代码的概念混为一谈是错误的:
所有C#代码始终在服务器端执行,即使它位于客户端条件块内。
这意味着您的
@{Session[“BrowserName”]=“XX”}
代码块始终在服务器上执行,而不仅仅是一个适当的代码块,因为这两个代码块都只是“客户端条件”-在服务器上它们只是“文本”

您应该/可以做的是在服务器端评估中打开条件并检查服务器上的用户代理:

<script>
    @if(Request.UserAgent.Contains("IE ") || new Regex(@"Trident.*rv\:11\.").Match(Request.UserAgent).Success)
    {    
        Session["BrowserName"] = "IE";
        <text>
            alert("Internet Explorer");
            $('head').append('<link href="@Url.Content("~")Content/Styles/styleForIE.css" rel="stylesheet" />');
        </text>
    }
    else
    {
        Session["BrowserName"] = "other";
        <text>
            alert("Other Browser");
            $('head').append('<link href="@Url.Content("~")Content/Styles/styleForOther.css" rel="stylesheet" />');
        </text>
    }
</script>

@if(Request.UserAgent.Contains(“IE”)| | new Regex(@“Trident.*rv \:11\”).Match(Request.UserAgent.Success)
{    
会话[“浏览器名称”]=“IE”;
警报(“Internet Explorer”);
$('head')。追加('');
}
其他的
{
会话[“浏览器名称”]=“其他”;
警报(“其他浏览器”);
$('head')。追加('');
}