Mapping an ASMX Service Using Routing in ASP.NET MVC

I wonder if there is a way to map the ASMX service URL, as it does with pages (using the method routes.MapPageRoute()).

When I tried to do this simply by pointing MapPageRoute to my service, I get an error

The type 'MvcApplication1.Services.EchoService' is not inherited from 'System.Web.UI.Page'.

Matthias

+5
source share
3 answers

I came across this question, trying to find the answer myself, and since I figured out how to do it, I decided that I would answer it.

, , , - ASP.NET ASP.NET MVC, -, URL-. URL- , ( URL- , -).

PageRouteHandler, RouteCollection.MapPageRoute, , System.Web.Page, -. :

using System;
using System.Web;
using System.Web.Routing;
using System.Web.Services.Protocols;

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));
    }
}

- .

- :

routes.Add("RouteName", new Route("path/to/your/service", new RouteValueDictionary() { { "controller", null }, { "action", null } }, new ServiceRouteHandler("~/actualservice.asmx")));

: ( ), Html.ActionLink ( ). , , MVC , , .

, :

public static Route MapServiceRoute(this RouteCollection routes, string routeName, string url, string virtualPath)
{
    if( routes == null )
        throw new ArgumentNullException("routes");
    Route route = new Route(url, new RouteValueDictionary() { { "controller", null }, { "action", null } }, new ServiceRouteHandler(virtualPath));
    routes.Add(routeName, route);
    return route;
}

:

routes.MapServiceRoute("RouteName", "path/to/your/service", "~/actualservice.asmx");

, -, .:)

+5

, anwer, Web API?:)

EDIT: , , , , , .

+1

( ), . - -. , , .

MVC - .ASMX, .

, , , .NET.

, .

  • -, - .ASMX, - . - .ASMX. , . , !

  • - MVC , http.

:

        routes.MapRoute(
            "Lead",
            "lead/{action}.mvc",
            new { controller = "Lead" });

:

        var dict = new RouteValueDictionary
                   {
                       { "controller", null },
                       { "action", null }
                   };
        var handler = new LeadRouteHandler();
        var route = new Route("lead/MVC_General.mvc", dict, handler);
        routes.Add("Lead", route);

, "MVC_General". , , -.

  1. .

IRouteHandler:

public class LeadRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new LeadHttpHandler();
    }
}

IHttpHandler:

public class LeadHttpHandler : IHttpHandler
{

    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        // Just enough code to preserve the route json interface for tests
        var typedResult = new PsaLeadSubmissionResult();
        typedResult.Registered = false;
        typedResult.Message = new List<string>
                              {
                                  "Not Implemented"
                              };

        var jsonResult = JsonConvert.SerializeObject(typedResult);

        context.Response.Write(jsonResult);
    }
}

IHttpHandler ProcessRequest . - , , , - .ASMX, .

Global.asax. URL-, URL- , .

0

All Articles