如何将Web API添加到现有的ASP.NET MVC 4 Web应用程序项目?

本文翻译自:How to add Web API to an existing ASP.NET MVC 4 Web Application project?

I wish to add an ASP.NET Web API to an ASP.NET MVC 4 Web Application project, developed in Visual Studio 2012. Which steps must I perform to add a functioning Web API to the project? 我希望将ASP.NET Web API添加到Visual Studio 2012中开发的ASP.NET MVC 4 Web应用程序项目中。我必须执行哪些步骤才能将正常运行的Web API添加到项目中? I'm aware that I need a controller deriving from ApiController, but that's about all I know. 我知道我需要一个从ApiController派生的控制器,但这就是我所知道的。

Let me know if I need to provide more details. 如果我需要提供更多详细信息,请与我们联系。


#1楼

参考:https://stackoom.com/question/oJ9g/如何将Web-API添加到现有的ASP-NET-MVC-Web应用程序项目


#2楼

You can install from nuget as the the below image: 您可以从nuget安装,如下图所示:

在此输入图像描述

Or, run the below command line on Package Manager Console: 或者,在Package Manager控制台上运行以下命令行:

Install-Package Microsoft.AspNet.WebApi

#3楼

The steps I needed to perform were: 我需要执行的步骤是:

  1. Add reference to System.Web.Http.WebHost . 添加对System.Web.Http.WebHost引用。
  2. Add App_Start\\WebApiConfig.cs (see code snippet below). 添加App_Start\\WebApiConfig.cs (请参阅下面的代码段)。
  3. Import namespace System.Web.Http in Global.asax.cs . Global.asax.cs导入名称空间System.Web.Http
  4. Call WebApiConfig.Register(GlobalConfiguration.Configuration) in MvcApplication.Application_Start() (in file Global.asax.cs ), before registering the default Web Application route as that would otherwise take precedence. 注册默认Web应用程序路由之前 ,调用WebApiConfig.Register(GlobalConfiguration.Configuration) MvcApplication.Application_Start() (在文件Global.asax.cs )中的WebApiConfig.Register(GlobalConfiguration.Configuration) ,否则将优先使用。
  5. Add a controller deriving from System.Web.Http.ApiController . 添加从System.Web.Http.ApiController派生的控制器。

I could then learn enough from the tutorial (Your First ASP.NET Web API) to define my API controller. 然后,我可以从教程 (您的第一个ASP.NET Web API)中学到足够的知识来定义我的API控制器。

App_Start\\WebApiConfig.cs: App_Start \\ WebApiConfig.cs:

using System.Web.Http;

class WebApiConfig
{
    public static void Register(HttpConfiguration configuration)
    {
        configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
            new { id = RouteParameter.Optional });
    }
}

Global.asax.cs: 的Global.asax.cs:

using System.Web.Http;

...

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

Update 10.16.2015: 更新10.16.2015:

Word has it, the NuGet package Microsoft.AspNet.WebApi must be installed for the above to work. Word有它,必须安装NuGet包Microsoft.AspNet.WebApi才能使上述工作正常。


#4楼

UPDATE 11/22/2013 - this is the latest WebApi package: 更新2013年11月22日 - 这是最新的WebApi包:

Install-Package Microsoft.AspNet.WebApi

Original answer (this is an older WebApi package) 原始答案(这是一个较旧的WebApi包)

Install-Package AspNetWebApi

More details . 更多细节


#5楼

Before you start merging MVC and Web API projects I would suggest to read about cons and pros to separate these as different projects. 在开始合并MVC和Web API项目之前,我建议您阅读cons和pros将这些项目分开作为不同的项目。 One very important thing (my own) is authentication systems, which is totally different. 一个非常重要的事情(我自己)是身份验证系统,它完全不同。

IF you need to use authenticated requests on both MVC and Web API, you need to remember that Web API is RESTful (don't need to keep session, simple HTTP requests, etc.), but MVC is not. 如果您需要在MVC和Web API上使用经过身份验证的请求,您需要记住Web API是RESTful(不需要保持会话,简单的HTTP请求等),但MVC不需要。

To look on the differences of implementations simply create 2 different projects in Visual Studio 2013 from Templates: one for MVC and one for Web API (don't forget to turn On "Individual Authentication" during creation). 要查看实现的差异,只需在模板中在Visual Studio 2013中创建2个不同的项目:一个用于MVC,另一个用于Web API(不要忘记在创建期间打开“个人身份验证”)。 You will see a lot of difference in AuthencationControllers. 您将在AuthencationControllers中看到很多不同之处。

So, be aware. 所以,请注意。


#6楼

As soon as you add a "WebApi Controller" under controllers folder, Visual Studio takes care of dependencies automatically; 只要在控制器文件夹下添加“WebApi Controller”,Visual Studio就会自动处理依赖关系;

Visual Studio has added the full set of dependencies for ASP.NET Web API 2 to project 'MyTestProject'. Visual Studio已将ASP.NET Web API 2的完整依赖项添加到项目“MyTestProject”中。

The Global.asax.cs file in the project may require additional changes to enable ASP.NET Web API. 项目中的Global.asax.cs文件可能需要进行其他更改才能启用ASP.NET Web API。

  1. Add the following namespace references: 添加以下命名空间引用:

    using System.Web.Http; 使用System.Web.Http; using System.Web.Routing; 使用System.Web.Routing;

  2. If the code does not already define an Application_Start method, add the following method: 如果代码尚未定义Application_Start方法,请添加以下方法:

    protected void Application_Start() { } protected void Application_Start(){}

  3. Add the following lines to the beginning of the Application_Start method: 将以下行添加到Application_Start方法的开头:

    GlobalConfiguration.Configure(WebApiConfig.Register); GlobalConfiguration.Configure(WebApiConfig.Register);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值