如何使用 IRouteHandler 在 WebForms 应用程序中设置多租户?
我有一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
StopRoutingHandler() 忽略对某些文件的请求
。
You can use the
StopRoutingHandler()
to ignore requests for certain files.