如何让 ASP.NET WebForms 路由正确路由 .asmx JSON 调用?

2024-04-05

我正在尝试在旧版 ASP.NET WebForms 应用程序中实现多租户。我希望 URL 能够指示正确的客户端,如下所示:

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

相关链接:

  • 获取 ScriptHandlerFactory 处理程序 https://stackoverflow.com/questions/6865728/getting-scripthandlerfactory-handler

开源http://www.teamlab.com http://www.teamlab.com项目是使用 ASP.NET Webforms 构建的,并使用多租户/saas 模型。我注意到您发布了另一个询问多租户的问题。

也许你可以研究一下他们的代码以获取参考想法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何让 ASP.NET WebForms 路由正确路由 .asmx JSON 调用? 的相关文章

随机推荐