Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建Restful Web服务以调用C中的存储过程_C#_.net_Rest_Web Services_Sql Server 2016 - Fatal编程技术网

C# 创建Restful Web服务以调用C中的存储过程

C# 创建Restful Web服务以调用C中的存储过程,c#,.net,rest,web-services,sql-server-2016,C#,.net,Rest,Web Services,Sql Server 2016,如何在Visual studio 2019中创建Restful web服务以调用存储过程。我尝试使用SOAP和WCF web服务,但不知道如何使用Restful web服务。我需要在URI模板中给出什么?任何示例代码或链接plz public interface IRestWebService { [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "&qu

如何在Visual studio 2019中创建Restful web服务以调用存储过程。我尝试使用SOAP和WCF web服务,但不知道如何使用Restful web服务。我需要在URI模板中给出什么?任何示例代码或链接plz

public interface IRestWebService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "",
            RequestFormat = WebMessageFormat.,
            ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped)]
        int callStoredProcedure(string value);
    } 

使用空模板创建Asp.Net Web应用程序并检查Web Api:

创建项目后,右键单击控制器文件夹并选择WebAPI2controllerempty 现在您有了一个Restful Api控制器,可以从任何地方调用它

{
    public class RestWebServiceController : ApiController
    {
        SqlConnection con;
        public RestWebServiceController()
        {
            con  = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ToString());
        }
         

        [HttpGet]
        public IHttpActionResult CallStoredProcedure(string Name)
        {
            int ReturnValue = 0;
            SqlCommand cmd = new SqlCommand("StartOnline", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlParameter ret = new SqlParameter();
            ret.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.AddWithValue("@Name", Name);
            ret.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(ret);
            cmd.ExecuteNonQuery();
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                ReturnValue = (int)ret.Value;
            }
            catch (Exception ex)
            {

            }
            return Ok(ReturnValue);
        }
    }
}```

web服务或调用存储过程是您在努力解决的哪一部分?您的问题缺少详细信息,请向我们展示一些您尝试过的代码。不要使用更新的信息进行评论,请编辑问题。请阅读不要尝试使用WCF REST服务,除非您需要维护已有10年历史的应用程序。WCF REST是在MVC和更高版本的Web API之前作为权宜之计创建的。甚至Web API现在也有9年的历史了。它需要大量的代码来完成Web API中只需要几行代码的工作。在任何情况下,您尝试的都与REST相反。对于REST,URL表示对象或资源,这些资源上的操作由诸如GET/POST/PUT/PATCH/DELETE之类的HTTP动词执行。如果你想找回所有的客户,你需要做一件事https://.../Customers. 对于特定的客户,请上车https://..../Customers/5. 要创建,请在…/Customers上发布。要编辑,请放置“…/Customers/1”等这就是为什么所有Web API教程都显示CustomerController,其中包含Get、Getint、PostCustomer等操作,以便在我的clienti.e windows应用程序中使用此restful Web服务请查看此链接