Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Web API to existing asp.net web forms Application

Tags:

My team wants to upgrade from WCF to Web API. We have a working asp.net web form application, that we have imported to VS2012 from VS2010. So far so good.

But now as I try to make a separate Web API project, I see that there is no Web API template available. The closest thing that I can find is by creating an MVC 4 application and setting the Project Template as WebAPI. I followed this way and everything falls in perfectly. I have the working API with a sample controller that I can invoke by making calls from the browser.

The only downside to this is that, this particular method brings in its own baggage. The MVC 4 project I created has JQUERY and other libraries included, plus some other folders that I probably don't need. What I want is the Web API structure only - and not the extra baggage.

I tried finding a template using online search but the package I found does not work properly and as very poor rating. enter image description here

I hope I have illustrated my problem properly. I am looking forward for some feedback now :) Thanks.

like image 461
Talha Masood Avatar asked Dec 12 '13 08:12

Talha Masood


1 Answers

In Visual Studio 2013

  1. Right-click on the ASP.NET Web Forms project.
  2. Add -> Add Scaffolded Item...
  3. Under Installed/Common/MVC/Web API choose the scaffold type you wish to use.
  4. Follow the instructions for the scaffold template. For example, when you choose "Web API 2 Controller with read/write actions" or "Web API 2 Controller - Empty", you are prompted for a controller name
  5. You will then need to move the recently created controller into the recently created Controllers folder.

Results

From what I can see, Visual Studio does the following:

  • "App_Start/WebApiConfig2.cs" is created.
  • Controllers folder is created.
  • Web.config is edited to add "handlers" element with content in "system.webServer".
  • The following references are added:
    • System.Net.Http
    • System.Net.Http.Formatting
    • System.Web.Extensions
    • System.Web.Http
    • System.Web.Http.WebHost
  • packages.config is updated to include:
    • "Microsoft.AspNet.WebApi"
    • "Microsoft.AspNet.WebApi.Client"
    • "Microsoft.AspNet.WebApi.Core"
    • "Microsoft.AspNet.WebApi.WebHost"

Notes

Subsequently, I recommend following the same steps, starting with right-clicking on the Controllers folder instead of the project. This will put the new controller in the Controllers folder instead of at the root level.

Readme from Visual Studio after following the above steps:

Visual Studio has added the full set of dependencies for ASP.NET Web API 2 to project 'RowersCode.Web'.

The Global.asax.cs file in the project may require additional changes to enable ASP.NET Web API.

  1. Add the following namespace references:

    • using System.Web.Http;
    • using System.Web.Routing;
  2. If the code does not already define an Application_Start method, add the following method:

    protected void Application_Start()

    {

    }

  3. Add the following lines to the beginning of the Application_Start method:

    GlobalConfiguration.Configure(WebApiConfig2.Register);

like image 164
Ryan Kyle Avatar answered Sep 19 '22 14:09

Ryan Kyle