gpt4 book ai didi

c# - 我可以在 ASP.NET MVC 站点中路由 Web 服务 (ASMX) 的 URL 吗?

转载 作者:行者123 更新时间:2023-11-30 12:37:27 26 4
gpt4 key购买 nike

我已经了解了如何使用类似这样的代码向 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。

有什么想法吗?或者换一种方式去做?我想要完成的是通过正常的路由系统控制我的网络服务的 url。一个示例是希望 TestService.asmx 作为 ExampleTestService/出现。

最佳答案

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

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

您可以使用

检索原始的 HttpContext
var context = System.Web.HttpContext.Current;

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

关于c# - 我可以在 ASP.NET MVC 站点中路由 Web 服务 (ASMX) 的 URL 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1020875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com