1

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);
    } 
  • Which part are you struggling with, the web service or calling a stored procedure? Your question lacks detail, show us some code that you've tried. – Jeremy Lakeman Jun 03 '21 at 06:07
  • Don't comment with updated info, edit the question. And read https://stackoverflow.com/help/how-to-ask – Jeremy Lakeman Jun 03 '21 at 06:52
  • *Don't* try to use WCF REST services unless you need to maintain some 10-year old application. WCF REST was created as a stop-gap until MVC and later Web API. Even Web API is 9 years old now. It requires a *lot* of code to what requires just a few lines in Web API. – Panagiotis Kanavos Jun 03 '21 at 07:22
  • In any case what you try is the **opposite** of REST. With REST, the URL represents *objects* or *resources* and actions on those resources are performed by HTTP verbs like GET/POST/PUT/PATCH/DELETE. If you want to retrieve all customers, you do a `GET` on `https://.../Customers`. For a specific customer, GET on `https://..../Customers/5`. To create, POST on `.../Customers`. To edit, PUT ` .../Customers/1` etc – Panagiotis Kanavos Jun 03 '21 at 07:24
  • That's why all Web API tutorials show eg a `CustomersController` with actions for `Get`, `Get(int)`, `Post(Customer)` etc – Panagiotis Kanavos Jun 03 '21 at 07:26
  • Read [Tutorial: Create a web API with ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio) at learn.microsoft.com. This shows how to create a web service that actually follows the REST style. It's completely different from what you tried, – Panagiotis Kanavos Jun 03 '21 at 07:29

1 Answers1

1

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);
        }
    }
}```
  • How to consume this restful web service in my client(i.e windows )application –  Jun 04 '21 at 02:57
  • take a look at this link https://stackoverflow.com/questions/32716174/call-and-consume-web-api-in-winform-using-c-net – shahab jani Jun 04 '21 at 06:42