相同解决方案中的MVC和Web

相同解决方案中的MVC和Web

本文介绍了相同解决方案中的MVC和Web API项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不久前创建了一个解决方案,其中包含一个Web API 2项目(向移动设备提供JSON数据)和一个类库(包括我的数据访问服务).

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项目使用Ninject进行DI,一切正常.

The Web API project uses Ninject for DI and everything works fine.

现在,我需要为几个网页添加一个单独的MVC项目.应该可以从www.example.com/api/controller访问api,而应该通过www.example.com/controller访问网站.

Now I need to add a separate MVC project for a few web pages. The api should be accessible from www.example.com/api/controller, while the website should be accessed through www.example.com/controller.

问题在于这两个中的每一个都有不同的注册"方法,该方法具有看似不兼容的路由集合.如果我将MVC项目设置为启动项目,则不会注册api的路由,反之亦然.如果我设置多个启动项目",它们将在不同的端口上运行,而不是我喝杯茶.

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. If I set "Mutiple startup projects", they run on different ports which is not my cup of tea.

如何在将MVC项目注册为启动项目的同时为其注册所有路由?

How I can set the MVC project as the startup project, while registering all routes for both of them?

还有一件事.由于Web API项目是较早创建的,因此已在其中编写了Ninject配置.当然,新的MVC项目中需要类库项目中的某些服务.我是否必须将N​​inject配置移至MVC项目,否则它们之所以起作用,是因为它们是在Web API项目启动时运行的?

One more thing. Because the Web API project was created sooner, Ninject configuration has been written inside it. Of course, some of the services from the Class Library project are needed inside the new MVC project. 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?

推荐答案

这2个项目彼此彼此独立,就像 2个不同应用程序一样,即使它们位于相同的解决方案.

This 2 projects are independent from each other like 2 different applications even if they are in the same solution.

要成功实现您想要实现的目标,您必须:

To succeed what you try to achieve you have to:

1)将 MVC 项目部署到www.example.com(主虚拟应用程序).

1) Deploy your MVC project to www.example.com (main virtual application).

2)将 WebAPI 项目部署到www.example.com/api( api 文件夹是虚拟应用程序).不要忘记从 WebAPI 路由中删除api(否则,您必须使用此路由www.example.com/api/api/controller).

2) Deploy your WebAPI project to www.example.com/api (api folder is a virtual application). Don't forget to remove api from your WebAPI routes (other wise you have to use this route www.example.com/api/api/controller).

这样做,您可以在相同的URL 中独立地访问两个项目.

Doing this you can acces independently both projects in the same url.

对于NInject部分,您可以在MVC项目中再次注册服务.另一个解决方案(我建议)是制作一个类库项目,并在您在两个项目中都引用该类库之后在此处注册服务.

For NInject part, you can register again the services in the MVC project. 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.

这篇关于相同解决方案中的MVC和Web API项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 05:44