Brh*_*aka 3 .net c# web-services visual-studio visual-studio-2019
我想使用 Visual Studio 2019 在现有的 .NET 项目中创建一个 WebService,使用 C#。在互联网上搜索,我能找到的只是旧版 VS 版本的教程......
如何创建它,使用 Visual Studio 2019 接收 POST 数据的最佳方法是什么?
Brh*_*aka 10
考虑到您已打开解决方案:
在项目的根文件夹中创建了一个名为WebService.asmx(或您输入的名称)的文件。在里面,您应该看到该代码:
<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>
Run Code Online (Sandbox Code Playgroud)
此文件仅用于调用代码,位于"~/App_Code/WebService.cs"。所以如果你想从POST / GET调用它,你应该使用:
www.host.com/pathTo/projectRoot/WebService.asmx/functionName?Params=values
打开"~/App_Code/WebService.cs" 后,您应该会看到如下内容:
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";
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以自定义代码以接收和处理POST / GET数据。
请注意Request["param"],您应该使用HttpContext.Current.Request["param"];.