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?
Update operations use the HTTP PATCH verb. Pass a JSON object containing the properties you want to update to the URI that represents the record. A response with a status of 204 No Content will be returned if the update is successful.
Although ASP.NET Web API is packaged with ASP.NET MVC, it is easy to add Web API to a traditional ASP.NET Web Forms application. To use Web API in a Web Forms application, there are two main steps: Add a Web API controller that derives from the ApiController class. Add a route table to the Application_Start method.
ASMX provides the ability to build web services that send messages using the Simple Object Access Protocol (SOAP). SOAP is a platform-independent and language-independent protocol for building and accessing web services.
It is based on SOAP and return data in XML form. It support only HTTP protocol. It is not open source but can be consumed by any client that understands xml.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With