views:

30

answers:

2

Back Story
I am currently updating an existing web application to support a multi-tenant environment. Today we current host an application on http://www.example.com/MyApp, but now we must be able to support multiple clients on the same web application.

So each client should be able to access the application through urls like: http://www.example.com/MyApp/Client1
http://www.example.com/MyApp/Client2

The web application is an ASP.NET webforms app that we are also upgrading to use .NET 4. I have already implemented the new routing available in ASP.NET 4, and I haven already put the appropriate routes in place to support the multi-tenant architecture.

The example URL http://www.example.com/MyApp/Client1/SomeModule/SomePage.aspx will route to and execute /MyApp/SomeModule/SomePage.aspx and I have access to the client part from within the RouteData.

BUT...

Main Question:
We have links in many parts of the application that use url's like ~/SomeModule/SomePage.aspx, so when they render out to the browser they will still show /MyApp/SomeModule/SomePage.aspx, but what I really want is to have it contain one of my route values to inject the client page like MyApp/Client1/SomeModule/SomePage.aspsx

Is there a way to override what the ~/ means throughout my application without going through my entire app and updating the links?

A: 

Set the AppRelativeVirtualPath property on your Page object to your desired path. But you have to do it as the first thing after your handler is created, since every control added to your Controls-collection will inherit this value from its parent.

A way to do this is to subclass the PageHandlerFactory class which is responsible for creating a Page instance when your request a .aspx file. Override the GetHandler with the following code:

public override IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
{
    var handler = (Page)base.GetHandlerHelper(context, requestType, VirtualPath.CreateNonRelative(virtualPath), path);
    page.AppRelativeVirtualPath = "...";

    return page;
}

and of course change the registration under httpHandlers in your web.config to point to your new factory class.

BurningIce
I tried this out, but changing the AppRelativeVirtualPath doesn't seem to affect url's within controls (such as HyperLink) on my page.
Wallace Breza
A: 

I doesn't seem like overriding the ~/ is possible. We have decided to take a different approach and create utility methods to help manage links and paths within our multi-tenant environment.

The best option I saw to actually accomplish this is to implement IUrlResolutionService, but we were unable to make this work successfully.

Wallace Breza