Asp.Net 3.5 Routing in Webservice?

I was looking for a way to route http://www.example.com/WebService.asmx at http://www.example.com/service/ using only the ASP.NET 3.5 routing infrastructure without the need to configure an IIS server.

So far I have done what most of the lessons told me, added a link to the routing assembly, configured the material in the web.config file, added it to Global.asax :

protected void Application_Start(object sender, EventArgs e) { RouteCollection routes = RouteTable.Routes; routes.Add( "WebService", new Route("service/{*Action}", new WebServiceRouteHandler()) ); } 

... created this class:

 public class WebServiceRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { // What now? } } 

... and the problem is there, I do not know what to do . The tutorials and manuals I read use page routing, not web services. Is it possible?

Ps : the route handler works, I can visit / service / and it throws a NotImplementedException, which I left in the GetHttpHandler method.

+7
web-services url-rewriting routing
source share
3 answers

I just thought that I would end this question with a slightly more detailed solution based on the answer from Markives, which worked for me.

Firstly, it is a route handler class that translates a virtual directory into your WebService as its constructor parameter.

 public class WebServiceRouteHandler : IRouteHandler { private string _VirtualPath; public WebServiceRouteHandler(string virtualPath) { _VirtualPath = virtualPath; } public IHttpHandler GetHttpHandler(RequestContext requestContext) { return new WebServiceHandlerFactory().GetHandler(HttpContext.Current, "*", _VirtualPath, HttpContext.Current.Server.MapPath(_VirtualPath)); } } 

and the actual use of this class in the Global.asax routing bit

 routes.Add("SOAP", new Route("soap", new WebServiceRouteHandler("~/Services/SoapQuery.asmx"))); 
+8
source share

This is for those who want to do this. It was very difficult for me to find information.

In the method GetHttpHandler(byVal requestContext as RequestContext) as IHttpHandler Implements IRouteHandler.GetHttpHandler (my version is higher)

This is for Webforms 3.5, by the way (mine in VB).

You cannot use the usual BuildManager.CreateInstanceFromVirtualPath () method to call your web service only for those things that implement iHttpHandler, which is missing .asmx. Instead, you need to:

 Return New WebServiceHandlerFactory().GetHandler( HttpContext.Current, "*", "/VirtualPathTo/myWebService.asmx", HttpContext.Current.Server.MapPath("/VirtualPathTo/MyWebService.aspx")) 

The MSDN documentation says that the third parameter should be RawURL, passing HttpContext.Current.Request.RawURL does not work, but passing the virtual path to the .asmx file instead works fine.

I use this functionality so that my web service can be called by any website configured anyway (even a virtual directory) that points (in IIS) to my application, which can call the application web service using something something like " http: // url / virtualdirectory / anythingelse / WebService ", and routing will always route this to my .asmx file.

+2
source share

You need to return an object that implements IHttpHandler that takes care of your request.

You can check out this article on how to implement a web service using this interface: http://mikehadlow.blogspot.com/2007/03/writing-raw-web-service-using.html

But this is probably closer to what you want http://forums.asp.net/p/1013552/1357951.aspx (There is a link, but it requires registration, so I have not tested)

+1
source share

All Articles