ASP.NET theme not rendering correctly

2020-05-04 11:00发布

问题:

I have a small web application which uses themes. The themes work on host, so on preinit, if the host = a, load x theme, if the host = b, load y theme.

In my code this looks like:

If request.url.host.contains("a") Then Page.Theme = x Else request.url.host.contains("b") Then Page.Theme = y

I have a url which is a.abc.com and another which is b.abc.com (well it is this structure, but the letters are meaningful/company names). Problem is (and I have done host == ""), when I debug my site on localhost (another clause in the above if block where host = localhost), the style renders perfectly. Alignment of elements are perfect as I expect with the numerical values I have provided for width, margins, etc in the css. But when I use the publish tool of VS2008 (with updatable ticked), and upload to a.abc.com, which has the same stylesheet as localhost (a copy in its own folder), there are all sorts of alignment issues as if I have done no work. Why do my styles render incorrectly @ runtime? If it helps, I am using VS2008 Pro Edition, IIS6 and Windows Server 2003.

What is frustrating is that the page source indicates the theme is loading ok, as it is referenced in HTML head. So when I publish, the theme for a.abc.com is loaded and referenced. Other than all this information, there is no obvious sign of what the problem is. I haven't tried to conventionally reference a single CSS file in the ASPX markup, but if I did and this worked, then it doesn't explain the problem either, anyway.

Thanks

回答1:

Are you setting the Theme early enough in your code? A Theme must be applied to a page very early in the request lifecycle, in the PreInit event, if you do it in code-behind.



回答2:

I encountered this problem recently and wrote a quick blog that explains how to get around it. Just in case someone else is having this problem, you just need to authorize the Theme folder in your web.config.



回答3:

Yep, it is on page preinit.

I think it is to do with forms authentication. If I view (not debug) the login page, it picks up the localhost theme and everything is displayed correctly. The login control is in the middle of the page, as it should be, etc.

But if I debug the page and, as you'd expect, the login page is the start/default page, the login control appears to the left as it does when I deploy and the source indicates the css for the appropriate host has been picked up - only when I debug, there is no style applied until I login.



回答4:

Are you using the built-in web server? When running in ASP.Net Development Server, the styles in App_Themes won't be used on any unauthenticated page (such as Login.aspx or ForgotPassword.aspx). That's because the user doesn't have browse permissions on that folder yet, or the App_Themes folder lacks browsing permissions. Apparently, IIS handles this but Cassini doesn't.

Try adding this to web.config to let Themes and styles work before authentication.

Or, if possible, switch to running the app on IIS on your machine.



回答5:

I have enabled and installed all the sub features in

Internet Information Services --> World Wide Web Services --> Common HTTP Features

This solved by Theme not working problem.