如何將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);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章