wpf - 在 WPF 应用程序中使用 ASP.NET MVC Controller 操作

标签 wpf asp.net-mvc asp.net-mvc-4 asp.net-web-api asp.net-web-api-routing

我正在设计一个具有两个 UI 的网络应用程序 - 一个是传统网页(HTML View ),另一个是 WPF 应用程序。我知道,为了实现分离,最好设计如下所示,一个由 MVC 应用程序和 WPF 应用程序使用的 Web API。

enter image description here

但是我是一个时间紧迫的人,我想知道我是否可以只使用如下所示的传统 MVC 设计。此外,我可能有更多的非 CRUD 操作,如果我要使用 WebAPI,将不得不以 RPC 样式实现,增加了 webAPI 的复杂性(更多工作,更多时间)。

enter image description here

我唯一的问题是 - 可以在 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 响应转换为更适合您的应用程序的内容。

这不是强制性的,但可以节省一些时间!

话虽这么说,但要注意所有需要身份验证的场景,这可能会让事情变得更难:

  • 如果您需要表单例份验证,那么您可以检索您的身份验证 首先是 cookie,然后将其包含在以后的 HTTP 请求中(请参阅 这个post了解更多信息)
  • 如果您需要 Windows 身份验证,则必须提供您的凭据(请参阅来自 asp.net 网站的 page)

关于wpf - 在 WPF 应用程序中使用 ASP.NET MVC Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362179/

相关文章:

mono - 如何在 Mono 2.10.8 上成功托管 ASP.NET MVC4 RC

oauth - ASP.NET MVC4 谷歌 oAuth

c# - 使用 LINQ2SQL 在 ASP.NET MVC 中的各种模型存储库之间共享数据上下文

asp.net-mvc - mvc View 中模型空引用异常

c# - 将 Application.Current.Dispatcher 用于调度程序线程外的事件

c# - WPF 数据绑定(bind)堆栈面板

asp.net mvc ajax上传解决方案?

c# - Kendo UI 小部件的完整本地化

.net - WPF - 组合框如何知道何时关闭下拉菜单

禁用时的 WPF 文本框样式