如何使用IRouteHandler在WebForms应用程序中设置多租户?

时间:2011-12-08 22:10:48

标签: asp.net webforms asp.net-mvc-routing multi-tenant

我有一个基于ASP.NET 4 WebForms的应用程序,我想使用路由来允许多租户,这样http://www.example.com/site/foo/Default.aspx用于名为“foo”的客户端,而http://www.example.com/site/bar/Default.aspx用于客户名为bar。

我得到了:

// 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));
        }
    }
}

它似乎适用于最初的.aspx页面。但路由正在拾取.js和其他不可编译的资源并抛出异常。避免路由这些的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

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

routes.Add(new Route("*{js}", new {js=@".*\.js(/.*)?", new StopRoutingHandler()));