简体   繁体   中英

Upgrading a web service from asmx to webAPI

I'm building a web app that currently uses traditional .asmx web services and I'm looking to upgrade these to WebAPI. I've looked around on the web but I'm looking for the easiest/fastest way to do this upgrade. The web services currently look somewhat like this:

using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class SomeWebServiceName : System.Web.Services.WebService
{
    SomeObject TheObject = new SomeObject;

    [WebMethod(EnableSession = true)]
    public string GetSomeData(string Param1, string Param2)
    {
         return TheObject.HandleRequest(Param1, Param2);
    }

    [WebMethod(EnableSession = true)]
    public string GetSomeMoreData(string ParamA)
    {
         return TheObject.HandleAnotherRequest(ParamA);
    }
}

At their simplest level, they instantiate an object and then the web methods of the web service call some method on that object to handle the requests.

On the client, I use jquery with .ajax() like this:

$.ajax({
    url: "../WebServices/SomeWebServiceName.asmx/GetSomeData",
    data: AjaxData, ....});

I want to remove any reference to .asmx and upgrade the entire application to WebAPI. What's the simplest way to do that with the code above?

As @Venkat said: "It's not easy to answer directly"; I mean, without considerable amount of manual coding; but making some assumptions I can recommend to implement a controller like:

public class SomeWebServiceNameController : ApiController
{
    SomeObject TheObject = new SomeObject();

    public string GetSomeData(string Param1, string Param2)
    {
        return TheObject.HandleRequest(Param1, Param2);
    }

    public string GetSomeMoreData(string ParamA)
    {
        return TheObject.HandleAnotherRequest(ParamA);
    }

    [HttpPost]
    public string PostSomeMoreData([FromBody]string ParamA)
    {
        return TheObject.HandleAnotherRequest(ParamA);
    }
}

You also should register routes (usually in "WebApiConfig.cs"):

public static void Register(HttpConfiguration config)
{
    config.Routes.MapHttpRoute(
        name: "NumberedParametersAPI",
        routeTemplate: "WebServices/{controller}/{action}/{Param1}/{Param2}"
    );
    config.Routes.MapHttpRoute(
        name: "CharacterizedParametersAPI",
        routeTemplate: "WebServices/{controller}/{action}/{ParamA}",
        defaults: new { ParamA = RouteParameter.Optional }
    );
}

I included the last method "PostSomeMoreData" to be consistent with the client call that you specified in your question (jQuery ajax method call). But keep in mind that primitive parameters in POST method of WebAPI are little bit confusing. Please read these links:

http://www.intstrings.com/ramivemula/articles/testing-asp-net-web-apiget-post-put-delete-using-fiddler/

http://yassershaikh.com/how-to-call-web-api-method-using-jquery-ajax-in-asp-net-mvc-4/

http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

Create a class below, place it under Controllers/Api folder, add the following WebApiConfig under App_Start

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{action}/{id}",
            new { id = RouteParameter.Optional, action = RouteParameter.Optional });
    }
}

Controller Codee

public class SomeWebServiceNameController : ApiController
{
    SomeObject TheObject = new SomeObject;

    [HttpGet]
    public string GetSomeData(string Param1, string Param2)
    {
         return TheObject.HandleRequest(Param1, Param2);
    }

    [HttpGet]
    public string GetSomeMoreData(string ParamA)
    {
         return TheObject.HandleAnotherRequest(ParamA);
    }
}

Add the following call in global.asax.cs under application_start to register the route:

WebApiConfig.Register(GlobalConfiguration.Configuration);

This is a very simple explanation, and you would probably want to return an object instead of string, but that should do it.

It's not easy to answer directly. First we need to validate your application architecture will really support HTTP/REST based calls. My request is before we migrate to Web API, the purpose needs to be clear.

I am not sure of the the easiest way, but the hard way is to migrate manually. If you web services have class files behind asmx methods or you have an abstraction to your services, we can easily reuse the abstraction to upgrade to Web API.

Please check the below link to get an better idea about Web API: http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-and-WCF-REST-and-Web-Service.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM