1

I have an ASP.NET 4 WebForms-based application, and I want to use routing to allow multi-tenancy, such that http://www.example.com/site/foo/Default.aspx is for the client named "foo" and http://www.example.com/site/bar/Default.aspx is for the client named bar.

I got as far as:

// Global.asax in Application_Start
routes.Add("ClientSelector", new System.Web.Routing.Route
(
   "site/{client}/{*path}",
   new Lcmp.Web.Configuration.ClientRoute()
));


public class ClientRoute : System.Web.Routing.IRouteHandler
{
    private string m_Path;
    private string m_Client;

    public ClientRoute() { }

    public bool IsReusable
    {
        get { return true; }
    }

    public IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
    {
        this.m_Path = (string)requestContext.RouteData.Values["path"];
        this.m_Client = (string)requestContext.RouteData.Values["client"];

        string virtualPath = "~/" + this.m_Path;

        bool shouldValidate = false;

        if (shouldValidate && !UrlAuthorizationModule.CheckUrlAccessForPrincipal(
            virtualPath, requestContext.HttpContext.User,
                          requestContext.HttpContext.Request.HttpMethod))
        {
            requestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            requestContext.HttpContext.Response.End();
            return null;
        }
        else
        {
            HttpContext.Current.RewritePath(virtualPath);
            HttpContext.Current.Items.Add("Client", this.m_Client);
            return (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page));
        }
    }
}

and it seems to work for the initial .aspx page. But the routing is picking up .js and other non-compilable resources and throwing exceptions. What is the best way to avoid routing those?

Scott Stafford
  • 43,764
  • 28
  • 129
  • 177

1 Answers1

2

You can use the StopRoutingHandler() to ignore requests for certain files.

routes.Add(new Route("*{js}", new {js=@".*\.js(/.*)?", new StopRoutingHandler()));
Community
  • 1
  • 1
George Stocker
  • 57,289
  • 29
  • 176
  • 237
  • Ah, true, and I'll mark as answer. But my question was bad - actually, I need to route those other elements too (at least asmx), and fix that somehow. Thanks! I posted another question: http://stackoverflow.com/questions/8527677/how-do-i-use-asp-net-webforms-routing-to-an-asmx-scriptservice – Scott Stafford Dec 16 '11 at 02:09