调用独立Web API以在ASP.NET MVC中填充ViewModel

时间:2022-12-16 13:22:39

Lets say I have a standalone ASP.NET Core Web API as its own project (since I don't want to merge it w/ my MVC app)

可以说我有一个独立的ASP.NET Core Web API作为自己的项目(因为我不想与我的MVC应用程序合并)

I now want to create an MVC web application (another project, but same solution) that will make calls to the API. I am aware that for ajax requests I will directly call the API through JavaScript which is straight forward. However, I am a bit confused as to how the MVC controller would make calls to the same API to populate the ViewModel and send to the View when a user requests a page i.e. http://myurl/books/1

我现在想要创建一个MVC Web应用程序(另一个项目,但相同的解决方案),它将调用API。我知道对于ajax请求,我将通过JavaScript直接调用API。但是,我有点困惑的是MVC控制器如何调用相同的API来填充ViewModel并在用户请求页面时发送到View,即http:// myurl / books / 1

I am aware that if I was using something like Angular which is fully client-side I wouldn't be running into this issue.

我知道如果我使用像Angular这样完全客户端的东西,我就不会遇到这个问题。

1 个解决方案

#1


1  

That's a good question. There's three different approaches that I can think of:

这是个好问题。我能想到三种不同的方法:

  • The first is to call the same API from your MVC controller with a HTTPClient object. Might seem a bit weird calling "yourself" on the server side but it works. In my opinion this isn't a great solution, because you're losing type-checking and you'll also making an unnecessary call over the network, and you also have to parse and convert the response back from WebAPI to your .net objects.

    第一种是使用HTTPClient对象从MVC控制器调用相同的API。在服务器端调用“你自己”可能看起来有点奇怪但是它有效。在我看来,这不是一个很好的解决方案,因为你正在丢失类型检查,你也会通过网络进行不必要的调用,你还需要解析并将响应从WebAPI转换回你的.net对象。

  • The second approach is to move all of the logic that's inside the API into a new project and compile it all down to a DLL. Your Web Api can then call the code inside the library and your MVC project can do it too. A pretty good approach.

    第二种方法是将API内部的所有逻辑移动到一个新项目中,并将其全部编译为DLL。然后,您的Web Api可以调用库中的代码,您的MVC项目也可以执行此操作。一个很好的方法。

  • The third approach is to do what you've suggested - don't populate the Model at all on the server side inside your MVC project and just do it all from the client side. Yes, this will work great with Angular but you can also do this yourself using bog-standard JQuery or Knockout. This approach means much more client side javascript to write (use Typescript!) but you'll end up with a much nicer separation of concerns and cleaner solution.

    第三种方法是按照你的建议做 - 不要在MVC项目内的服务器端填充模型,只需从客户端完成。是的,这对Angular很有用,但你也可以使用bog标准的JQuery或Knockout自己做。这种方法意味着要编写更多的客户端javascript(使用Typescript!),但最终会得到更好的关注点分离和更清晰的解决方案。

#1


1  

That's a good question. There's three different approaches that I can think of:

这是个好问题。我能想到三种不同的方法:

  • The first is to call the same API from your MVC controller with a HTTPClient object. Might seem a bit weird calling "yourself" on the server side but it works. In my opinion this isn't a great solution, because you're losing type-checking and you'll also making an unnecessary call over the network, and you also have to parse and convert the response back from WebAPI to your .net objects.

    第一种是使用HTTPClient对象从MVC控制器调用相同的API。在服务器端调用“你自己”可能看起来有点奇怪但是它有效。在我看来,这不是一个很好的解决方案,因为你正在丢失类型检查,你也会通过网络进行不必要的调用,你还需要解析并将响应从WebAPI转换回你的.net对象。

  • The second approach is to move all of the logic that's inside the API into a new project and compile it all down to a DLL. Your Web Api can then call the code inside the library and your MVC project can do it too. A pretty good approach.

    第二种方法是将API内部的所有逻辑移动到一个新项目中,并将其全部编译为DLL。然后,您的Web Api可以调用库中的代码,您的MVC项目也可以执行此操作。一个很好的方法。

  • The third approach is to do what you've suggested - don't populate the Model at all on the server side inside your MVC project and just do it all from the client side. Yes, this will work great with Angular but you can also do this yourself using bog-standard JQuery or Knockout. This approach means much more client side javascript to write (use Typescript!) but you'll end up with a much nicer separation of concerns and cleaner solution.

    第三种方法是按照你的建议做 - 不要在MVC项目内的服务器端填充模型,只需从客户端完成。是的,这对Angular很有用,但你也可以使用bog标准的JQuery或Knockout自己做。这种方法意味着要编写更多的客户端javascript(使用Typescript!),但最终会得到更好的关注点分离和更清晰的解决方案。