Implement Web-API in normal Web Form application

Sourav.Kayal
Posted by in ASP.NET MVC category on for Beginner level | Points: 250 | Views : 7177 red flag

In this article we will learn to integrate Web-API in Web Form application.

Implement Web-API in normal Web Form application

In this example we will implement Web-API service to normal Web Form application. We know that MVC4 and Web-API is build on top of core ASP.NET so it’s very much possible to do it. Here is the step by step process to get it done.

Open one normal empty web application. Here is screen shot to help exact template from Visual Studio.


Here is new project created in solution explorer once we press OK button.


Add one folder called WebAPI in this solution.


Add controller class to this folder, here is sample screen shot.


Here we have created “person” controller which is derive from ApiController class. Here is complete code of person controller.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
 
namespace WebApplication1.WebAPI
{
    public class personController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
 
        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }
 
        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }
 
        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }
 
        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}

Add Global.aspx file in application , because we have chosen empty web application and there is no Global.aspx page on that.


Create App_start folder in application


Add a class file to App_start called WebApiconfig


Complete code of WebApiConfig class. In this method e have implemented default route for controller.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
 
namespace WebApplication1.App_Start
{
    public class WebApiConfig
    {
         public static void Register(HttpConfiguration config)
         {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
 
    }
}

Now open Global.aspx page and modify Application_start event like below

protected void Application_Start(object sender, EventArgs e)
        {
            WebApiConfig.Register(GlobalConfiguration.Configuration);
        }

Call to Web method from browser


 

Conclusion:-

In this example we have learned to add Web-API in Web Form application. It is very helpful when we want to implement Web-API in any existing Web Form application. 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)