C# 我可以在ASP.NET MVC站点中路由web服务(ASMX)的url吗?

C# 我可以在ASP.NET MVC站点中路由web服务(ASMX)的url吗?,c#,.net,asp.net-mvc,httphandler,C#,.net,Asp.net Mvc,Httphandler,我已经了解了如何使用如下代码向WebForms添加自定义路由 public class WebFormsRouteHandler : IRouteHandler { public string VirtualPath { get; set; } public IHttpHandler GetHttpHandler(RequestContext requestContext) { // Compiles ASPX (if needed) and insta

我已经了解了如何使用如下代码向WebForms添加自定义路由

public class WebFormsRouteHandler : IRouteHandler
{
    public string VirtualPath { get; set; }

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        // Compiles ASPX (if needed) and instantiates the web form
        return (IHttpHandler) BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof (IHttpHandler));
    }
}
我正试图让类似的东西工作,但对于web服务文件(TestService.asmx.),前面的方法会引发异常,因为页面没有从IHttpHandler继承。我见过其他一些使用WebServiceHandlerFactory的代码,如下所示

return new WebServiceHandlerFactory().GetHandler(context, requestType, url, pathTranslated);
它返回一个我需要的IHttpHandler,但它需要传入一个HttpContext,但作为RequestContext的一部分,我唯一可以访问的是HttpContextBase。据我所知,我无法将其转换为HttpContext


有什么想法吗?或者换一种方式?我试图实现的是通过正常的路由系统控制我的web服务的URL。例如,希望TestService.asmx成为ExampleTestService/

有趣的想法。我不知道你可以这样使用网络表单。我们目前正在将旧的web表单应用程序与
IgnoreRoutes
集成。我肯定会把你的问题加入书签;)

但也许我能帮你解决你的问题。好的
HttpContext
仍然存在,它只是被MVC包装到模拟友好的
HttpContextBase

您可以使用

var context = System.Web.HttpContext.Current;
在控制器中,您必须完全指定类型,以将其与名为
HttpContext

的控制器属性区分开来。我就是这样做的:

 Return New WebServiceHandlerFactory().GetHandler(HttpContext.Current, "*", "/Build/WebService.asmx", HttpContext.Current.Server.MapPath(aspxToLoad))

我在这方面取得了部分成功。我可以获取自定义url以启动web服务,但在尝试调用它时失败。然后,您最好的机会是通过ASP.NET代码进行调试,直到if失败并找出原因。。。