Posts Tagged web.config

SuzanneDynamically Setting the Theme

Tuesday, March 9th, 2010

Themes can be used to customize the look of your Website. If you need to select the theme based on user settings, you’ll be glad to know that ASP.NET allows you to set the theme of a page dynamically when the page is being created. This process is pretty straight forward; however, there are a couple of issues that may arise.

First of all, ASP.NET establishes the current theme prior to the page’s Load() event. In order to dynamically change the theme before the Load() event, you’ll need to set it during the page’s PreInit() event.

Setting the Theme in the PreInit() Event

That was easy enough. But what happens if your site uses a master page? Master pages allow you to control the appearance of all the pages on your site from a single master page. So it makes sense to want to set the theme from the master page. However, master pages do not provide a PreInit() event. So setting the theme dynamically from a master page is not as easy as you might guess.

One way to deal with this is to create a class that derives from IHttpModule. This class can be made to respond to all page requests on your site, and it gets called just before the request is handled, in plenty of time to set the theme for the current page.

public class ThemeManager : IHttpModule
{
   public ThemeManager()
    {
    }
  public void Init(HttpApplication app)
    {
        // Set our handler to be called just before handling a request
        app.PreRequestHandlerExecute +=
            new EventHandler(Context_PreRequestHandlerExecute);
    }
  void Context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        // Note: If handler does not implement IRequiresSessionState or
        // IReadOnlySessionState then Session state will be unavailable
        if (HttpContext.Current.CurrentHandler is Page)
        {
            // Set theme
            Page page = (Page)HttpContext.Current.CurrentHandler;
            page.Theme = "MyTheme";
        }
        public void Dispose()
        {
        }
    }
}

Setting the Theme from an IHttpModule-Derived Class

In order to use this class, you’ll have to modify your web.config file.

<configuration>
 <system.web>
 <httpModules>
 <add />
 </httpModules>
 </system.web>
</configuration>

Web.config Changes

Also, be sure to clear any theme settings from your web.config file. Otherwise, it’s possible for ASP.NET to get confused and possibly reference multiple CSS files.

ASP.NET themes are quite powerful, and can be used to manage the appearance of various controls and images. Another thing themes do is let you specify CSS, or style sheets. If this is all your themes are used for, then a much simpler approach can be taken.

All you need to do is add a Literal control inside of the <head> section of your master page. Then, you can set it to include your style sheet during the master page’s Load event.

Since you aren’t modifying the look of controls using skins, there is no need to change this setting in the PreInit() handler. In fact, you could handle it even later than the Load event since all that needs to happen is your page references the correct CSS file.

http://www.all1tunes.com

http://www.all1social.com

Tags: , , , , , , , ,
Posted in Microsoft Technology | No Comments »

SuzanneASP.Net Compilation Tool – do you want to allow updates without redeploying?

Saturday, March 6th, 2010

If we have an ASP.Net website that:
1. We want to pre-compile before deploying to the live servers for the usual reasons of performance (no delay on first hit) and security (source code not hosted on the servers)
2. Before starting the web application, we automatically generate its web.config file
3. By default we want to disable view state in all the pages by adding a
node in the generated web.config
The main issue was that even though the generated web.config file had the correct setting in it, the view state wasn’t being disabled. This confused us for quite a while.
It turns out that the default setting when a website is compiled by the ASP.Net compiler doesn’t allow subsequent updates to the site.
In our particular case, this meant the compiled pages were using the (default) value in our non-existent web.config at compile time, not the one actually on the server at runtime.
Once we realised that, the solution was easy: simply add a –u parameter to the compiler flags which meant:
-u specifies that the Aspnet_compiler.exe should create a precompiled application that allows subsequent updates of contents such as .aspx pages.
If this option is omitted, the resulting application contains only compiled files and cannot be updated on the deployment server. You can update the application only by changing the source markup files and recompiling.

http://www.all1Press.com

http://www.all1tunes.com

http://www.all1social.com

Tags: , , , , ,
Posted in Microsoft Technology | No Comments »

NaggieVisual Studio 2010 clean application level web.config

Friday, February 19th, 2010

Introducing new small improvement that has been made in Visual Studio 2010 & .NET 4 to reduce the size of the ASP.NET application level web.config 3.0 and 3.5 web.config

As ASP.NET technology evolved, the application level Web.config had new things added to it. Since the earlier frameworks were using the same set of machine level configuration files, incremental feature that was added subsequent to the 2.0 release resulted in additional config settings included in the file.
.NET 4 web.config

With .NET 4, the web.config is tremendously reduced in size to improve the simplicity of ASP.NET

The config settings have been moved down to the machine config file. This includes registers all of the ASP.NET tag sections, handlers, modules and settings for the following:
• ASP.NET AJAX
• ASP.NET Dynamic Data
• ASP.NET Routing
• ASP.NET Chart Control

You can look at the trimmed down web.config by creating a .Net 4 ‘ASP.NET Empty Web Application’ in Visual Studio 2010.

Following is the web.config file for .NET 4 C# ‘ASP.NET Empty Web Application’:

image thumb Visual Studio 2010 clean application level web.config

The config file above has settings to tell ASP.NET to enable debugging by default for the application and provides the version of .NET framework to use.

http://www.all1tunes.com

http://www.all1social.com

Tags: , , , , , , ,
Posted in Microsoft Technology, Purely Technical | No Comments »