我想使用 Visual Studio 2019 使用 C# 在现有 .NET 项目中创建 WebService。在互联网上搜索,我能找到的只是旧 VS 版本的教程......
如何创建它?使用 Visual Studio 2019 接收 POST 数据的最佳方法是什么?
考虑到您已打开解决方案:
- 右键单击项目名称(在解决方案资源管理器中),转到"Add"然后“添加新项目...”
- Select 《视觉C#》,向下滚动,选择“网络服务(ASMX)”然后单击"Add".
一个名为Web服务.asmx(或您输入的名称)是在项目的根文件夹中创建的。在里面,您应该看到该代码:
<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>
这个文件只是用来调用代码的,位于“~/App_Code/WebService.cs”。所以如果你想从发布/获取,你应该使用:
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";
}
}
现在,您可以自定义代码来接收和处理发布/获取 data.
请注意,不要使用Request["param"]
,你应该使用HttpContext.Current.Request["param"];
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)