I have encountered a problem loading .css file using WebResource in ASP.NET Webform project. The project refers to a sharedProject folder. Every page in the project inherits from a common base page inside the sharedProject. Now I create a CommonStyle.css inside sharedProject and try to inject it into the base page.
Weird thing is that the WebResource with 404 error message has a type of "text/html". After I move the PageBase.cs into MyProject folder, the WebResource can then successfully load. I am so frustrated and confused right now. Please help.
Directory structure is as follows:
- SharedProject
- CommonStyle.css
- PageBase.cs
- MyProject
- Webpage1
- Webpage2
- Webpage3
Code in PageBase.cs, OnInit function:
protected override void OnInit(EventArgs e)
{
HyperLink hlHelpFile = new HyperLink();
ClientScriptManager cs = Page.ClientScript;
Type rsType = typeof(PageBase);
hlHelpFile.NavigateUrl = cs.GetWebResourceUrl(rsType, "Web.CommonStyle.css");
this.Page.Header.Controls.Add(hlHelpFile);
HtmlLink css = new HtmlLink();
css.Href = hlHelpFile.NavigateUrl;
css.Attributes.Add("rel","stylesheet");
css.Attributes.Add("type","text/css");
css.Attributes.Add("media","all");
this.Page.Header.Controls.Add(css);
base.OnInit(e);
}
I have also added webResourceAttribute in AssemblyInfo.cs as follows:
[assembly: WebResourceAttribute("SharedProject.CommonStyle.css", "text/css")]
I've struggled so much but finally figured it out. The thing is I put assembly information in the wrong place. The WebResource assembly should always be put in the same project as the base page class (Shared Project).
I know this is a stupid mistake. I will put this here in case someone else is struggling at the same problem.