gpt4 book ai didi

c# - 错误 CS0103 : The name ' ' does not exist in the current context

转载 作者:太空狗 更新时间:2023-10-29 22:58:40 24 4
gpt4 key购买 nike

当我的 View 加载时,我需要检查用户正在访问哪个域,并根据结果为页面上显示的 Logo 引用不同的样式表和图像源。

这是我的代码:

@{
string currentstore=HttpContext.Current.Request.ServerVariables["HTTP_HOST"];

if (currentstore == "www.mydomain.com")
{
<link href="/path/to/my/stylesheets/styles1-print.css" rel="stylesheet" type="text/css" />
string imgsrc="/content/images/uploaded/store1_logo.jpg";
}
else
{
<link href="/path/to/my/stylesheets/styles2-print.css" rel="stylesheet" type="text/css" />
string imgsrc="/content/images/uploaded/store2_logo.gif";
}
}

然后,再往下一点,我这样调用 imgsrc 变量:

<a href="@Url.RouteUrl("HomePage")" class="logo"><img  alt="" src="@imgsrc"></a>

我收到一条错误消息:

error CS0103: The name 'imgsrc' does not exist in the current context

我想这是因为“imgsrc”变量是在现在关闭的代码块中定义的...?

在页面下方引用此变量的正确方法是什么?

最佳答案

只需将声明移到 if block 之外。

@{
string currentstore=HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
string imgsrc="";
if (currentstore == "www.mydomain.com")
{
<link href="/path/to/my/stylesheets/styles1-print.css" rel="stylesheet" type="text/css" />
imgsrc="/content/images/uploaded/store1_logo.jpg";
}
else
{
<link href="/path/to/my/stylesheets/styles2-print.css" rel="stylesheet" type="text/css" />
imgsrc="/content/images/uploaded/store2_logo.gif";
}
}

<a href="@Url.RouteUrl("HomePage")" class="logo"><img alt="" src="@imgsrc"></a>

你可以让它更干净一些。

@{
string currentstore=HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
string imgsrc="/content/images/uploaded/store2_logo.gif";
if (currentstore == "www.mydomain.com")
{
<link href="/path/to/my/stylesheets/styles1-print.css" rel="stylesheet" type="text/css" />
imgsrc="/content/images/uploaded/store1_logo.jpg";
}
else
{
<link href="/path/to/my/stylesheets/styles2-print.css" rel="stylesheet" type="text/css" />
}
}

关于c# - 错误 CS0103 : The name ' ' does not exist in the current context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26067741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com