webservice简单例子

1、添加web服务。

/// <summary>
    /// demo 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    //[System.Web.Script.Services.ScriptService]
    public class demo : System.Web.Services.WebService
    {

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

        [WebMethod]
        public int GetSum(int a, int b)
        {
            return a + b;
        }
    }

2、添加服务引用:地址为上一步添加的web服务访问地址,高级=》添加web引用=》添加引用。

3、调用

public ActionResult GetSum(int a, int b)
        {
            localhost.demo s = new localhost.demo();
            var sum = s.GetSum(a, b);
            return Content(sum.ToString());
        }
原文地址:https://www.cnblogs.com/flywing/p/7279903.html