0

Answer

Posting this here as 8 hours hasnt passed for me to answer my own question:

I figured this out. I removed Server.MapPath from the PreRender and it rendered it correct. Brain fart on my part. Thanks guys

in my code i have:

CssIncludes.Add("~/Sites/0/PageLayouts/Predefined/News/CSS/HeaderMenu.css");

Once rendered on page it comes out in the page source as:

 <link href='C:\inetpub\wwwroot\mysite\Sites\0\PageLayouts\Predefined\News\CSS\HeaderMenu.css' type='text/css' rel='stylesheet' />

now if i manually copy that css url and paste it in my browser, it loads the css fine, yet it will not show up on my c:\localhost\mysite\ at all!

Any solutions?

CSS Includes list:

public List<string> CssIncludes
    {
        get
        {
            if (_cssincluded == null)
                _cssincluded = new List<string>();
            return _cssincluded;
        }
    }

void Page_PreRender(object sender, EventArgs e)
    {
        foreach (string css in CssIncludes)
        {
            Page.Header.Controls.Add(new LiteralControl(string.Format("<link href='{0}' type='text/css' rel='stylesheet' />", Server.MapPath(css))));
        }
    }
2
  • It is a List i have setup. Added it to my question at the bottom Commented Apr 14, 2012 at 18:02
  • ok i figured it out, i removed the Server.MapPath Commented Apr 14, 2012 at 18:09

3 Answers 3

2

remove ~ sign and try again, i hope it will work

Add website path key in web.config in appSettings section

<add key="Websitepath" value="http://www.yoursite.com" /> 

and make url like

includeCss(ConfigurationManager.AppSettings["Websitepath"].ToString()+"Sites/0/PageLayouts/Predefined/News/CSS/HeaderMenu.css";
Sign up to request clarification or add additional context in comments.

5 Comments

Tried and this didnt change anything :(
Why don't you put css path in html page instead of loading in code file, secondly you can store your website path in some configuration file like web.config and later append /Sites/0/PageLayouts/Predefined/News/CSS/HeaderMenu.css
i want to control it via the cs because i have different sets of css from many different layout designs.
add in web.config in appSettings section <add key="Websitepath" value="yoursite.com" /> and make url like ConfigurationManager.AppSettings["Websitepath"]+Sites/0/PageLayouts/Predefined/News/CSS/HeaderMenu.css;
i figured it out, i removed server.mappath, i guess i didnt get enough sleep last night. pretty embarressing
1

ASP.Net is trying to parse the css path as a windows file path.

I recommend you write the CSS <link/> yourself and make it relative to the current page.

Int this case the proper tag would likely be:

<link type='text/css' rel='stylesheet' href='CSS/HeaderMenu.css'/>

keep in mind that any links are UNIX-style paths (i.e. / not backslashes).

EDIT:

Accord to the Asker (SandMan) Server.MapPath needed to be removed as well.

This is likely what was parsing the css path.

2 Comments

i added the code above to show how i am adding the CSS controls to my page via C#
yes it is. i had a brain fart. i removed server.mappath and it is working fine. if someone puts my answer into an answer i will mark it as correct as i can't mark my own for 8 hours.
0

figured this out. I removed Server.MapPath from the PreRender and it rendered it correct. Thanks guys.

Changed:

CssIncludes.Add("~/Sites/0/PageLayouts/Predefined/News/CSS/HeaderMenu.css");

to:

CssIncludes.Add("../Sites/0/PageLayouts/Predefined/News/CSS/HeaderMenu.css");

and changed

void Page_PreRender(object sender, EventArgs e)
{
    foreach (string css in CssIncludes)
    {
        Page.Header.Controls.Add(new LiteralControl(string.Format("<link href='{0}' type='text/css' rel='stylesheet' />", Server.MapPath(css))));
    }
}

to:

void Page_PreRender(object sender, EventArgs e)
{
    foreach (string css in CssIncludes)
    {
        Page.Header.Controls.Add(new LiteralControl(string.Format("<link href='{0}' type='text/css' rel='stylesheet' />", (css))));
    }
}

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.