views:

174

answers:

2

I was looking for a way to route http://www.example.com/WebService.asmx to http://www.example.com/service/ using only the ASP.NET 3.5 Routing framework without needing to configure the IIS server.

Until now I have done what most tutorials told me, added a reference to the routing assembly, configured stuff in the web.config, added this to the 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 right there, I don't know what to do. The tutorials and guides I've read use routing for pages, not webservices. Is this even possible?

Ps: The route handler is working, I can visit /service/ and it throws the NotImplementedException I left in the GetHttpHandler method.

+1  A: 

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 webservice using that 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 didnt test)

Cine
+1  A: 

This is for anyone else who wants to do the above. I found it incredibly difficult to find the information.

In the getHttpHandler(byVal requestContext as RequestContext) as IHttpHandler Implements IRouteHandler.GetHttpHandler method (my version of the above)

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

You cant use the usual BuildManager.CreateInstanceFromVirtualPath() method to invoke your web servce that's only for things that implement iHttpHandler which .asmx doesn't. 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 3rd parameter should be the RawURL, passing HttpContext.Current.Request.RawURL doesn't work, but passing the virtual path to the .asmx file instead works great.

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

Markive