Register Global Namespaces in ASP.NET MVC 3 Preview 1 and ASP.NET WebForms

Posted by: Malcolm Sheridan , on 9/10/2010, in Category ASP.NET MVC
Views: 38249
Abstract: The following article demonstrates how to register global namespaces in ASP.NET MVC 3 Preview 1.
From time to time you find little gold nuggets that you had no idea existed. This is a gold nugget that you can not only use in ASP.NET MVC 3 Preview 1, but also ASP.NET WebForms. If you use the Razor view engine, there's no simple way to register a global namespace. For example, this code will work:
@System.Globalization.DateTimeFormatInfo.CurrentInfo.MonthNames.Length
The following code won’t work because the System.Globalization namespace hasn’t been imported into the view:
@DateTimeFormatInfo.CurrentInfo.MonthNames.Length
If I wanted to register the System.Globalization namespace inside a view, this won’t work as there’s no syntax for this:
<%@ Import Namespace="System.Globalization" %>
 
Currently in Razor there is no page directive syntax to do that. A second option would be to register the namespace in the pages element in the web.config file. 
 
<pages>
      <namespaces>       
        <addnamespace="System.Globalization" />
      </namespaces>
</pages>
 
This is the preferred option in my opinion because it allows you to centralize the namespace registrations. This however doesn't work with the Razor view engine currently. Fingers crossed this will work when it’s released. 
If you want to import a global namespace into the current Razor views, it’s a simple two step process. Open the AssemblyInfo file under the Properties folder and add the following line of code:
[assembly: PreApplicationStartMethod(typeof(PreStartupCode), "Starting")]
 
PreApplicationStartMethod is a new attribute in .NET 4.0 that allows you to have code run way early in the ASP.NET pipeline as an application starts up. Way early, even before Application_Start. The first argurment is the type of class to be run. This is a static class that I have called PreStartupCode. The second argument is the method to run. Here’s the PreStartupCode class:
public static class PreStartCode
{
public static void Starting()
       {
            CodeGeneratorSettings.AddGlobalImport("System.Globalization");
       }
}
 
The code above is using the new CodeGeneratorSettings class in .Net 4.0. This gives you the power to inject settings, such as global namespaces, into your project. If you try the following code now, it will work:
@DateTimeFormatInfo.CurrentInfo.MonthNames.Length
The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET, a Telerik Insider and a regular presenter at conferences and user groups throughout Australia and New Zealand. Being an ASP.NET guy, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with jQuery and JavaScript. He also writes technical articles on ASP.NET for SitePoint and other various websites. Follow him on twitter @malcolmsheridan


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Martin Bowle on Saturday, September 11, 2010 12:16 AM
Indeed a gold nugget! Was this there is asp 2.0?
Comment posted by Martin Bowle on Saturday, September 11, 2010 12:18 AM
I meant to ask if we can do this in 2.0 too? any workarounds to register a global nm before app_start?
Comment posted by Malcolm Sheridan on Saturday, September 11, 2010 5:13 PM
Hi Martin,

This is new to .Net 4.0.  Registering in the web.config file is still the preferred option in 2.0.
Comment posted by Dmitry Boltrushko on Thursday, September 30, 2010 8:06 AM
@using System.Globalization

This works in Razor

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

Tags

JQUERY COOKBOOK

jQuery CookBook