2009-06-20 54 views
4

我已经看到如何使用这样的代码将自定义路由添加到WebForms。我可以在ASP.NET MVC站点中路由Web服务(ASMX)的URL吗?

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服务的文件,因为该页面不从的IHttpHandler继承(TestService.asmx),以前的方法抛出异常。我已经看到了使用WebServiceHandlerFactory这样

return new WebServiceHandlerFactory().GetHandler(context, requestType, url, pathTranslated); 

,像我需要返回IHttpHandler的一些其他的代码,但它需要通过一个HttpContext的,但我有机会为过的RequestContext的一部分的唯一的事情是HttpContextBase。从我可以告诉我不能转换为HttpContext。

任何想法?或者,也许有另一种方式去解决它?我试图完成的是通过正常的路由系统来控制我的Web服务的URL。一个例子是想让TestService.asmx成为ExampleTestService /。

回答

1

有趣的想法。我不知道你可以用这种方式使用Web表单。我们目前正在将旧的网页表单应用程序与IgnoreRoutes集成。我肯定会收藏你的问题;)

但也许我可以帮助你解决你的问题。好的旧HttpContext仍然存在,它只是由MVC包装成模拟友好HttpContextBase

您可以找回原来的HttpContext

var context = System.Web.HttpContext.Current; 

在控制器,你必须完全指定类型从所谓的控制器属性区分HttpContext

+0

我得到了部分成功。我可以得到我的自定义URL来调出Web服务,但尝试调用它时会失败。 – 2009-06-25 05:15:14

+0

然后你最好的机会是通过ASP.NET代码进行调试,如果失败,找出原因...... – chris166 2009-06-25 05:46:35

1

这是我要做的事:

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