How can I create a project that uses WebApi2 in visual studio 2012?
Do I have to create with a blank project or do I have to create a MVC project?
I don't want to install Visual Studio 2013 preview, please help.
Task 1 - Creating the API ProjectRun Visual Studio 2012 Express for Web, to do this go to Start and type VS Express for Web then press Enter. From the File menu, select New Project. Select the Visual C# | Web project type from the project type tree view, then select the ASP.NET MVC 4 Web Application project type.
Now you can create ASP.Net Web API project using Visual Studio 2010. When you create a new ASP.Net MVC 4 project, you can choose the Web API template.
To check this, you can open your solution, expand the webapi solution, then open the packages. config file that holds the nuget config for what you have installed. You should see some web api version message in the below.
Here, we will create a new stand-alone Web API project without MVC project. For this, open Visual Studio 2013 for Web -> go to File menu and select New Project.. This will open New Project popup as below. Select Web template in the left pane and ASP.NET Web Application in the middle pane.
You need to install ASP.NET and Web Tools 2013.1 for Visual Studio 2012 which adds the Web API 2 and MVC 5 templates
Create a MVC project, right-click to and select
Wait, after a while you should be good to go.
As far as I know, Web API 2 is equal to the 5.0.0-beta2, so you can just pull that from NuGet as described by Olav.
However, if you've installed the VS2012.2 update, you can start with a Web API template, not just with a MVC template. Start a new project, select MVC and then select the Web API template. It won't add that much, just a few example controllers and some configuration files tailored for Web API.
For specific VS2013 features like they've shown in some Build video's: I'm afraid you would need the 2013 Preview in order to use those.
My initial research led me to take the manual configuration approach I detail below, so I actually never tried Olav's suggestion. If that works, that's probably the way to go. But if not, or if there are missing pieces to that approach, here are the steps I took. I'm using VS 2012 Update 3.
From the package manager console, run:
Install-Package Microsoft.AspNet.WebApi -pre
I received a warning that it was skipping _ViewStart.cshtml
but it gave me the option to overwrite. I chose Yes.
Again from the package manager console, run:
Install-Package Microsoft.AspNet.Mvc -Pre
Change web.config, views\web.config, and areas\views\web.config according to these instructions. Ignore steps 1 - 5 under Upgrade Steps. Also, I didn't need to do anything concerning WebHelpers.
XmlDocumentationProvider
in the Areas\HelpPage folder doesn't fully implement the interface. Not sure what that's all about, but at this point it's not important to me so I just added empty implementations to the two missing methods (tip: right click on IDocumentationProvider and select implement interface - it will add the two missing methods).I did this a couple days ago and my memory is already getting fuzzy, but I think that covers it. At this point I'm able to use WebAPI2 including attribute-based routing!
Open view and select package manager console
and paste following command and press enter.
Install-Package Microsoft.AspNet.WebApi
Package manger console
will display at bottom.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With