简体   繁体   English

同一解决方案中的 MVC 和 Web Api 项目

[英]MVC & Web Api projects within same Solution

I have an MVC 4 project sitting atop an N-tier application.我有一个位于 N 层应用程序之上的 MVC 4 项目。 I now have a requirement to to be able to consume the application programmatically.我现在需要能够以编程方式使用应用程序。 I have created a new Web API project within the same solution which sits along side the MVC project, again atop the N-tier application.我在同一个解决方案中创建了一个新的 Web API 项目,该项目位于 MVC 项目旁边,再次位于 N 层应用程序之上。

But I am unclear as to how this all works as the MVC layer is the startup project: it sets up my DI, automapper, etc. and is the project I deploy to the server.但我不清楚这一切是如何工作的,因为 MVC 层是启动项目:它设置我的 DI、自动映射器等,并且是我部署到服务器的项目。

So how should this all be set up?那么这一切应该如何设置呢? Can I set up my MVC project to route all /api requests to the new Web API project?我可以设置我的 MVC 项目以将所有/api请求路由到新的 Web API 项目吗? Or does the Web API project need to be deployed separately?还是需要单独部署Web API项目?

I don't want to be doing anything unconventional so if there is a much more common way of setting this up, please point me in the right direction.我不想做任何非常规的事情,所以如果有更常见的设置方式,请指出正确的方向。

Thanks.谢谢。

WebApi is an alternative Service oriented application from Microsoft just like WCF. WebApi 是 Microsoft 的一个替代的面向服务的应用程序,就像 WCF 一样。 But WCF uses SOAP protocol and WebAPI uses HTTP protocol for communication.但是 WCF 使用 SOAP 协议,而 WebAPI 使用 HTTP 协议进行通信。

So if you are using WCF to provide service for your MVC application you would host that wcf service seperately and consume its service by MVC application, EXACTLY same way you have to host your WebAPI project seperately and provide service to your Web application (MVC).因此,如果您使用 WCF 为您的 MVC 应用程序提供服务,您将单独托管该 wcf 服务并通过 MVC 应用程序使用其服务,与您必须单独托管您的 WebAPI 项目并向您的 Web 应用程序 (MVC) 提供服务的方式完全相同。

for some reasons if you want them (MVC and WebAPI) to use in the same project, follow this rules from this article.出于某些原因,如果您希望它们(MVC 和 WebAPI)在同一个项目中使用,请遵循本文中的规则。

http://odetocode.com/blogs/scott/archive/2013/07/01/on-the-coexistence-of-asp-net-mvc-and-webapi.aspx http://odetocode.com/blogs/scott/archive/2013/07/01/on-the-coexistence-of-asp-net-mvc-and-webapi.aspx

I just did the same thing yesterday.我昨天刚刚做了同样的事情。 I have in the same MVC 4 project regular Controllers and ApiControllers.我在同一个 MVC 4 项目中有常规控制器和 ApiController。

You need to add the routing in the Global Asax for WebApi :您需要在 Global Asax for WebApi 中添加路由:

WebApiConfig.Register(GlobalConfiguration.Configuration);

Take a look at the WebApiConfig :看看 WebApiConfig :

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

Don't forget also to add the Nuget Packages for WebApi (if you don't have them already).不要忘记为 WebApi 添加 Nuget 包(如果你还没有的话)。 In my case I did not had them because my project was originally MVC 3 and was later upgraded.就我而言,我没有它们,因为我的项目最初是 MVC 3,后来升级了。

您只需要将路由和 API 控制器添加到您现有的 MVC 站点,它们都将在site/api/下可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM