1

I have inserted a web.config key as below:

<add key="WebInstance" value="/testApp"/>

When I call my Site.Master

<link rel="stylesheet" href="<%= WebInstance%>/themes/bs3/css/bootstrap.min.css" />

In my Site.Master.cs

private string _webInstance = ConfigurationManager.AppSettings["WebInstance"]

public string WebInstance { get { return _webInstance ; }}

The first referencing in Site.Master works perfectly. Example:

<link rel="stylesheet" href="/testApp/themes/bs3/css/bootstrap.min.css" />

But in the other pages, it references the full tag.

For instance, when I do inspect element on the browser function, what I see on my <head></head> tags are these:

<link rel="stylesheet" href="<%= WebInstance %>/themes/bs3/css/bootstrap.min.css"  />

I also had the functions of getting the key on my ascx.cs

Why is it just calling the tag instead of getting the real value?

UPDATE

SITE.MASTER and the 'other' pages are 2 different things, the WebInstance key is from the web.config file.

In Site.Master.cs I have its own function to call to the Key and also the other pages have their own respective functions to call the Key.

Please take a look at the attached image for better understanding.

UPDATE AGAIN

I have found the answer to all this, I had runat="server" on my <Head></Head> tags on the other pages, removing it solved the problem. Thanks anyway ;)

11
  • Site.Master is not a page, it is a master page, you cannot open it on its own. Can you explain what exactly do you mean by "The first referencing in Site.Master works perfectly."? Commented Jun 7, 2016 at 13:06
  • What I mean is, those page that inherited from Site.Master will be able to reference to the style pages properly based on the key, but for some other pages when I did the same, instead, it shows <%= WebInstance %> as a physical path and not converting it to the key I want. Commented Jun 7, 2016 at 13:09
  • What is different between these other pages and the pages that work fine? Commented Jun 7, 2016 at 13:12
  • Is it on static html pages where its not working, by any chance? Commented Jun 7, 2016 at 13:13
  • Instead of just showing us code that does work, how about showing us where it doesn't work? Commented Jun 7, 2016 at 13:15

1 Answer 1

1

Your other pages can't directly reference code-behind in their master page. You could add the same property to your individual pages, but then you get duplication.
Although I'm not a big fan of static classes, in this case it might be good compromise:

public static class SiteSettings
{
    public string WebInstance
    { 
        get { return ConfigurationManager.AppSettings["WebInstance"];}}
    }
}

Then in your .aspx, reference SiteSettings.WebInstance.

Some developers will make a base page that other pages inherit from and put common properties there. That can work but I'd use it as a last resort. Sooner or later you're going to want some behaviors of the base page but not others, or you'll have to make some broad changes to the site and it becomes a big pain.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.