RSS 2.0 Feed
RSS 2.0


Atom 1.0 Feed
Atom 1.0

  Awesome web.config Changes in ASP.NET 2.0 


I've been spending more and more time developing with .NET 2.0 now that I've got the May community technical preview installed. Chances are that I'll be posting more .NET 2.0 related items as well since that is on my brain. I'll try to keep them to a minimum since I like to keep somewhat of a focus on what is available now, but I'd also like to strees the need to prepare for what is coming. I've always been of the opinion that you need to get into the new technologies as soon as possible, so that you're already an expert by the time the stuff comes out. You know how it goes, you snooze you loose.

There are some really cool changes coming in ASP.NET's web.config files that I am really excited about. I'll just point out a few that I've used (I hate going back to 1.1 because I can't use them). The web.config file in ASP.NET 2.0 allows you to set a lot of things that will apply everywhere in your site. For example, you can tell all pages to inherit from a specific base page class (instead of just using the default System.Web.UI.Page class). Add a pages section and add the pageBaseType, like this:

<system.web>
    <!-- ... -->
    <pages pageBaseType="MyWeb.UI.MyPageBase" />
    <!-- ... -->
</system.web>

Now all pages in my site will inherit from MyWeb.UI.MyPageBase, unless otherwise specified, and I don't have to change the base class on every page I create in the web app. Cool!

You can do the same with your namespace Imports. If you want to import the same set of namespaces on every page, you just add them to an Imports sections under the pages.

<system.web>
    <!-- ... -->
    <pages>    
        <imports>
            <add namespace="MyWeb.UI" />
        </imports>
    </pages>    
    <!-- ... -->
</system.web>

Things are definitely getting cool now. There's a whole lot more in there too (too much to get into here). No more need to register tag prefixes on every page. Now you can add it to the web.config just as we did with namespaces, this time under the registerTagPrefixes section adding the tagPrefix & associated namespace. I just love how things just keep getting smarter. It will only take a few days of using this stuff when Whidbey comes out before we laugh at how we used to have to do that stuff on every page!




                   



Leave a comment below.

Comments

  1. Ian 1/7/2005 9:28 AM
    Gravatar
    Isn't 'pageBaseType' in 1.1?
  2. Ryan Farley 1/7/2005 10:11 AM
    Gravatar
    Ian,

    So it is! (http://msdn.com/library/en-us/cpgenref/html/gngrfpagessection.asp). Big egg on face! Wow. You know I came accross this in ASP.NET 2.0 info and I guess I just never looked to see if it was already available.

    I gave it a quick try and it's not quite the same as it is in ASP.NET 2.0, you loose some functionality & some flexibility when using it in 1.1 compared to how it is used in 2.0.

    Maybe time for a follow-up post showing it in 1.1?

    Thanks Ian,
    -Ryan
  3. T 5/17/2005 12:50 PM
    Gravatar
    That does not work for me for some unknown reason nothing about namspaces in the web config works for me i m using 2.0
  4. Michael Ober 5/23/2005 7:43 AM
    Gravatar
    Ryan-

    Yeah, its in 1.1, and it worked really well for me. But, in 2.0, it gives the error "The 'pageBaseType' attribute must be set to a valid Type name (format: <typename>,<assemblyname>)"

    However, the class that i'm using as the base page is not in a separate assembly- its in the auto-created assembly with the rest of the ASP code.

    The solution? i have not figured it out yet. I don't know what changed between 1.1 to 2.0, so i'm going to keep looking.
  5. Sire404 8/25/2005 11:51 PM
    Gravatar
    <imports> has been renamed to <namespaces>
  6. 阿力 9/27/2005 3:50 AM
    Gravatar
  7. Anand Morbia 10/3/2006 3:19 AM
    Gravatar
    what if i want to set common pageBaseType for all the pages except few page.
  8. sachin pawar 2/6/2007 9:53 PM
    Gravatar
    I have used pageBaseType, but I got following error when I tried to run the application..!

    Thanks
    Sachin

    ******Error********
    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).

    Source Error:



    Line 10: using System.Web.UI.HtmlControls;
    Line 11:
    Line 12: public partial class Model_And_Build_Home : System.Web.UI.Page
    Line 13: {
    Line 14: protected void Page_Load(object sender, EventArgs e)


  9. Will C 4/24/2007 6:21 AM
    Gravatar
    Sachin

    This is because your CodeFile is inheriting from System.Web.UI.Page not from your custom page control. If you set it to inherit from the custom page control it will work fine. Does anyone know of a way to set this so that it automatically creates the Codefile inherrting from the correct control?

    Will
  10. host 6/13/2007 11:27 AM
    Gravatar
    Yes thats great. I love the new asp.net
  11. Chirag 7/4/2007 8:42 AM
    Gravatar
    I had one problem...about applying CSS ..on webpage

    i had used authentication mode as form in webconfig..

    after that i cant get my css effect on page but as soon as i remove the authentication tag form web config

    the css effect takes place..

    so wat d prob consist
  12. Chad 11/13/2007 6:57 AM
    Gravatar
    If you want to specify any type in web.config that is located in your App_Code directory and does not have a namespace, simply use: <add type="MyCustomType, App_Code" />.

    That will work if you are using the web site projects.

    Thanks,
    Chad
  13. Vladimir Kelman 4/21/2008 9:29 AM
    Gravatar
    pages pageBaseType="MyWeb.UI.MyPageBase" works fine if you're using inline model, but with code-behind model it doesn't work. Visual Studio still picks System.Web.UI.Page instead of MyWeb.UI.MyPageBase as a default. Does anyone knows how to force VS to use custom base class a default?

    vkelman at gmail dot com
  14. وستا 9/9/2008 2:48 AM
    Gravatar
    i want to deny a role access to all page in root except where i explicitly allow the role such as booksearch.aspx.what <location> tag is needed for this?
  15. John Sadler 4/23/2009 6:58 AM
    Gravatar
    I have a class file i created called "MyNamespace.ClassName" for example, which i've added to the web.config file namespaces. The project compiles and works fine.

    I then try to add in a web service and it breaks in the web.config on that namespace line with the following error...

    A using namespace directive can only be applied to namespaces; 'MyNamespace.ClassName' is a type not a namespace

    Can anyone help me with this? Is there a way to keep my class file import statement in the web.config and still use a web service? Or am i going to have to add the @Imports to all my .aspx files?

    Thanks
  16. Ryan Farley 4/23/2009 9:14 AM
    Gravatar
    @John,

    The problem is exactly as the error described. You cannot add class names as namespaces. You could add "MyNamespace", but adding the ClassName is not allowed. Why you didn't get that error before I don't know, but you are not allowed to add a class name like a namespace.

    -Ryan
Comments have been closed on this topic.



 

News


Also see my CRM Developer blog

Connect:              


Sponsor

Sections