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

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 to http://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.

  • Tohid Azizi

    Finally! Nice job.

    • http://kashyapas.com Lohith

      Thanks :)

  • Mansyn

    Why won’t web forms and VB die already!

    • http://kashyapas.com Lohith

      Sorry I didn’t get you ?

  • Rahul Mehta

    nice one

    • http://kashyapas.com Lohith

      Thanks :)

  • speschka

    Yeah, there’s some pieces missing from this template. I just downloaded, installed and created a new project then hit F5 to run. I got 15 compiler errors, all of which seem to be related to missing type or namespace “DotNetOpenAuth”, “AspNet”, “Optimization”, “BundleCollection”, “Newtonsoft”, “ProviderDetails”, and “OpenAuthAccountData”. There is also an error that GetFriendlyUrlSegments does not exist and I haven’t even tried to see why that is yet.

    • http://kashyapas.com Lohith

      hi

      thanks for the update. seems like the pre built NuGet packages failed to get the references :(. need to dig whats causing the problem.

  • rickj1

    I tried this template I haven’t got it to work I updated all the nugget packages to include prelease I get 10 errors and 10 warnings I want to know how do I remove this template from VS 2012

  • Arjunan88

    Hi..Does it work in visual studio 2010 with sp1? Can i install template alone in vstudio 2010[sp1]?