2011-12-08 34 views
1

我有一個基於ASP.NET 4 WebForms的應用程序,並且我想要使用路由來允許多租戶,例如http://www.example.com/site/foo/Default.aspx用於名爲「foo」的客戶端,並且http://www.example.com/site/bar/Default.aspx用於客戶端名爲bar。如何使用IRouteHandler在WebForms應用程序中設置多租戶?

我得到儘可能:

// 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和其他不可編譯的資源並拋出異常。避免路由選擇的最佳方法是什麼?

回答

2

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

routes.Add(new Route("*{js}", new {[email protected]".*\.js(/.*)?", new StopRoutingHandler())); 
+0

啊,真的,我會標記爲答案。但我的問題很糟糕 - 實際上,我還需要路由其他元素(至少是asmx),並以某種方式修復它。謝謝!我發佈了另一個問題:http://stackoverflow.com/questions/8527677/how-do-i-use-asp-net-webforms-routing-to-an-asmx-scriptservice –