我如何获得ASP.NET的WebForms路由路由的.asmx JSON调用正确? [英] How do I get ASP.NET WebForms Routing to route .asmx JSON calls properly?

查看:174
本文介绍了我如何获得ASP.NET的WebForms路由路由的.asmx JSON调用正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现在传统ASP.NET应用程序的WebForms多租户。我想要的网址,以指示正确的客户端,就像这样:

I am attempting to implement multi-tenancy in a legacy ASP.NET WebForms app. I want the URL to indicate the proper client, like so:

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

不过,我不能得到它路由的.asmx的正常。该路由规则拿起所有传入的URL就好了:

However, I cannot get it to route the .asmx's properly. This routing rule picks up all incoming urls just fine:

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

不过,我有处理的.asmx调用的问题。这里是我的IRouteHandler,下文。我得到的错误是:

But I am having issues with handling .asmx calls. Here's my IRouteHandler, below. The error I get is:

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,但由于某种原因它不工作。我设置的内容类型 - 如果我没有送这个路由完全相同的要求,正常工作

It's supposed to be JSON, but for some reason it's not working. I am setting the content-type - if I send this same exact request without routing, it works fine.

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

相关链接:

  • Getting ScriptHandlerFactory handler

推荐答案

我尽我所能,结束了失败,并WCF的.svc服务,而不是将我的所有Web服务。

I tried my best, ended up failing, and converted all my web services to WCF .svc services instead.

这篇关于我如何获得ASP.NET的WebForms路由路由的.asmx JSON调用正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆