22

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.

1

2 Answers 2

30

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); 
2
  • 1
    I don't see "Add Scaffolding Item..."
    – Si8
    CommentedMar 22, 2017 at 13:48
  • 2
    In VS2015 it is "New Scaffolded Item..."CommentedApr 7, 2017 at 8:30
0

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.

4
  • @daddy6Elbows. Actually my requirement was to add Web API in the existing Web Forms Application and hence my Question Title "Adding Web API to existing asp.net web forms Application". However just to answer your concern, Adding Web API to MVC may not be so different. Just make a separate project of Web API and either reference it in you MVC or call the methods from MVC over HTTP from this project. Both should work.CommentedApr 1, 2014 at 23:44
  • 1
    Yes, you were looking at how to add MVC to an existing Web Forms application, not to an MVC application. My apologies for mis-characterizing the question. I just don't think anyone looking for an answer to your original question will gain much insight from your subsequent answer, since you give no steps, examples or anything but a link to an article that seem only semi-related. I could be wrong and perhaps it doesn't matter if no one else asks the question.
    – witttness
    CommentedApr 2, 2014 at 16:23
  • I have an external website which is ASP.net Web Form. I published my Web API to a folder as file system. I, then copied those files/folders to a sub folder that I created in my website, so it can be accessed externally. But when I tried to query a API call, I get an error. Am I doing the process wrong?
    – Si8
    CommentedMar 22, 2017 at 13:56
  • I know this is an old thread but below link explains how to add WebAPI to existing Webforms Project. c-sharpcorner.com/article/…CommentedDec 3, 2020 at 7:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.