我正在设计一个具有两个 UI 的网络应用程序 - 一个是传统网页(HTML View ),另一个是 WPF 应用程序。我知道,为了实现分离,最好设计如下所示,一个由 MVC 应用程序和 WPF 应用程序使用的 Web API。
但是我是一个时间紧迫的人,我想知道我是否可以只使用如下所示的传统 MVC 设计。此外,我可能有更多的非 CRUD 操作,如果我要使用 WebAPI,将不得不以 RPC 样式实现,增加了 webAPI 的复杂性(更多工作,更多时间)。
我唯一的问题是 - 可以在 WPF 应用程序中使用 MVC 操作吗?如果是,我是否需要使用任何特殊的 API 来执行此操作,如 this post 中所述还是新的 HttpClient 包就足够了?
最佳答案
是的,就像任何其他能够发送 HTTP 请求和接收响应的应用程序一样,WPF 应用程序能够使用 ASP MVC Controller 操作。
毕竟,ASP MVC 框架只是解析 URL,推导出路由信息(区域、 Controller 、 Action 、参数等),最后在向您发送 HTTP 响应之前调用带有参数的关联 Action 。
来自 MSDN :
In contrast, user interaction with ASP.NET MVC applications is organized around controllers and action methods. The controller defines action methods. Controllers can include as many action methods as needed. Action methods typically have a one-to-one mapping with user interactions. Examples of user interactions include entering a URL into the browser, clicking a link, and submitting a form. Each of these user interactions causes a request to be sent to the server. In each case, the URL of the request includes information that the MVC framework uses to invoke an action method.
因此,即使是一个简单的 HttpClient 也足以与 ASP MVC Controller 操作进行交互。 但您也可以使用您选择的框架或库来帮助您构建 HTTP 请求和/或将 HTTP 响应转换为更适合您的应用程序的内容。
这不是强制性的,但可以节省一些时间!
话虽这么说,但要注意所有需要身份验证的场景,这可能会让事情变得更难:
关于wpf - 在 WPF 应用程序中使用 ASP.NET MVC Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362179/