Blog Home  Home RSS 2.0 Atom 1.0 CDF  
Hairy Spider Blog - .net
A web of intrigue
 
 Tuesday, February 27, 2007
At last what seems to be a throughly complete reference for URL rewriting in .Net

Tip/Trick: Url Rewriting with ASP.NET

2/27/2007 8:55:41 PM (GMT Standard Time, UTC+00:00)  #      .net  | 
 Monday, October 02, 2006

The my valleys house website is bilingual (English and cymraeg.) I'm not a welsh speaker so it did present a challenge. However, the System.Globalization part of asp.net 2 was really helpful. So on my Windows XP SP2 development machine I was able to set the CultureInfo to auto and set the language in my browser to the correct settings and away I went, automatically the site appeared in the language defined in my browser.

This was great, until I came t orelease the site onto the live server at which point Kaboom! no welsh culture info. Wicked, I thought, atleast when I tried to find out I realised that there is support for custom cultures in asp.net 2. Unfortunately you need to own the server that you want to install the culture info - there seems to be no way other than some dubious reflection calls.

So how did I resolve the issue?

I piggy backed an existing culture that was installed on the maching. So in the Application_BeginRequest I find out what culture the client is requesting and if that happens to cy I set the thread culture to be catalans. (Catalans just happened to be the closest alphabetically.)

Argh. Anyway it all seems to be working ok and you can view the welsh version of my valleys house. If you set your browser languages to have welsh as the first entry then you'll automatically get this site.

Maybe someday Windows 2003 will have welsh installed as a culture and I can do this whole thing properly. Windows XP does have welsh but it's only included in service pack 2.

10/2/2006 11:21:55 AM (GMT Daylight Time, UTC+01:00)  #      .net  | 
 Tuesday, February 28, 2006
Well it's not all that difficult.

I've just created something that will enable one to view and edit any scripts that are loaded in the browser.



In essence all you need to do is to create a class library and include a reference to the DOMHelper dll. Then you just need to create a usercontrol. Design the control as usual and when you come to work on the code element make the class inherit the bho.csseditor.Plugin class.


The first method you'll want to override is the Activate method this will be called when a new document is loaded and when the tab is made visible. The title property will be what's displayed in the tab and there's a property on the base object called ParentFrame from which you can get a reference to the mshtml.WebBrowser and mshtml.Document objects.

Once you are happy about life and your coding, then you can import your shiny new plugin into the DOMHelper. To do this is actually a bit of a nightmare, much harder than I thought it would be, but I'll let you know how I managed it and you can do the same back to me:

I found out using FUSLOGVW that you may need to add the DOMHelper and interop.shdocvw assemblies to the GAC. Having thought about this again you may also be able to get away with placing your assembly in the same folder as the DOMHelper that is loaded by Internet Explorer. You'll have to try this yourself.

Anyway, once you've added the assemblies to the GAC using GACUtil (available as part of the 300Mb SDK) you can then load the assembly. Click the little monitor image and choose add-ins and follow the instructions from there.

There you go - you're up and away.

As a starter for 10 you can download the DOMScripter Visual Studio Project, which also contains a binary if you're too lazy to build the code yourself.

Any comments would be welcome.

Notes:
  • You won't be able to do anything in the designer when the the class inherits the Plugin class because it's an abstract class, ya de yada.
  • If you have a textbox on your own plugin then you may want to ensure that it receives the attention that it's due i.e. handles backspace correctly by handling the GotFocus() event and calling the ParentFrame.SetFocus().
  • You can use the CLR Debugger (also part of the SDK) to run IE and find any bugs.

Usual disclaimers apply.
2/28/2006 5:01:01 PM (GMT Standard Time, UTC+00:00)  #      .net | c-sharp | DOM Helper | Inline CSS Editor  | 
 Monday, February 27, 2006
I was asked if there was a simple way to write a browser helper object (BHO) using C#.

It's actually very easy to do this if you know what you're looking for, I used the information on steven cohn's weblog everything is there that you need to write a BHO. I recommend that you read the article - it's very concise and accurate. You can download my interpretation of this BHO article in a single file. When you download this file and include it in a project you'll need to add the Shdocvw COM object and in .Net version 2 there's a .net component called Microsoft.mshtml.

It's incredibly easy to write something that will do the very simple, however when you get into messing with the mshtml and shdocvw then you may run into problems. I had terrible trouble with the DOM Helper. There's absolutely no documentation for mshtml. So most of it is trial, error and a fair bit of guesswork.

If you're looking for something quick then I would recommend that you have a stab at implementing a menu extension. They are far easier to write and can be called from the context menu of IE.


2/27/2006 6:02:57 PM (GMT Standard Time, UTC+00:00)  #      .net | c-sharp  | 
 Thursday, February 02, 2006

Since I installed .Net version 2 I haven't been able to create any new version 1 websites in visual studio 2003. Any .net pages are 404'd and debugging has also broken. Old sites still works.

I found a way to work around this. To get a site working you need to create a new virtual directory, then copy the code over and change the relative settings in the csproj and webinfo files.

I believe that it's got something to do with the way that the virtual directories are being created by Visual Studio. Looking at the information in the IIS Metabase yeilds the following differences between a working application and a broken one:

Key nameworkingfailing
KeyType IIsWebVirtualDir IIsWebDirectory
Win23Error 0 !Missing!
Path !path to site! !Missing!
DirBrowseFlags 1073741886 !Missing!
AccessFlags 513 !Missing!
ScriptMaps !Missing! .htw,C:\WINNT\System32\webhits.dll,3,GET,HEAD,POST

There are also differences in the keys on the bin folder:

Key nameworkingfailing
KeyType !Missing! IIsWebDirectory

Updating the broken setting to those of the fixed settings seems to resolve the situation. If I were so inclined - I'd knock out a script. As it is, I'm tired after writing that dictionary definition thing.

2/2/2006 12:32:36 PM (GMT Standard Time, UTC+00:00)  #      .net | c-sharp  | 
 Wednesday, January 25, 2006
So how the hell do you convert from one coordinate system to another. Well the proper way would be to study and learn all about 3D trig.

The easy way is to read this paper: National GPS Network information: Annexe C. Converting between grid Eastings and Northings and ellipsoidal latitude and longitude

I've done this and it made my eyes bleed. I've taken the information in it and produced the following C# code:

using System;


namespace tt
{
/// /// /// public class LatLonConversions
{
const double a = 6377563.396;
const double b = 6356256.91;
const double e2 = (a -b)/a;
const double n0 = -100000;
const double e0 = 400000;
const double f0 = 0.999601272;
const double phi0 = 0.855211333;
const double lambda0 = -0.034906585;
const double n = (a-b)/(a+b);


private LatLonConversions()
{ }

public static LatLon ConvertOSToLatLon(double E, double N)
{
double phi = 0;
phi = (N-n0)/(a*f0) + phi0;
double M = b * f0 * ( (1 + n + 5/4*n*n + 5/4*n*n*n)*(phi - phi0) - (3*n + 3*n*n + 21/8*n*n*n) * Math.Sin(phi-phi0) * Math.Cos(phi+phi0) +
(15/8*n*n + 15/8*n*n*n) * Math.Sin(2*(phi-phi0))*Math.Cos(2*(phi+phi0))-35/24*n*n*n*Math.Sin(3*(phi-phi0))*Math.Cos(3*(phi+phi0)));

while (N-n0-M>=0.01)
{
phi = (N-n0-M)/(a*f0)+phi;
M = b * f0 * ( (1 + n + 5/4*n*n + 5/4*n*n*n)*(phi - phi0) - (3*n + 3*n*n + 21/8*n*n*n) * Math.Sin(phi-phi0) * Math.Cos(phi+phi0) +
(15/8*n*n + 15/8*n*n*n) * Math.Sin(2*(phi-phi0))*Math.Cos(2*(phi+phi0))-35/24*n*n*n*Math.Sin(3*(phi-phi0))*Math.Cos(3*(phi+phi0)));
}

double v = a*f0*Math.Pow(1-e2*Math.Sin(phi)*Math.Sin(phi),-0.5);

double p = a*f0*Math.Pow(1-e2*Math.Sin(phi)*Math.Sin(phi),-1.5)*(1-e2);
double n2 = v/p-1;

double vii = Math.Tan(phi)/(2*p*v);
double viii = Math.Tan(phi)/(24*p*v*v*v)*(5+3*Math.Tan(phi)*Math.Tan(phi) + n2 - 9*Math.Tan(phi)*Math.Tan(phi)*n2);
double ix = Math.Tan(phi)/(720*p*Math.Pow(v, 5)) * (61 + 90 * Math.Tan(phi) * Math.Tan(phi) + 45 * Math.Pow(Math.Tan(phi), 4));
double x = (1/Math.Cos(phi))/v;
double xi = (1/Math.Cos(phi))/(6*Math.Pow(v, 3)) * (v/p + 2*Math.Pow(Math.Tan(phi), 2));
double xii = (1/Math.Cos(phi))/(120*Math.Pow(v, 5)) * (5 + 28*Math.Pow(Math.Tan(phi), 2) +24*Math.Pow(Math.Tan(phi), 4));
double xiia = (1/Math.Cos(phi))/(5040*Math.Pow(v, 7)) * (61 + 662*Math.Pow(Math.Tan(phi), 2) +1320*Math.Pow(Math.Tan(phi), 4)+ 720*Math.Pow(Math.Tan(phi), 6));

double e = (E-e0);
double lon = (phi - vii * e*e + viii*e*e*e*e-ix*e*e*e*e*e*e ) * 180 / Math.PI;
double lat = (lambda0 + x*e - xi*e*e*e + xii*e*e*e*e*e - xiia*e*e*e*e*e*e*e) *180 / Math.PI;



return new LatLon(lon, lat);
}
}

public class LatLon
{
public double Latitude;
public double Longitude;


public LatLon()
{
Latitude =0;
Longitude =0;
}

public LatLon(double lat, double lon)
{
Latitude =lat;
Longitude =lon;
}

}

}

You can test an implementation of this code using my OS2LatLong webservice.
1/25/2006 10:54:01 AM (GMT Standard Time, UTC+00:00)  #      .net | c-sharp | gmap  | 
 Friday, December 30, 2005
I wanted to have some fun with the Google Maps API I had a feed of data so I thought that I'd use the feed which comes with a little bit of information tagged to a postcode and use that power the google map.

Here's the link to view some posh vehicles on a gmap of the uk

I had some real fun converting the UK style OS map references to lat and long. Which I'll cover in the next post.

12/30/2005 12:19:22 PM (GMT Standard Time, UTC+00:00)  #      .net | gmap | google  | 
 Monday, November 28, 2005

I first wrote multisite about 3 years ago. It's a really simple ISAPI filter that allows IIS to "apparently" host more that one website. It's not designed as a replacement for a proper server shouldn't be used for anything other than development.

The reason I wrote multisite is because at the time I was writing a CMS which controlled a number of sites. I wanted to be able to host the CMS and the client sites on my development machine without having to continually reconfigure IIS.

Under classic asp it worked really well. The second version allowed one to configure the site through it's own webpage. I have a new version waiting to be put up, but one thing that's always been a problem has been hosting more than one ASP.Net application. It is possible to host multiple asp.net applications, I think that they need to be installed on IIS as proper virtual applications.

Now, I think that under ASP.Net 2 that there may finally be a nicer answer. I've just read these two posts and I think that VirtualPathProvider may hold a number of answers. Of course with ASP.Net you've always been able to create a web application outside of IIS but why would you want to reinvent the IIS wheel?

VirtualPathProvider Extensibility in ASP.NET 2.0
Overriding ASP.NET combine behavior using a VirtualPathProvider

I must admit that I don't really get a great deal of time these days to do much development work of my own. I'll take a look at these and then maybe finally I can put Multisite to bed.
11/28/2005 10:51:58 AM (GMT Standard Time, UTC+00:00)  #      .net | Multisite  | 
 Thursday, November 24, 2005

Strange title I know. I found it after running a regmon on a dot-net executable.

Anyway, it's to do with assembly binding more can be found here: Assembly Binding Log Viewer (Fuslogvw.exe)


11/24/2005 10:07:24 AM (GMT Standard Time, UTC+00:00)  #      .net | Misc  | 
 Saturday, November 05, 2005

What with the advent of .Net 2.0 and a request from geniusleft. I've reworked my original MSIL addin for SnippetCompiler to work the version 2.0 beta.

Again you can download the source for MSIL2.0 for SnippetCompiler. You'll have to build it against .net version2. MSBuild seems to to a good job for me.

Usual caveats apply - remember to change the path to ILDASM (Line 88 of Class1.cs) to the copy you have on your machine.


11/5/2005 8:25:14 PM (GMT Standard Time, UTC+00:00)  #      .net | c-sharp | MSIL Addin  | 
 Wednesday, August 31, 2005

BrowserLauncher is a small windows forms application which acts as an intermediary between launching a url and the web browser.

My default browser these days is Firefox, but a number of sites that I access frequently Firefox can not handle. This isn't because Firefox is broken, it's just that the sites are written particularly badly. Also there are a number of sites which I want launched in their own browser.

So I wrote BrowserLauncher. When this runs it registers itself as the default browser through manipulating the StartMenuInternet registry key - it then adds itself to the HKCR\http key. The next time you launch a URL from the desktop, start bar or ShellExecute - the details are passed to BrowserLauncher. BrowserLauncher will search its list of browser entries and launch the URL with the specified browser.

Additionally I've added some DropMyRights code that allows you to launch the browser with 3 levels of privilege, therefore allowing IE to run in Normal or Constrained mode.

Please note:

  • This software works for me, it may not work for you, if you have a bug please let me know and I'll endeavour to fix it.
  • The software will try to manipulate some registry keys.

8/31/2005 9:57:23 AM (GMT Daylight Time, UTC+01:00)  #      .net | BrowserLauncher | c-sharp | Downloads | Source Code  | 
 Tuesday, August 30, 2005

Argh, I've been getting these exceptions being thrown in a windows forms app. I've thought that the error was simply too common an error, however it seems that there are many people out there suffering the same problem.

 

8/30/2005 9:41:35 AM (GMT Daylight Time, UTC+01:00)  #      .net  | 
Copyright © 2008 Rhys Jeremiah. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: