Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, reference a different 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 further down I call the imgsrc variable like this:

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

I get an error saying:

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

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

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

like image 702
embryo Avatar asked Sep 26 '14 20:09

embryo


1 Answers

Simply move the declaration outside of 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 could make it a bit 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" />
    }
}
like image 136
mason Avatar answered Sep 21 '22 17:09

mason