Home > Articles > Programming > ASP .NET

  • Print
  • + Share This
From the author of

Creating a Web Service

Creating a web service sounds like something that should be an article on its own, but ASP.NET makes it so easy that I can fit it within this short section. To create the web service, right-click the name of the site and select Add New Item. In the Add New Item dialog box, choose Web Services in the Visual Studio Installed Templates section. I named my file MyAtlasWebService.asmx, but you can choose whatever name you like; just keep in mind that there are later references to the file.

Once the file has been created, you’ll see that a method called HelloWorld has been created in the C# class. We’re going to rename this method to GetTime and add a string parameter called username. We’ll also remove the code from the method and add a return value that responds with the username and the current time (see Listing 1).

Listing 1 A custom ASP.NET web service (MyAtlasWebService.js).

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyAtlasWebService : System.Web.Services.WebService
{

  public MyAtlasWebService ()
  {

  }

  [WebMethod]
  public string GetTime(string username)
  {
    return "Hello " + username + ", the current time is: " + DateTime.Now;
  }

}

That’s it! Now we’re ready to make requests to the web service and its GetTime method.

  • + Share This
  • 🔖 Save To Your Account