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

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


当前回答

在我的MVC 5项目中添加WebAPI。

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 Right click controllers folder > add new item > web > Add Web API controller Web.config will be modified accordingly by VS Add Application_Start method if not there already protected void Application_Start() { //this should be line #1 in this method GlobalConfiguration.Configure(WebApiConfig.Register); } Add the following class (I added in global.asax.cs file) 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 } ); } } Modify web api method accordingly 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"; } // GET api/<controller>/5 : url to use => api/vs/5 public string Get(int id) { return (id + 1).ToString(); } } } Rebuild and test Build a simple html page <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="../<path_to_jquery>/jquery-1.9.1.min.js"></script> <script type="text/javascript"> var uri = '/api/vs'; $(document).ready(function () { $.getJSON(uri) .done(function (data) { alert('got: ' + data); }); $.ajax({ url: '/api/vs/5', async: true, success: function (data) { alert('seccess1'); var res = parseInt(data); alert('got res=' + res); } }); }); </script> </head> <body> .... </body> </html>

其他回答

在我的MVC 5项目中添加WebAPI。

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 Right click controllers folder > add new item > web > Add Web API controller Web.config will be modified accordingly by VS Add Application_Start method if not there already protected void Application_Start() { //this should be line #1 in this method GlobalConfiguration.Configure(WebApiConfig.Register); } Add the following class (I added in global.asax.cs file) 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 } ); } } Modify web api method accordingly 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"; } // GET api/<controller>/5 : url to use => api/vs/5 public string Get(int id) { return (id + 1).ToString(); } } } Rebuild and test Build a simple html page <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="../<path_to_jquery>/jquery-1.9.1.min.js"></script> <script type="text/javascript"> var uri = '/api/vs'; $(document).ready(function () { $.getJSON(uri) .done(function (data) { alert('got: ' + data); }); $.ajax({ url: '/api/vs/5', async: true, success: function (data) { alert('seccess1'); var res = parseInt(data); alert('got res=' + res); } }); }); </script> </head> <body> .... </body> </html>

只要你在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);

在你开始合并MVC和Web API项目之前,我建议你先阅读一下优缺点,把它们区分为不同的项目。一个非常重要的事情(我自己的)是认证系统,这是完全不同的。

如果你需要同时在MVC和Web API上使用经过身份验证的请求,你需要记住Web API是RESTful的(不需要保持会话,简单的HTTP请求等),但MVC不是。

要查看实现的差异,只需在Visual Studio 2013中从模板创建2个不同的项目:一个用于MVC,一个用于Web API(不要忘记在创建过程中打开“个人认证”)。您将在authenticationcontrollers中看到许多不同之处。

所以,要注意。

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

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

就是这样;)

更新11/22/2013 -这是最新的WebApi包:

Install-Package Microsoft.AspNet.WebApi

原始答案(这是一个较旧的WebApi包)

Install-Package AspNetWebApi

更多的细节。