Nat*_*mer 5 .net c# asp.net-mvc httphandler
我已经看到如何使用这样的代码向 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));
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用类似的方法来工作,但对于 Web 服务文件 (TestService.asmx)。以前的方法会引发异常,因为该页面不是从 IHttpHandler 继承的。我看过其他一些使用 WebServiceHandlerFactory 这样的代码
return new WebServiceHandlerFactory().GetHandler(context, requestType, url, pathTranslated);
Run Code Online (Sandbox Code Playgroud)
这会返回一个我需要的 IHttpHandler,但它需要传入一个 HttpContext,但作为 RequestContext 的一部分,我唯一可以访问的东西是 HttpContextBase。据我所知,我无法从中转换为 HttpContext 。
有任何想法吗?或者也许是一种不同的方式来解决它?我想要完成的是通过普通路由系统控制我的 Web 服务的 url。一个例子是希望 TestService.asmx 作为 ExampleTestService/ 出现。
有趣的想法。我不知道您可以这样使用网络表单。我们目前正在将旧的 Web 表单应用程序与IgnoreRoutes. 我一定会为你的问题添加书签;)
但也许我可以帮助你解决你的问题。好的旧的HttpContext仍然存在,它只是被 MVC 包装到模拟友好中HttpContextBase。
HttpContext您可以通过以下方式检索原始文件
var context = System.Web.HttpContext.Current;
Run Code Online (Sandbox Code Playgroud)
在控制器中,您必须完全指定类型,以将其与名为的控制器属性区分开来HttpContext