.NET Zone is brought to you in partnership with:

My name is Lohith G N. I am a Production Engg by education but a Software Engg by profession. I have an industry experience of 10 years in software development. I started with Web Application Development with ASP.NET 1.1 in 2001. Now have expertise in Web Application Development using ASP.NET Web Forms & ASP.NET MVC, Windows Application Development using Win Forms & WPF, Service Oriented Development with WCF. I have been awarded Microsoft Most Valuable Person in Oct 2011 for ASP.NET/IIS area and focus on Open Data Protocol or OData. Lohith is a DZone MVB and is not an employee of DZone and has posted 22 posts at DZone. You can read more from them at their website. View Full User Profile

Releasing “ASP.NET Web Forms with Web API Application” Project Template

05.28.2013
| 2974 views |
  • submit to reddit

With ASP.NET MVC 4 release, one of the interesting features added was that of Web API. But there was confusion that Microsoft created in terms of tooling. If you had to create a Web API – you had to select a ASP.NET MVC 4 Web Application and then you could select Web API template.

asplogo

What if you needed to create a Web API in Web Forms application. Well you can always add a Web API in Web Forms application by using the “Add New Item” feature and selecting “Web API Controller Class” item template.

Wouldn’t it be nicer to have a project template which helps you create a Web Forms application with Web API infrastructure in place. It becomes easier if the project template does everything i.e. add the necessary infrastructure, set up the folder structure, include the right packages and make it all work. So this was a need I saw as a opportunity to create something for the community.

Without much further ado – announcing the release of “ASP.NET Web Forms with Web API Application”project template. You can download the project template from Visual Studio Gallery here:http://visualstudiogallery.msdn.microsoft.com/a92c3bac-02ae-4229-9796-93e194d452ab.

image

Installing Project Template:

When you download the project template extension from Visual Studio Gallery, you will get “WebFormWebApi.vsix” file. The VSIX is a Visual Studio Extension installer. Double click on the VSIX and you should see the following install screen:

image

The project template is distributed as a Visual Studio extension and is supported on all versions of Visual Studio 2012.

Creating Web Forms with Web API Application Project:

Once you have completed installing the project template, open a visual studio and select File > New Project. In the New Project dialog, select Visual C# and you should see a new project templated titled “ASP.NET Web Forms Web Api Application”. Select the template, give it a name and click Ok.

SNAGHTML1027519d

Look at the Web Forms with Web Api Project Structure:

Once visual studio finishes creating the project, take a look at the project structure. The template performs the following changes to the project:

  • Adds WebApiConfig.cs in App_Start folder. This contains a single Register method which will register the Web API routes.
  • Modifies Global.asax.cs > Application_Start method to call the Regiater method of WebApiConfig.
  • Add Api folder and adds a dummy controller named ValuesController.
  • Adds Help folder. This folder  contains infrastructure related to Web Api Help. This feature will provide the help pages related to your Web API’s in the project

Here is the screen shot of solution explorer once the project is created:

image

Accessing Web API:

Once the project is created, build it once and run the project. When the browser opens, change the URL tohttp://localhost:<port>/api/Values. This will invoke the web api and depending on what browser you open, action will be different. If you open the page in IE, IE will prompt you to save a JSON payload. If you open the page in Chrome/Firefox, they will show you a XML data. So your project is now set up to host the Web API.

SNAGHTML103aa642

Accessing Web API Help Pages:

Just navigate to http://localhost:<port>/Help/Index. This will list down all the Web API controllers available in the project and list down the routes they support.

image

image

Summary:

We looked at the new project template I created which helps you created a Web Forms project with web Api feature. This template supports .NET 4.5 &C# and is available for all versions of Visual Studio 2012 including the express edition. It also has a dummy API controller and a Web API Help pages in built. Hope you guys take a look at the new project template, download and play with it. Do let me know of any feedback or suggestions or comments related to the template.

Till next time, Happy Coding. Code with Passion, Decode with Patience.

Published at DZone with permission of Lohith Nagaraj, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)