Home > Articles > Programming > ASP .NET

Like this article? We recommend ๏”€

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.