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?

0

1 Answer 1

2

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

routes.Add(new Route("*{js}", new {js=@".*\.js(/.*)?", new StopRoutingHandler()));
Sign up to request clarification or add additional context in comments.

1 Comment

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: stackoverflow.com/questions/8527677/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.