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.
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.
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:
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.
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:
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.
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.
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.