One of the applications using our Web API site can only consume an ASMX web service. I recently deployed the Web API project with the web service to the development server and tried testing the web service using the auto generated web page.
I enabled HttpGet
and HttpPost
in the system.web
-> webService
-> protocols
web.config section in order to use the auto-generated web page for testing. When browsing to the method I want to test, the URL is in the following format:
https://mydomain.dev.com/MyApp/MyService.asmx?op=MyMethod
When I click the Invoke button, I get the following message:
No such host is known
The URL in the response is in the follow format:
https://mydomain.dev.com/MyApp/MyService.asmx/MyMethod
How do I configure to the route to allow me to use the auto-generated ASMX page for testing with HttpGet
and HttpPost
protocols enabled?
In order to use the ASMX web service automated test harness, I had to create a custom route handler which implements the IRouteHander
interface to map the HTTP POST to the appropriate web service. The custom IRouteHandler
will return the correct IHttpHandler
for web services.
WebServiceRouteHandler Route Handler
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));
}
}
RegisterRoutes method in RouteConfig.cs
routes.Add("MyWebServiceRoute", new Route(
"path/to/service/method",
new RouteValueDictionary() { { "controller", null }, { "action", null } }, new WebServiceRouteHandler("~/MyWebService.asmx")));
My implementation is based on the following blog post: Creating a route for an .asmx Web Services with ASP.NET Routing