Talha Masood
Talha Masood

Reputation: 993

Adding Web API to existing asp.net web forms Application

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.

Upvotes: 22

Views: 32707

Answers (2)

Ryan Kyle
Ryan Kyle

Reputation: 743

In Visual Studio 2013

  1. Right-click on the ASP.NET Web Forms project.
  2. Add -> Add Scaffolded Item... or New 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;
  1. If the code does not already define an Application_Start method, add the following method:

protected void Application_Start()

{
}
  1. Add the following lines to the beginning of the Application_Start method:
GlobalConfiguration.Configure(WebApiConfig2.Register);

Upvotes: 30

Talha Masood
Talha Masood

Reputation: 993

After much research I have been able to come up with a solution to this problem. Let me illustrate my solution with respect to the Visual Studio Version.

VS 2012

As I mentioned in the question, there is no definite way to create the Web API project in VS2012. You are gonna have to do it by creating an MVC 4 application and setting the Project Template as WebAPI. Then once you have done this and you have your Web API functional, you can safely delete the extra baggage like the Jquery libraries and other stuff, because these things are absolutely of no use here in your project.

VS 2013

In VS2013 there is however a better approach followed to add and manage the Web API projects. Currently I am using VS2013 for the Web API and all things have fallen into place just as I wanted. Kindly see this link and you will get a better idea

http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

I hope this information will help all those new to Web API. Especially for those who want to upgrade to Web API or add Web API to existing projects.

Upvotes: 0

Related Questions