WebApi2 in visual studio 2012
Asked Answered
B

5

22

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.

Broad answered 21/8, 2013 at 4:55 Comment(0)
R
33

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

Richierichlad answered 10/2, 2014 at 3:0 Comment(3)
These links lead to the Web Platform Installer which seems to no longer have the ASP.NET and Web Tools 2013.1 for Visual Studio 2012 installer. The ASP.NET and Web Tools 2013.2 installer is available via the Web PI but does not seem to add the Web API 2 templates. I was able to find the 2012.1 installers here microsoft.com/en-us/download/details.aspx?id=41532Viewless
@RyanTaylor your comment is spot on. It should be an answer!Diamagnetic
@RyanTaylor installed WT2013.1 but I don't see that Templates in Add a New Project dialog. Any advise?Diamagnetic
A
7

Create a MVC project, right-click to and select

  1. Create an MVC project
  2. Right click the project in solution explorer and select "Manage nuget packages"
  3. Select "include Prerelease"
  4. Search for web api and select "Microsoft ASP.NET Web API", make sure the version is 5.0.0-beta2 (Prerelease)

Wait, after a while you should be good to go.

Apartheid answered 21/8, 2013 at 7:38 Comment(1)
+1. Looks like there's a permanent NUGET package update for Web API 5.1 now (non-beta) that fixes this issue for Visual Studio 2012. Just do the nuget package update steps above on your Web API packages and it should work.Yesterday
L
7

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.

Loredo answered 21/8, 2013 at 8:12 Comment(0)
O
4

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.

  • Start with a new MVC 4 / Web API project
  • 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.

  • Add references to System.Web.Http 5.0 to web.config according to step 2 of this answer.
  • If you build at this point, you'll get a compiler error stating that 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!

Oatmeal answered 16/10, 2013 at 15:25 Comment(0)
S
2

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.

enter image description here

Sayette answered 18/9, 2015 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.