Format Dates with HTTP Module Not Working

X

xenophon

I am trying to format datetime output with an HTTP Module. Right now
dates are displaying in US format like "MM/DD/YYYY 12:00:00". I want
to change the output to "MM/DD/YYYY" if US or "DD.MM.YYYY" if German,
etc. My HTTP Module code below does not seem to affect the output of
my datagrid with Datetime values in it, no matter what language I put
at the top of my IE browser's language list. Can anyone say how I can
make this better?



public class CultureFilter : IHttpModule
{
public CultureFilter()
{
}
public void Init(HttpApplication httpApplication)
{
try
{
if (HttpContext.Current.Request.UserLanguages.Length > 0)
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(
HttpContext.Current.Request.UserLanguages[0] );
}
}
catch (ArgumentException xxxArg )
{
string oops = xxxArg.ToString();
}
}
public void Dispose()
{
}
#endregion
}


Thanks.
 
B

Brock Allen

This is not the typical patter for doing HttpModule development. In Init
you should be registering for events from the HttpApplication, such as BeginRequest,
EndRequest etc. It is inside of those event handlers where you can access
the Request or Response. The idea if a handler is to process pre-request
events either prior or after the page (or handler) has executed. Init is
not the place for that.
 
S

Steven Cheng[MSFT]

Thanks for Brock's inputs.

Hi xenophon,

As Brock has mentioned, the Init method is used to register the event
handler for the HttpMOdule's events which should not directly include any
code (for processing request/response).

#Custom HttpModule Example
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcustomhttpmodules.
asp?frame=true

So for your scenario, the code should be something like:

public class I18nModule : IHttpModule {
public String ModuleName {
get { return "I18nModule"; }
}

public void Init(HttpApplication application) {
application.BeginRequest += (new
EventHandler(this.Application_BeginRequest));
}

private void Application_BeginRequest(Object source, EventArgs e) {
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
try
{
if (context.Request.UserLanguages.Length > 0)
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(context.Request.UserLanguages[0] );
}
}
catch (ArgumentException xxxArg )
{
string oops = xxxArg.ToString();
}

}


public void Dispose()
{
}
}


Hope also helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top