Reputation: 706
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.
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 ;)
Upvotes: 1
Views: 95
Reputation: 29272
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.
Upvotes: 1