简体   繁体   English

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

[英]MVC and Web API Projects in the same Solution

I created a solution a while ago that contains a Web API 2 project (provides JSON data to mobile devices) and a Class Library (includes my data access services). 我刚刚创建了一个包含Web API 2项目(向移动设备提供JSON数据)和类库(包括我的数据访问服务)的解决方案。

The Web API project uses Ninject for DI and everything works fine. Web API项目使用Ninject进行DI,一切正常。

Now I need to add a separate MVC project for a few web pages. 现在我需要为几个网页添加一个单独的MVC项目。 The api should be accessible from www.example.com/api/controller , while the website should be accessed through www.example.com/controller . api应该可以从www.example.com/api/controller访问,而网站应该通过www.example.com/controller访问。

The problem is that each of these two, has a different "Register" method with seemingly incompatible route collections. 问题是这两者中的每一个都有一个不同的“注册”方法,看似不兼容的路由集合。 If I set the MVC project as the startup project, routes for the api are not registered, and vice versa. 如果我将MVC项目设置为启动项目,则不会注册api的路由,反之亦然。 If I set "Mutiple startup projects", they run on different ports which is not my cup of tea. 如果我设置“多个启动项目”,它们会在不同的端口运行,这不是我的一杯茶。

How I can set the MVC project as the startup project, while registering all routes for both of them? 如何将MVC项目设置为启动项目,同时为两者注册所有路由?

One more thing. 还有一件事。 Because the Web API project was created sooner, Ninject configuration has been written inside it. 由于Web API项目是较早创建的,因此Ninject配置已在其中编写。 Of course, some of the services from the Class Library project are needed inside the new MVC project. 当然,在新的MVC项目中需要来自类库项目的一些服务。 Do I have to move Ninject configuration to the MVC project, or they just work because they are run on startup of the Web API Project? 我是否必须将N​​inject配置移动到MVC项目,或者它们只是工作,因为它们是在Web API项目启动时运行的?

This 2 projects are independent from each other like 2 different applications even if they are in the same solution . 这2个项目彼此独立 ,就像2个不同的应用程序一样,即使它们在同一个解决方案中也是如此

To succeed what you try to achieve you have to: 为了成功实现您的目标,您必须:

1) Deploy your MVC project to www.example.com (main virtual application ). 1)将MVC项目部署到www.example.com (主虚拟应用程序 )。

2) Deploy your WebAPI project to www.example.com/api ( api folder is a virtual application ). 2)部署您的WebAPI项目www.example.com/api (API文件夹是一个虚拟应用程序 )。 Don't forget to remove api from your WebAPI routes (other wise you have to use this route www.example.com/api/api/controller ). 不要忘记从WebAPI路由中删除api (另外,您必须使用此路由www.example.com/api/api/controller )。

Doing this you can acces independently both projects in the same url . 这样做,您可以在同一个URL中独立访问这两个项目。

For NInject part, you can register again the services in the MVC project. 对于NInject部分,您可以再次注册MVC项目中的服务。 Another solution will (which i recommend) be to make a class library project and register there the services after that you reference this class library in both projects. 另一个解决方案(我建议)是在两个项目中引用此类之后创建一个类库项目并在那里注册服务。

What I like to do, is to have the MVC and WebAPI project in two separate projects ( separation of concerns ) but let them share the same business logic. 我喜欢做的是将MVC和WebAPI项目放在两个独立的项目中( 关注点分离 ),但让它们共享相同的业务逻辑。

I like to utilize the Command and Query pattern. 我喜欢使用命令和查询模式。 So all commands and queries is located in another solution project, which both the MVC and WebAPI project has access to. 因此,所有命令和查询都位于另一个解决方案项目中,MVC和WebAPI项目都可以访问该项目。

I deploy the MVC project on the www.domain.com path, the WebAPI project on the api.domain.com and enable CORS on WebAPI for the www origin. 我部署的MVC项目www.domain.com路径,在该项目的WebAPI api.domain.com并启用CORS上的WebAPI的www起源。

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

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