5

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

4

2 回答 2

1

我就是这样做的:

 Return New WebServiceHandlerFactory().GetHandler(HttpContext.Current, "*", "/Build/WebService.asmx", HttpContext.Current.Server.MapPath(aspxToLoad))
于 2011-09-28T11:34:49.983 回答
1

有趣的想法。我不知道你可以那样使用网络表单。我们目前正在将旧的 Web 表单应用程序与IgnoreRoutes. 我一定会为您的问题添加书签;)

但也许我可以帮你解决你的问题。好的旧的HttpContext仍然存在,它只是被 MVC 包装到 mock friendlyHttpContextBase中。

您可以HttpContext使用

var context = System.Web.HttpContext.Current;

在控制器中,您必须完全指定类型以将其与名为的控制器属性区分开来HttpContext

于 2009-06-20T12:53:22.783 回答