【问题标题】:Using VS2019 to create a WebService使用VS2019创建WebService
【发布时间】:2019-08-02 14:35:57
【问题描述】:

我想使用 Visual Studio 2019 在现有的 .NET 项目中使用 C# 创建 WebService。在网上搜索,我只能找到旧 VS 版本的教程......


如何创建它,以及使用 Visual Studio 2019 接收 POST 数据的最佳方法是什么?

【问题讨论】:

  • 与之前的版本完全一样。创建 WCF 服务。强调 POST 毫无意义,因为 SOAP 无论如何都与 POST 一起工作

标签: c# .net visual-studio web-services visual-studio-2019


【解决方案1】:

考虑到您的解决方案已打开:

  • 右键单击项目名称(在解决方案资源管理器中),转到“添加”,然后“添加新项目...”

  • 选择“Visual C#”,向下滚动,选择“Web Service (ASMX)”,然后点击“添加”

一个名为 WebService.asmx 的文件(或您输入的名称)已在项目的根文件夹中创建。在里面,你应该会看到这段代码:

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

这个文件只是用来调用代码,在"~/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";
    }

}

现在,您可以自定义代码以接收和处理 POST / GET 数据。

请注意,您应该使用HttpContext.Current.Request["param"];,而不是Request["param"]

【讨论】:

  • @ĴošħWilliard 只要答案没有错。虽然这个答案是错误的 - 是的,已经有 很多 文档,不,您没有使用 ASMX 模板创建 SOAP 服务
  • ASMX 不推荐,因为老了,但是我用集成几千个寄存器,没有问题。
  • @RyanWilson 在这个时代,答案将是 ASP.NET Core GRPC:P
  • @PanagiotisKanavos 以及说“他们已彻底记录”,实际链接到相关文档会更有用。如果我对此完全陌生(并且有人在网上搜索可能会找到此答案的术语)我怎么知道那里有哪些文档以及在哪里?例如,我刚刚尝试搜索 VS2019 帮助,它没有这个答案有用。显然我在寻找“错误的东西”——但我应该应该寻找什么?
  • 如果“WebService.asmx”模板或ASP.NET Web Application项目模板丢失,请使用Visual Studio安装程序并添加“asp.net and web tools”组件
【解决方案2】:

这就是我使用 asmx 页面创建项目的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-09
    • 2014-12-05
    • 2010-10-27
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    相关资源
    最近更新 更多