Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is web API and how it is independent of System.Web ?

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 423
    Comment on it

    What is web API:-
            Microsoft released the ASP.NET MVC 4.0 in Feb 2012 and along with it, the ASP.NET Web API also released. Web API is an addition to the ASP.NET that provides a new, robust HTTP framework for creating REST(REpresentational State Transfer) service. There are variety of consumers of data like browser, mobile device and tablet. Web API provides common way for accessing the data.


    Web APIs support wide variety of media types including XML, JSON etc.The programming model of ASP.NET Web API resembles ASP.NET MVC in being simple, instead of requiring you to define interfaces, create implementation classes, and decorate them with several attributes.The web API is exactly the same as a normal MVC controller except that the base class is ApiController, Web API decorates method with attributes to make it easy to do CRUD operations.
    The HTTP actions and their corresponding CRUD operations are:

    GET (Read)
    Retrieves the representation of the resource.

    PUT(Update)
    Update an existing resource.

    POST (Create)
    Create new resource.

    DELETE (Delete)
    Delete an existing resource.

    Example:-

    public class testAPI:ApiController
    {
            [HttpPut]  
            public void Put(int id, [FromBody]string value)  
            {  
            }  
            [HttpPost]  
            public void Post([FromBody]string value)  
            {  
            }  
            [HttpDelete]  
            public void Delete(int id)  
            {  
            }
    }
    

    How it is independent of System.Web:-
         What makes web API independent of system.web is hosting in different enviormnet. web API can be hosted in window application,console application, window service and in IIS also. when we host web API in all the above application except IIS we don't require system.web and it is called self hosting.we need to add the reference of System.Web.Http.SelfHost dll for self hosting.
    EX- We will create a console applicatiopn

    static void Main(string[] args)
    {
                var config = new HttpSelfHostConfiguration("http://localhost:8080");
                config.Routes.MapHttpRoute(
                    "API Default", "api/{controller}/{id}",
                    new { id = RouteParameter.Optional });
    
                using (HttpSelfHostServer server = new HttpSelfHostServer(config))
                {
                    server.OpenAsync().Wait();
                    Console.WriteLine("Press Enter to quit.");
                    Console.ReadLine();
                }
    }
    

    Reference

    http://www.c-sharpcorner.com/UploadFile/4d9083/how-to-create-web-api-in-Asp-Net-mvc/

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: