Asp.Net 3.5路由到Web服务?

Dio*_*mes 7 asp.net routing web-services url-rewriting asp.net-3.5

我一直在寻找一种方式来路由http://www.example.com/WebService.asmxhttp://www.example.com/service/仅使用ASP.NET 3.5路由架构,而无需配置IIS服务器.

到目前为止,我已经完成了大多数教程告诉我的内容,添加了对路由程序集的引用,在web.config中配置了东西,将其添加到Global.asax:

protected void Application_Start(object sender, EventArgs e)
{
    RouteCollection routes = RouteTable.Routes;

    routes.Add(
        "WebService",
        new Route("service/{*Action}", new WebServiceRouteHandler())
    );
}
Run Code Online (Sandbox Code Playgroud)

...创建了这个类:

public class WebServiceRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        // What now?
    }
}
Run Code Online (Sandbox Code Playgroud)

......问题就在那里,我不知道该怎么做.我读过的教程和指南使用的是页面路由,而不是web服务.这甚至可能吗?

Ps:路由处理程序正在工作,我可以访问/ service /抛出我在GetHttpHandler方法中留下的NotImplementedException.

小智 8

我想我会根据Markives为我提供的答案提供更详细的解决方案来解决这个问题.

首先,这里是路由处理程序类,它将虚拟目录作为构造函数参数传递给WebService.

public class WebServiceRouteHandler : IRouteHandler
{
    private string _VirtualPath;

    public WebServiceRouteHandler(string virtualPath)
    {
        _VirtualPath = virtualPath;
    }

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new WebServiceHandlerFactory().GetHandler(HttpContext.Current, 
            "*", 
            _VirtualPath, 
            HttpContext.Current.Server.MapPath(_VirtualPath));
    }
}
Run Code Online (Sandbox Code Playgroud)

以及Global.asax的routey位中此类的实际用法

routes.Add("SOAP",
    new Route("soap", new WebServiceRouteHandler("~/Services/SoapQuery.asmx")));
Run Code Online (Sandbox Code Playgroud)

  • 关于如何映射方法的任何想法?所以代替/Services/SoapQuery.asmx/HelloWorld,我希望路径为/ Services/SoapQuery/HelloWorld (2认同)