为什么人们在MVC UI项目中添加Web API控制器而不创建单独的Web API项目? [英] Why people adds web API controllers in MVC UI project and not create a separate web API project?

查看:86
本文介绍了为什么人们在MVC UI项目中添加Web API控制器而不创建单独的Web API项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中看到我刚刚开始工作,Web API控制器被添加到MVC UI项目中,并且没有为应用程序创建单独的Web API项目。



我尝试了什么:



当我向应用程序的建筑师询问此事时,他刚回答它已经在应用程序中构建,因此他没有修改架构。 
我没有得到这种在MVC UI项目中使用Web API控制器的架构风格的实际原因。

请有人能解释一下它的优点,如果这是一个很好的做法。

解决方案

< blockquote>对你的项目一无所知,我会给你我的观点。



虽然你的同事说的是正确的,但web api控制器作为MVC的一部分包含在内项目(在功能方面,MVC控制器和web api控制器非常相似)...你的同事可能会互换地使用它们而没有意识到或者可能只是不关心,因为它们用于相同的目的。





如果你去那里并将所有web api继承的控制器从ApiController(例如:MyWebApiController:ApiController)更改为Controller,我会打赌很多钱(将它们从web api转换为mvc控制器)它可能仍然可以正常工作。如果你做文件上传或其他事情可能会有区别,但简单/基本的crud命令可能看不出任何问题。



所以关键,它真的重要吗?可能不是,但那对我从未见过的项目做了很多假设。有优势吗?如果它是一个只是Web UI的简单独立项目,我能看到的唯一优势就是只有一个项目/解决方案可以处理(对我来说这不是什么大不了的事)。



至于这是一个很好的做法,如果你正在构建一个具有多个UI的应用程序(想想网络,桌面,移动),我可能不会紧紧地结合与已经完成的API层一样的Web UI项目,我将其拆分为单独的项目,并将API作为自己的项目和MVC作为自己的项目引入。



可能在开发路径上太远了,尝试拆分它只是在这一点上被视为技术债务但我怀疑在mvc项目中使用web api控制器是否有任何伤害。


I have seen in the application I have just started working that Web API controllers are added to MVC UI project and there is no separate Web API project created for the application.

What I have tried:

When I asked architect of the application about this, he just answered that it was already built in application and thus he did not modify the architecture. 
I did not get the actual reason for this architectural style of having Web API controller in MVC UI Project.

Please someone can explain me the advantage of it and if it is a good practice to follow this.

解决方案

Knowing nothing about your project, I'll give you my view.

While what your coworker said is correct, web api controllers are included as part of the MVC project (in terms of functionality, MVC controllers and web api controllers are very similar)...your coworker might be using them interchangeably without realizing or possibly just doesn't care since they serve the same purpose.


I'd bet a lot of money if you went in there and changed all of the web api inherited controllers from ApiController (Ex: MyWebApiController : ApiController) to Controller (turning them from web api to mvc controllers) it would probably still work the same. There may be differences if your doing file uploads or other things but simple/basic crud commands would likely see no issue.

So the point, does it really matter? Probably not but thats making a lot of assumptions about a project I have never seen. Is there advantages? If its a simple standalone project that is only the web UI, the only advantage I can see is only one project/solution to deal with (to me this isn't a big deal anyways).

As for this being a good practice, If you were building an app that would have multiple UI's (think web, desktop, mobile) I probably wouldn't tightly couple the web UI project with the API layer like what has been done, i'd split that out into separate projects and introduce the API as its own project and the MVC as its own project.

It may be too far down the dev path that attempting to split this apart is just considered technical debt at this point for you but I doubt using web api controllers in an mvc project is doing any harm.


这篇关于为什么人们在MVC UI项目中添加Web API控制器而不创建单独的Web API项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆