2011-12-15 53 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模式。我注意到你发布了另一个关于多租户的问题。

也许你可以查看他们的代码以获得参考。