user15106301
user15106301

Reputation:

Create Restful webservices to call a stored procedure in C#

How to create Restful web service to call a stored procedure in Visual studio 2019.I tried using SOAP and WCF Web service but I don't know how to use RESTful Web service. What I need to give in the URI template ?Any example code or link plz

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

Upvotes: 1

Views: 640

Answers (1)

shahab jani
shahab jani

Reputation: 88

Create a Asp.Net Web Application with Empty template and check Web Api: Create New Asp.Net Web application

After creating the project right click on Controller Folder and select Web Api 2 Controller-Empty Now you have a Restful Api Controller that can be called from any where.(for example from an agular http get request service)

{
    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);
        }
    }
}```

Upvotes: 1

Related Questions