[英]ASMX service in a ASP.NET WEB API project
使用我們的Web API站點的應用程序之一只能使用ASMX Web服務。 我最近將帶有Web服務的Web API項目部署到了開發服務器,並嘗試使用自動生成的網頁測試Web服務。
為了使用自動生成的網頁進行測試,我在system.web
> webService
> protocols
web.config部分中啟用了HttpGet
和HttpPost
。 瀏覽到我要測試的方法時,URL的格式如下:
https://mydomain.dev.com/MyApp/MyService.asmx?op=MyMethod
當我單擊“調用”按鈕時,出現以下消息:
尚無此類主機
響應中的URL格式如下:
https://mydomain.dev.com/MyApp/MyService.asmx/MyMethod
如何配置路由以允許我使用自動生成的ASMX頁面進行啟用HttpGet
和HttpPost
協議的測試?
我不確定,但是我認為您需要在web.config中為Web服務啟用HttpGet
和HttpPost
協議。 您可以參考以下文檔 :
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
<system.web>
更新資料
您必須確保有一個用於asmx文件的httpHandler,並且未被用於Web api的httpHandler覆蓋。 然后確保路由未由您的webapi路由器映射,例如:
routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");
為了使用ASMX Web服務自動化測試工具,我必須創建一個自定義路由處理程序,該處理程序實現IRouteHander
接口以將HTTP POST映射到適當的Web服務。 自定義IRouteHandler
將為Web服務返回正確的IHttpHandler
。
WebServiceRouteHandler路由處理程序
public class WebServiceRouteHandler : IRouteHandler
{
private readonly string _virtualPath;
private readonly WebServiceHandlerFactory _webServiceHandlerFactory = new WebServiceHandlerFactory();
public WebServiceRouteHandler(string virtualPath)
{
if (virtualPath == null) throw new ArgumentNullException("virtualPath");
if (!virtualPath.StartsWith("~/")) throw new ArgumentException("Virtual path must start with ~/", "virtualPath");
_virtualPath = virtualPath;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
//Note: can't pass requestContext.HttpContext as the first parameter because that's type HttpContextBase, while GetHandler wants HttpContext.
return _webServiceHandlerFactory.GetHandler(HttpContext.Current, requestContext.HttpContext.Request.HttpMethod, _virtualPath, requestContext.HttpContext.Server.MapPath(_virtualPath));
}
}
RouteConfig.cs中的RegisterRoutes方法
routes.Add("MyWebServiceRoute", new Route(
"path/to/service/method",
new RouteValueDictionary() { { "controller", null }, { "action", null } }, new WebServiceRouteHandler("~/MyWebService.asmx")));
我的實現基於以下博客文章: 使用ASP.NET路由為.asmx Web服務創建路由
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.