Standing on the shoulders of giants.
 Wednesday, December 27, 2006

 ... or why does your tool introduce bugs in my code and doesn't warn me?

When migrating an ASP.NET 1.1 project to 2.0, using the VS 2005 (SP1) conversion wizard and the Web Application Project conversion tool (included in service pack 1), the AutoEventWireup @page attribute is set to "True" for all pages, even when you previously had set it to "False".

This potentially breaks your application, when you have eventhandlers for init/load/prerender bound manually in places where the conversion tool doesn't change them, e.g. in the constructor of a common basepage. This will cause the events to be handled twice, with unexpected results.

The problem is not that the default is changed from 1.1 to 2.0, but that by modifying my code the tool introduces new (interesting) bugs. And all this without a big neon-sign, which warns for the changes being made and the potential problems it introduces.

27 Dec 2006 17:01 W. Europe Standard Time  #    Comments [0] - Trackback
Asp.Net | Development

 Wednesday, August 16, 2006

Although there are several ways in ASP.Net to influence the way DateTimes are displayed some methods are better than others and some are evil.

The easiest way to display DateTime and have some influence on them, is to specify the culture and the uiCulture in the globalization tag in the web.config. This is the preferred way if you can use the default date/time formats in that culture.

Example:

<%=DateTime.Now %> is rendered as "16-8-2006 10:14:49" with this globalization tag <globalization uiCulture="nl-NL" culture="nl-NL" /> and rendered as "8/16/2006 10:18:37" with the culture and uiCulture set to en-US.

If you want a little more control, or when the default date/time formats don't offer enough flexibility you can specify a format in the ToString method of the DateTime object. This will still use the culture specified in the web.config for the (abbreviated) names of the months. The downside to this method is that you have to remember to set the correct format string, every time you want to display a date/time.

Example:

<%=DateTime.Now.ToString( "dd MMM \\'yy" )%> is rendered as "16 août '06" with the culture set to fr-FR ( the \\ are required for escaping the ' ).  

 When you want to offer your users a localized version of your web-application, you can't rely on the globalization settings in the web.config, instead you'll have to either set the Culture and UICulture of the current executing thread or use one of the ToString overrides that accepts an IFormatProvider. When you only specify an IFormatProvider, the default (or G) format is used to render the string and although you can override the default format, that is generally considered evil.

Example:

<%=DateTime.Now.ToString( new System.Globalization.CultureInfo("es-ES")) %> is rendered as "16/08/2006 11:43:11", while <%=DateTime.Now.ToString("dd MMM \\'yy", new System.Globalization.CultureInfo("es-ES")) %> is rendered as "16 ago '06".

These methods were the only methods that were available on version 1.x of the .Net framework. In .Net 2.0 you have the ability to define a custom culture using the CultureAndRegionInfoBuilder class. After creating a new culture, this new culture can be installed on a computer and used by any application on that computer. This allows all (web-)applications on that computer to use the same formatting for dates, and numbers, casing etc. This way you can change the default format for string rendering in a less evil way.

Example:

First setup the custom culture using a console application, since this requires administrator rights.

class Program{ 

static void Main( string[] args ) {

if (args.Length == 0 || args[0].EndsWith("i", StringComparison.OrdinalIgnoreCase)) {

RegisterNewRegion();
Console.WriteLine("Registered new region \"x-en-US-custom\".");
} else if (args[0].EndsWith("u", StringComparison.OrdinalIgnoreCase)) {
UnRegisterNewRegion();
Console.WriteLine("Unregistered new region \"x-en-US-custom\".");
} else {
Console.WriteLine("use /i to register and /u for unregister.");
}

Console.WriteLine("Press enter.");
Console.ReadLine();
}

static void RegisterNewRegion() {

// instantiate a new CultureAndRegionInfoBuilder object named x-en-US-demo
CultureAndRegionInfoBuilder cib = new CultureAndRegionInfoBuilder("x-en-US-custom", CultureAndRegionModifiers.None);
//load the culture data from the default en-US culture and region
cib.LoadDataFromCultureInfo(new CultureInfo("en-US"));
cib.LoadDataFromRegionInfo(new RegionInfo("US"));
// set the custom date/time format
cib.GregorianDateTimeFormat.ShortDatePattern = "dd-MM-yy";
cib.GregorianDateTimeFormat.LongDatePattern = "dd MMM \\'yy";
cib.GregorianDateTimeFormat.ShortTimePattern = "HH:mm";
cib.GregorianDateTimeFormat.LongTimePattern = "HH:mm";

// register the culture
cib.Register();
}

static void UnRegisterNewRegion() {

CultureAndRegionInfoBuilder.Unregister("x-en-US-custom");
}

}

Now you can use your custom culture in any web-application, like the previous examples.

<%=DateTime.Now %> is rendered as "16-8-2006 10:14:49" with this globalization tag <globalization uiCulture="x-en-US-custom" culture="x-en-US-custom" />

Another use would be for writing dates to a log-file, when you need to sort or parse them in future.

Thanks to michkap from Sorting It All Out for the tip on CultureAndRegionInfoBuilder and the post that inspired this post.

16 Aug 2006 13:59 W. Europe Daylight Time  #    Comments [0] - Trackback
Asp.Net | Development

 Friday, August 12, 2005

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

Note: this will be 0.0.0.0 if you use it in the code-forward of an aspx page.

12 Aug 2005 11:54 W. Europe Daylight Time  #    Comments [0] - Trackback
Asp.Net | Codesnippet

 Thursday, June 23, 2005

Interesting and annoying behaviour of Asp.Net with respect to HttpModules; the httpModules element in a web.config in a subdirectory, (note: not a seperate application) are ignored. No warning, no error, no nothing, just ignored.

After I spend the better part of yesterday afternoon working on this problem and google wasn’t very helpfull, I hope this will be helpfull to someone.

Proof of concept:

Solution

Layout of the project, notice the second web.config in the subdirectory.

HttpModules element in the root web.config:

<httpModules>

         <add name="FirstHttpModule" type="httpModule.HttpModules.FirstHttpModule, httpModule" />

</httpModules>

Interesting part of the FirstHttpModule class

private void context_BeginRequest(object sender, EventArgs e) {
    HttpApplication source = sender as HttpApplication;
    if( source != null ){
        source.Context.Response.Write( "Hello from FirstHttpModule. " );
    }
}

HttpModules element in the subdirectory web.config:

<httpModules>

<clear />

         <add name="SecondHttpModule" type="httpModule.HttpModules.SecondHttpModule, httpModule" />

</httpModules>

Interesting part of the SecondHttpModule class:

private void context_BeginRequest(object sender, EventArgs e) {

private void context_BeginRequest(object sender, EventArgs e) {
    HttpApplication source = sender as HttpApplication;
    if( source != null ){
        source.Context.Response.Write( "Hello from SecondHttpModule. " );
    }
}

Result of a request to webform1.aspx in the root:

FirstModule

Result of a request to webform1.aspx in the subdirectory:

SecondModule

 

Source for the proof of concept: httpModule.zip (17.17 KB)
23 Jun 2005 15:53 W. Europe Daylight Time  #    Comments [1] - Trackback
Asp.Net | Development

 Friday, May 20, 2005

Mart is experimenting with WSS 2.0 on windows 2003 R2, he just posted his first article on the installation.

20 May 2005 11:24 W. Europe Daylight Time  #    Comments [0] - Trackback
Asp.Net | Development

 Thursday, January 27, 2005

Scott Hanselman has a good tip to protect your clients’ clients’ privacy.

This is one a lot of people know, but it's worth covering again because it's easily forgotten as it's a small detail. Since we do eFinance sites, we often don't want folks' UserNames collected and stored in AutoComplete, especially when the site is browsed on a public machine.

<form id="formSignOn" autocomplete="off" method="post" runat="server">

Note that autocomplete="false" doesn't work. However, autocomplete="off" works in both IE and FireFox.

27 Jan 2005 16:55 W. Europe Standard Time  #    Comments [2] - Trackback
Asp.Net

 Wednesday, November 17, 2004

Changes have been announced to both the naming of "special" directories (i.e. resources, data, bin ... ) and the compilation model of Asp.net in beta 2 of vs 2005. [0]

Personally I would have preferred a single root for the 'special'-directories, which has been considered, but rejected.

The single root approach was rejected because it did not enable us to support directories located in arbitrary locations in the application tree, such as the local resources directory. 

The default compilation model is changed to keep the markup outside of the compiled binaries. I'm not sure what to think if it, but certainly during development it's a lot easier to work with.

[0] changes in vs 2005 beta 2 for asp.net

17 Nov 2004 00:15 W. Europe Standard Time  #    Comments [0] - Trackback
Asp.Net | Visual Studio 2005 | Whidbey

 Monday, May 31, 2004

It took me some time to find out, what options I had to enable to enable debugging in a Whidbey ASP.Net project in windows 2003 server, so maybe this is usefull for someone.

Start with a regular webproject, using the IIS Manager map a virtual directory (EKPool in this picture) to the directory where you created this webproject. Make sure it's an application on the "Directory"-tab and make sure it's using v 2.0.* of the framework on the "ASP.Net"-tab.

Allow the "ASP.NET v2.0.*" Web Service Extension, prohibited by default.

Open the "property pages" of your webproject. Change the Server property on the "Start Options"-tab to "Use custom server" and enter the path to the virtual directory you just created in the "Base Url" textbox. Check the ASP.NET debugging checkbox.

Check the web.config file for the "compilation node" :<compilation debug="true" defaultLanguage="c#" />.

Start debugging with [F5] or run the application without debugging with [CTRL]+[F5].

This also works if you create an application in the root of your website, something which won't work, when you use the integrated webserver in Visual Studio Whidbey.

31 May 2004 14:28 W. Europe Daylight Time  #    Comments [0] - Trackback
Asp.Net | Visual Studio 2005

Generally I'm quite impressed with the work they've done on Whidbey thus far, but I feel some things could have been done better.

The MembershipProvider framework, works quite well in simple scenarios, but I find it too rigid to be really usefull.

My main concern is the use of a MembershipUser class in the UpdateUser method, which makes using a derived class less easy, for example a MembershipUser class with a birthdate or a PIN code. Same goes for the CreateUser method, only it takes strings as parameters representing all the properties of MembershipUser, why not a MembershipUser?

Another problem is the requirement to implement all methods, all methods on the MembershipProvider are marked abstract. In most cases I don't have the need to use a Question and Answer system to retrieve or reset a password. Not implementing GetPassword(string username, string answer) and ResetPassword(string username, string answer), for example by throwing a NotImplementedException, and relying on the documentation, is a maintenance disaster waiting to happen.

A better solution, I think would have been to use Interfaces for both base classes, those are much easier to extend and less usefull methods can be hidden by implementing them explicitly in yout customg provider, i.e. IMembershipProvider.ChangePassword(string username, string oldPassword, string newPassword).

31 May 2004 13:35 W. Europe Daylight Time  #    Comments [0] - Trackback
Asp.Net | Visual Studio 2005 | Whidbey

 Saturday, May 22, 2004

Kent Sharkey struggled with Browser Agent and Regex and created these browser caps, so Asp.Net doesn't tread Mozilla browser as low-end browsers. Add them to your machine config.

 

22 May 2004 18:14 W. Europe Daylight Time  #    Comments [0] - Trackback
Asp.Net | Development

 Sunday, May 16, 2004

Caching pages, or parts of pages can give you a great boost in performance for your web app. Even if you only cache a page for a couple of seconds, the difference in performance can be huge for high load sites.

[ASP.NET Micro Caching: Benefits of a One-Second Cache]

16 May 2004 15:12 W. Europe Daylight Time  #    Comments [0] - Trackback
Asp.Net

 Monday, March 01, 2004

How you can and why you shouldn't hijack __doPostBack on a regular basis and what's been improved in Asp.Net 2.0 (Whidbey) to make life for web- and controldevelopers easier.

No more hijacking of __doPostBack in Whidbey

01 Mar 2004 21:13 W. Europe Standard Time  #    Comments [0] - Trackback
Asp.Net

 Monday, February 02, 2004

Dino Esposito announces his asp.net 2.0 book, this won't be out for a couple of months, at the same time as the beta 1 (which should hit around Tech Ed 2004 ).

02 Feb 2004 20:44 W. Europe Standard Time  #    Comments [0] - Trackback
Asp.Net | Development

 Sunday, January 25, 2004

Encrypt a set of name/value pairs into a single string, which can make passing them in a querystring a little safer. Not that you can trust anything coming from a user, but every little bit helps.

25 Jan 2004 23:40 W. Europe Standard Time  #    Comments [0] - Trackback
Asp.Net | Development

 Thursday, December 04, 2003

A new Mono Release ( 0.29 ). In this release Asp.Net is considered feature-complete.

04 Dec 2003 00:14 W. Europe Standard Time  #    Comments [0] - Trackback
Asp.Net | Development

 Friday, November 21, 2003

Steve Maine has a great sessionstate preformance tip. On a related note, don't overload the constructors for aspx pages (the class in the code behind file), this resulted in a hard to diagnose httpexception, when using SessionState.

21 Nov 2003 22:46 W. Europe Standard Time  #    Comments [0] - Trackback
Asp.Net | Development

 Thursday, November 20, 2003

Avonelle has some issues with web setups, I hope this will get a lot easier in a Whidbey.

20 Nov 2003 00:41 W. Europe Standard Time  #    Comments [0] - Trackback
Asp.Net | Development

Subscribe
About
newtelligence dasBlog 2.1.7309.765
Admin
All Content © Copyright 2008, Paul van Brenk
DasBlog theme 'Business' created by Christoph De Baene (delarou)