5

I want to use Visual Studio 2019 to create a WebService inside an existing .NET project, using C#. Searching the internet, all I could find were tutorials for older VS versions...


How can I create it, and what's the best approach for receiving POST data using Visual Studio 2019?

1
  • 3
    In exactly the same way as the previous version. You create a WCF service. The emphasis on POST is meaningless since SOAP works with POST anyway Aug 2, 2019 at 14:40

2 Answers 2

14

Considering you have your solution opened:

  • Right click on the project name (at the solution explorer), go to "Add" and than "Add new item..."

first step

  • Select "Visual C#", scroll down, select "Web Service (ASMX)" and click "Add".

second step

A file called WebService.asmx (Or the name you entered) was create on the root folder of your project. Inside, you should see that code:

<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>

This file is just used to call the code, at "~/App_Code/WebService.cs". So if you want to call it from POST / GET, you should use:

www.host.com/pathTo/projectRoot/WebService.asmx/functionName?Params=values

After opening "~/App_Code/WebService.cs", you should see something like that:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{

    public WebService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

}

Now, you can customize your code to receive and process POST / GET data.

Note that instead of using Request["param"], you should use HttpContext.Current.Request["param"];.

13
  • 2
    @ĴošħWilliard as long as the answer isn't wrong. This answer is wrong though - yes, there's a lot of documentation already and no, you don't created SOAP services with the ASMX template Aug 2, 2019 at 14:44
  • 2
    ASMX is not the most recommended since is old, but I use to integrate thousands of registers, without problems.
    – Brhaka
    Aug 2, 2019 at 14:46
  • 2
    @RyanWilson in this day and age the answer would be ASP.NET Core GRPC :P Aug 2, 2019 at 14:47
  • 4
    @PanagiotisKanavos as well as saying "They are thoroughly documented" it'd be more useful to actually link to the relevant documentation. If I'm completely new to this (and someone searching the web with terms that will find this answer probably will be) how do I know what documentation there is and where? For example, I've just tried searching the VS2019 help and it's less useful than this answer. Clearly I am searching for "the wrong thing" - but what should I be looking for? Dec 16, 2019 at 14:03
  • 3
    in case "WebService.asmx" template or ASP.NET Web Application project template is missing, use visual studio installer and add "asp.net and web tools" component
    – ASh
    Aug 25, 2020 at 14:59
5

this is how I created a project with an asmx page. create new project

create an empty on

right click for a new item

and look for webservice.asmx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.