Monday 27 February 2017

WEB API TUTORIAL

Step 1: File----> New Project----> ASP .NET Web Application----->Web API
















Step 2: Following File Structure Shows after Select Web API.

















ValuesController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MyWebApiDemo.Controllers
{
    [Authorize]
    public class ValuesController : ApiController//values controller class inherit from ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }
}

Step 3: After Run The Default Application we are getting following Error
Denied For This Request



















Step 4: Comment the authorized attribute then run the application

Get method with 2 parameter  [Authorize]




















Step 5: Get method pass id parameter then getting following output.


















Step 6: After commet the values controller class (default values controller)
Then display below error





















No comments:

Post a Comment