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

发布于 2024-12-20 07:43:08 字数 1841 浏览 2 评论 0原文

我有一个基于 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 和其他不可编译的资源并抛出异常。避免路由的最佳方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洛阳烟雨空心柳 2024-12-27 07:43:08

可以使用StopRoutingHandler() 忽略对某些文件的请求

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

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

routes.Add(new Route("*{js}", new {js=@".*\.js(/.*)?", new StopRoutingHandler()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文