Saturday, November 7, 2009

Path resolution through IUrlResolutionService

When developing for a web application you often come accross the scenario where you need to generate a path to a different page and all you have is a app relative path. To resolve this path to a usable client path i have often used different methods to resolve a path.

When working on a project at work i saw a colleage use the following interface
IUrlResolutionService

I had never seen that one before and i looked it up
MSDN page

Quote from the page:
Defines a service implemented by objects to resolve relative URLs based on contextual information.
....

This interface defines the service that is used to resolve URL paths. The IUrlResolutionService interface is used for resolving relative paths and paths that contain the ~ operator. Server controls that reference resources can define the paths to the resources through the ~ operator, which represents the root of the current application. A path containing the ~ operator will not work if passed to the browser. The server control must convert the path to an absolute or relative path before passing it to the browser.

Control implements this interface. A control that derives from Control could override this implementation to provide customized resolution of URLs.

This interface is implemented by the Control, among other classes, which means that Controls like Page, etc all have a method to resolve the path.
you can pass along controls or the page deeper into your code using this interface to resolve app relative paths for you. For example "~/foo/bar.aspx" would get resolved properly by the control to a path the client can use based on the current context of the control/page.

I myself are using this more often these days it seems a nice way to do it.

No comments:

Post a Comment