1

Looking to convert some .asmx webservices to REST. I am not interested in WCF style, rather a more simple way if that makes sense. All my operations will be CRUD. I have read about REST and HTTPWebRequest and HttpWebResponseDoes anyone know the simpleset way to do this? My asmx code is below.. A quick REST service of this would be appreciated if anyone can help. Thank You!

    [WebMethod]
    public Products[] GetProducts()
    {
        ProductDA dataAccess = new ProductDA();
        List<Product> obj = new List<Product>();
        obj = dataAccess.GetProducts();
        return obj.ToArray();

    }
midnightCoder
  • 65
  • 1
  • 11

3 Answers3

3

Have you checked out the new ASP.NET WebAPI? Sounds like it would be a good choice if you're able to target .NET 4 as a platform. You should be able to use your existing service implementation with virtually no changes.

MattDavey
  • 8,395
  • 3
  • 29
  • 54
  • Its not a MVC project but will take a look. I am hoping to use the existing service implementation with minimal changes as you stated so hopefully I can find the code snippet there. – midnightCoder Mar 15 '12 at 19:21
  • If anyone has a REST code example based on my asmx service above it would be appreciated. I'm cranking through WEB API now but cont see what I'm looking for as of yet.. thanks all.. – midnightCoder Mar 15 '12 at 20:18
  • If anyone has a REST code example based on my asmx service above it would be appreciated. I'm cranking through WEB API now but cont see what I'm looking for as of yet.. thanks all.. – midnightCoder Mar 15 '12 at 20:43
2

I would look at the new Web API, which is currently part of the ASP.NET MVC 4 beta (it has a go-live license). Here is Scott Guthrie demonstrating how to use it:

http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2364

I should note that you do not have to convert your web site to MVC in order to use this.

mgnoonan
  • 6,860
  • 5
  • 22
  • 26
1

Asmx file can also be used for rest api creation (Which is not the recommended approach).

This can be achieved by the below code snippet.

[ScriptService]
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Randezvous : WebService
{
    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public void getUnitPersonels(string user, string pass, decimal unitNo)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        #region ..:: Kullanıcı şİfre Kontrol ::..
        if (!(unit == "xxx" && pass == "yyy"))
        {

            string msg = "User or pass is wrong.";
            Context.Response.Write(serializer.Serialize(msg));
            return;
        }
        #endregion

        List<Personels> personels = _units.getUnitPersonels(unitNo);

        string jsonString = serializer.Serialize(personels);
        Context.Response.Write(jsonString);
    }
}

You can test this code in c# with the code that is shown below:

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var builder = new UriBuilder("http://localhost:18511/Randezvous.asmx/getUnitPersonels");
    var query = HttpUtility.ParseQueryString(builder.Query);
    query["unitNo"] = "0";
    builder.Query = query.ToString();
    string url = builder.ToString();

    var result = Task.FromResult(client.GetAsync(url).Result).Result.Content;
    var resultJson = result.ReadAsStringAsync().Result;

}
Serhat Oz
  • 720
  • 7
  • 12