我已经看到了如何使用这样的代码向 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/ 出现。