2011-12-15 61 views
3

我正試圖在傳統的ASP.NET WebForms應用程序中實現多租戶。我想要的URL指示正確的客戶端,如下所示:如何獲取ASP.NET WebForms路由以正確路由.asmx JSON調用?

http://example.com/client_name/Default.aspx 
http://example.com/client_name/MyWebService.asmx 

但是,我不能讓它正確路由.asmx的。這個路由規則拾取所有傳入的url就好了:

routes.Add("ClientSelector", new System.Web.Routing.Route 
(
    "{client}/{*path}", 
    routeHandler: new ClientRoute() 
)); 

但是我在處理.asmx調用時遇到問題。下面是我的IRouteHandler。我得到的錯誤是:

A first chance exception of type 'System.Web.Services.Protocols.SoapException' occurred in System.Web.Services.dll 

Additional information: Unable to handle request without a valid action parameter. Please supply a valid soap action. 

它應該是JSON,但由於某種原因它不工作。我設置的內容類型 - 如果我發送相同的確切請求沒有路由,它工作正常。

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

      if (virtualPath.EndsWith(".aspx")) 
       return (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page)); 
      else 
      { 
       var asmxPos = virtualPath.IndexOf(".asmx", StringComparison.OrdinalIgnoreCase); 
       if (asmxPos >= 0) 
       { 
        // What goes here? This isn't working... 
        var asmxOnlyVirtualPath = virtualPath.Substring(0, asmxPos + 5); 
        return new System.Web.Services.Protocols.WebServiceHandlerFactory().GetHandler(
         HttpContext.Current, HttpContext.Current.Request.HttpMethod, asmxOnlyVirtualPath, HttpContext.Current.Server.MapPath(asmxOnlyVirtualPath)); 
       } 
       else 
        return new StaticRoute(); 
      } 
     } 
    } 
} 

相關鏈接:

+0

會發生什麼情況,被註釋掉的代碼看起來是否正確,是否會出現錯誤? – 2011-12-15 23:00:28

回答

0

我盡了最大的努力,最終失敗了,並將我的所有Web服務轉換爲WCF .svc服務。

1

開源http://www.teamlab.com項目是建立與ASP.NET Web表單,並使用多租戶/ SaaS模式。我注意到你發佈了另一個關於多租戶的問題。

也許你可以查看他們的代碼以獲得參考。