NOTE: There is no MVC in this code. Pure old Web Forms and
.asmx
Web Service.
I have inherited a large scale ASP.NET Web Forms & Web Service (.asmx
) application at my new company.
Due to some need I am trying to do URL Routing for all Web Forms, which I was successfully able to do.
Now for .asmx
, routes.MapPageRoute
does not work. Based on the below article, I created an IRouteHandler
class. Here's how the code looks:
using System;
using System.Web;
using System.Web.Routing;
using System.Web.Services.Protocols;
using System.Collections.Generic;
public class ServiceRouteHandler : IRouteHandler
{
private readonly string _virtualPath;
private readonly WebServiceHandlerFactory _handlerFactory = new WebServiceHandlerFactory();
public ServiceRouteHandler(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 _handlerFactory.GetHandler(HttpContext.Current, requestContext.HttpContext.Request.HttpMethod, _virtualPath, requestContext.HttpContext.Server.MapPath(_virtualPath));
}
}
http://mikeoncode.blogspot.in/2014/09/aspnet-web-forms-routing-for-web.html
Now when I do routing via Global.asax
, it work for the root documentation file but does not work with the Web Methods inside my .asmx
files.
routes.Add("myservice", new System.Web.Routing.Route("service/sDxcdfG3SC", new System.Web.Routing.RouteValueDictionary() { { "controller", null }, { "action", null } }, new ServiceRouteHandler("~/service/myoriginal.asmx")));
routes.MapPageRoute("", "service/sDxcdfG3SC", "~/service/myoriginal.asmx");
Goal
I would like to map an .asmx
Web Method URL such as www.website.com/service/myservice.asmx/fetchdata
to a URL with obscured names in it like www.website.com/service/ldfdsfsdf/dsd3dfd3d
using .NET Routing.
How can this be done?
The article you are referencing is not to provide extensionless routing to asmx WS, is to provide routing from
server/whateverYouAre/ws.asmx
toserver/ws.asmx
(the real resource location). This allows JS use local path (current location) to invoque the asmx without worry abot where the browser are.Anyway, maybe, just maybe, you can use the article as starting point. I never do this so it just a guess:
There are 2 modes to consume your WS. If the client is using SOAP the request URL will be:
with SOAPAction http header and the SOAP XML in the POST body. Your current routing solution should work. BUT if you are consuming the WS though raw HTTP GET/POST (i.e. from a webBrowser) the url of each webMethod is:
So I think you could to provide some url routing in the form of:
and modify GetHttpHandler:
to provide the raw URL of the requested resource in the form of
/server/service/myoriginal.asmx/webMethod
.My code is write on the fly from the top of my head so just make sure _
virtualPath + "/" + requestContext.RouteData.Values["webMethod"]
create the right URL before a early rage quit ;-) Modify it if needed.With some luck; WebServiceHandlerFactory should be able to locate the physical resource and, inspecting the raw URL, execute the webMethod by its name.
Not a direct answer but something worth considering.
You could possibly upgrade your ASMX service to a WCF service with compatible contract so that you don't have to upgrade your clients at all.
With that, you could use a known technique to dynamically route WCF services. Since this known technique involves an arbitrary address for your service, you can bind the WCF service to a
.......foo.asmx
endpoint address so that your clients not only don't upgrade their client proxies but also they have exactly the same endpoint address.In other words, to a client, your dynamically routed WCF service looks 1-1 identical as your old ASMX service.
We've succesfully used this technique over couple of last years to upgrade most of all old ASMXes to WCFs, preserving client proxies in many cases.
All technical details are documented in my blog entry
http://www.wiktorzychla.com/2014/08/dynamic-wcf-routing-and-easy-upgrading.html
It is slightly more tricky to do this with routing than in the article you posted because you don't want the incoming URL to have a query string parameter and it looks like the
WebServiceHandler
won't call the method without an?op=Method
parameter.So, there are a few parts to this:
ServiceRoute
) to do URL rewriting to add the?op=Method
parameterIRouteHandler
to wrap theWebServiceHandlerFactory
that calls the web service.ServiceRoute
ServiceHandler
RouteCollectionExtensions
Usage
You can either use
MapServiceRoute
to add the routes one at a time (with an optional name):Alternatively, you can call
MapServiceRoutes
to map a batch of your web service routes at once:References:
If the site is hosted in IIS you could use the IIS URL Rewrite to create a friendly url and redirect it to your internal path as per creating-rewrite-rules-for-the-url-rewrite-module. Each of these rules are stored in the web.config so can be managed within your development environment
The drawback (or benefit depending upon your usage) is that the original path would still be accessible