简体   繁体   English

使用ASP.NET MVC操作方法

[英]Consuming ASP.NET MVC action methods

current situation: an ASP.NET MVC web site with a number of controllers and action methods, and views to allow adding recipes. 当前情况:一个ASP.NET MVC网站,其中包含许多控制器和操作方法,以及允许添加配方的视图。

Now, I have to create a WPF application that acts as a UI to add recipes (same as the web site). 现在,我必须创建一个WPF应用程序,作为添加配方的UI(与网站相同)。

My question is: can I use ASP.NET MVC site to expose service operations that are consumed by the WPF application (and how can this be done)? 我的问题是:我可以使用ASP.NET MVC站点来公开WPF应用程序使用的服务操作(以及如何完成)? Or should I better create dedicated WCF services for that, and have the WPF AND the ASP.NET MVC site consume these services? 或者我应该为此更好地创建专用的WCF服务,并让WPF和ASP.NET MVC站点使用这些服务?

Thanks, Ludwig 谢谢,路德维希

I have successfully used MVC controllers and actions to service both HTML views from the browser, as well as external applications. 我已经成功地使用MVC控制器和操作来为浏览器的HTML视图以及外部应用程序提供服务。 It works well, but as it stands you'd need a little bit of tooling: 它运作良好,但就目前而言,您需要一些工具:

  1. I have an action filter that returns the Model resulting from an action in a format the client requested (by inspecting the Accepts header, looking for either application/json or text/xml). 我有一个动作过滤器,它返回由客户端请求的格式的动作产生的模型(通过检查Accepts标头,查找application / json或text / xml)。 So, I can serialize the resulting model as JSON or XML (I prefer JSON). 所以,我可以将生成的模型序列化为JSON或XML(我更喜欢JSON)。
  2. You will need to find or create a simple API in your client application to create WebRequests to your actions, and then process the results. 您需要在客户端应用程序中查找或创建一个简单的API,以便为您的操作创建WebRequests,然后处理结果。 I created a simple API that can POST or GET, and then deserializes any resulting JSON into an object (using JSON.NET). 我创建了一个可以POST或GET的简单API,然后将任何生成的JSON反序列化为一个对象(使用JSON.NET)。 There are REST client APIs out there you could use for this. 您可以使用REST客户端API。

However, you could avoid some of this extra tooling if you go the WCF-REST route. 但是,如果您使用WCF-REST路由,则可以避免一些额外的工具。 Better yet, I'd look into ASP.NET MVC 4's WebApi feature (which is what I will be migrating to). 更好的是,我将研究ASP.NET MVC 4的WebApi功能(我将迁移到这个功能)。

For the record, I think WCF is powerful, but our organization has grown tired of how complicated it can be to turn all the knobs and hit all the switches to get it working right, and set up easily from one install to the other. 为了记录,我认为WCF功能强大,但我们的组织已经厌倦了转动所有旋钮并点击所有开关以使其正常工作并从一个安装轻松设置到另一个安装的复杂程度。 MVC, on the other hand, just works... and since we already use it to service our HTML views, it's a real joy to only have to add a little extra code to have it handle service calls, too. 另一方面,MVC正常工作......由于我们已经使用它来为我们的HTML视图提供服务,所以只需要添加一些额外的代码来处理服务调用也是一件非常愉快的事情。 Just a personal preference, of course. 当然只是个人偏好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM