我想增加一个ASP。NET Web API到ASP。NET MVC 4 Web应用程序项目,在Visual Studio 2012开发。我必须执行哪些步骤才能将功能正常的Web API添加到项目中?我知道我需要一个从ApiController派生的控制器,但这就是我所知道的全部。

如果我需要提供更多细节,请告诉我。


当前回答

注意:这只是上面这个答案的缩写

Open NuGet Package manager console and run PM> Install-Package Microsoft.AspNet.WebApi Add references to System.Web.Routing, System.Web.Net and System.Net.Http dlls if not there already Add the following class public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } Add Application_Start method if not there already (in global.asax.cs file) protected void Application_Start() { //this should be line #1 in this method GlobalConfiguration.Configure(WebApiConfig.Register); } Right click controllers folder > add new item > web > Add Web API controller namespace <Your.NameSpace.Here> { public class VSController : ApiController { // GET api/<controller> : url to use => api/vs public string Get() { return "Hi from web api controller"; } } }

其他回答

上述解决方案非常有效。如下图所示,我在选择项目模板时更倾向于选择Web API选项

注意:该解决方案适用于Visual Studio 2013或更高版本。最初的问题是在2012年提出的,现在是2016年,因此增加了Visual Studio 2013或更高版本的解决方案。

你可以从nuget安装如下图所示:

或者,在包管理器控制台中运行以下命令行:

Install-Package Microsoft.AspNet.WebApi

只要你在controllers文件夹下添加了一个“WebApi Controller”,Visual Studio就会自动处理依赖关系;

Visual Studio has added the full set of dependencies for ASP.NET Web API 2 to project 'MyTestProject'. The Global.asax.cs file in the project may require additional changes to enable ASP.NET Web API. Add the following namespace references: using System.Web.Http; using System.Web.Routing; If the code does not already define an Application_Start method, add the following method: protected void Application_Start() { } Add the following lines to the beginning of the Application_Start method: GlobalConfiguration.Configure(WebApiConfig.Register);

注意:这只是上面这个答案的缩写

Open NuGet Package manager console and run PM> Install-Package Microsoft.AspNet.WebApi Add references to System.Web.Routing, System.Web.Net and System.Net.Http dlls if not there already Add the following class public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } Add Application_Start method if not there already (in global.asax.cs file) protected void Application_Start() { //this should be line #1 in this method GlobalConfiguration.Configure(WebApiConfig.Register); } Right click controllers folder > add new item > web > Add Web API controller namespace <Your.NameSpace.Here> { public class VSController : ApiController { // GET api/<controller> : url to use => api/vs public string Get() { return "Hi from web api controller"; } } }

我也有同样的问题,解决方法很简单

右击soltion 安装Microsoft.ASP.NET.WebApi从“管理Nuget包Sulotion”

就是这样;)