Error CS0103: the name '' does not exist in the current context

When my view loads, I need to check which domain the user is visiting, and based on the result, refer to another stylesheet and image source for the logo that appears on the page.

This is my code:

@{ 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"; } } 

Then, a little lower, I name the imgsrc variable as follows:

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

I get an error message:

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

I assume this is because the variable "imgsrc" is defined in the code block, which is now closed ...?

What is the correct way to reference this variable further down the page?

+5
source share
1 answer

Just move the ad outside the 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> 

You can do it a little cleaner.

 @{ 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" /> } } 
+7
source

Source: https://habr.com/ru/post/1203509/


All Articles