I remembered that I did some research into asp.net mvc apps a bit ago and remembered that they used routing so I thought I'd be able to rip it directly off that (or use the mvc libraries). After a little digging around, routing is available to asp.net 3.5 sp1 (which is azure's current platform) just by using the standard libraries. Actually Michael Kennedy has already posted about azure & routing perfectly - http://www.michaelckennedy.net/blog/2009/05/27/ASPNETRoutingInWindowsAzureUsingWebForms.aspx.
The crux:
- Reference System.Web.Routing and System.Web.Abstractions
- Create a Global.asax in your web role
- Modify your Global.asax like so
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
var routeHandler = new WebFormRouteHandler<Page>("~/Test.aspx");
routes.Add(new Route("evidence/{environment}", routeHandler));
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
var routeHandler = new WebFormRouteHandler<Page>("~/Test.aspx");routes.Add(new Route("evidence/{environment}", routeHandler));
}
- Create the WebFormRouteHandler
using System.Web;
using System.Web.Compilation;
using System.Web.Routing;
namespace SuperTravio
{
public class WebFormRouteHandler<T> : IRouteHandler where T : IHttpHandler, new()
{
public string VirtualPath { get; set; }
public WebFormRouteHandler(string virtualPath)
{
this.VirtualPath = virtualPath;
}
#region IRouteHandler Members
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
foreach (var aux in requestContext.RouteData.Values)
{
HttpContext.Current.Items[aux.Key] = aux.Value;
}
return (VirtualPath != null)
? (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(T))
: new T();
}
#endregion
}
}
- Create a Url routing handler that overrides IIS7's default action
using System.Web;
using System.Web.Routing;
namespace Supertravio
{
public class RoutingHandler : UrlRoutingHandler
{
protected override void VerifyAndProcessRequest(IHttpHandler httpHandler, HttpContextBase httpContext)
{
}
}
}
- Update your web.config to use the handler (from Michael's post)
<system.webserver>
<modules runallmanagedmodulesforallrequests="true">
...
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,
System.Web.Routing, Version=3.5.0.0,
Culture=neutral,
PublicKeyToken=31BF3856AD364E35">
</add>
<handlers>
<add name="UrlRoutingHandler" precondition="integratedMode" verb="*" path="UrlRouting.axd" type="Supertravio.RoutingHandler, Supertravio">
</add>
</handlers>
</modules></system.webserver>
You're done!
The only thing now is accessing the routed parts. So in my example (new Route("evidence/{environment}") I would get the parts from my page using (string)HttpContext.Current.Items["environment"].
No comments:
Post a Comment