用于 ASP.NET MVC 2 异步操作的强类型 API
有人尝试过为 ASP.NET MVC 2 异步操作创建强类型 API 吗?
此致, 阿列克谢·扎哈罗夫
Have anybody tried to create strongly typed API for ASP.NET MVC 2 async actions?
Best regards,
Alexey Zakharov
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我没理解错的话,我相信我们最近做了类似的事情。我们使用 ASP.NET MVC 2 Futures 库中的 JsonValueProviderFactory 来实现强类型操作(更多信息请参见 haaked.com)。
对于异步操作,我们有与此类似的东西:
只需使用以下命令对操作进行 POST:
有一个很棒的 Firefox 插件可以帮助测试这个名为“REST Client”的插件,我也推荐它。
希望这有帮助。
I believe we have recently done something like this if I've understood you correctly. We used the JsonValueProviderFactory from the ASP.NET MVC 2 Futures library to achieve the strongly typed action (more info on this on haaked.com).
For the async action we have something similar to this:
and just make a POST to the action with:
There is a great Firefox plug-in to help with testing this called 'REST Client' that I would recommend also.
Hope this helps.
MVC 可以很好地做到这一点,但我从您提供的代码示例中了解到,您需要一个基于 json 的 API。
Microsoft 正是针对这种情况创建了 WebAPI,它是强类型的,并且在底层设计中遵循 MVC 模式,而且它也运行良好,并且可以在同一 Web 应用程序中与 MVC 一起使用。
但是:
如果您愿意,您可以使用简单的 Json 序列化程序包从 MVC 控制器操作提供序列化对象,并简单地返回结果字符串,ActionResult 也支持这种情况,并且对于帖子,使用内置的元数据基础设施来验证您的类型特定数据从帖子中给你你想要的类型安全。
将 MVC 控制器视为休息端点也是可能的,这只是以正确的方式构建控制器以支持典型的 REST 调用的问题。
不过,我强烈建议使用 WebAPI,因为它更适合 API 场景。
MVC could well do this but my understanding from the code sample you have provided is that you want a json based API.
Microsoft created WebAPI for exactly this situation, it's both strongly typed and follows the MVC pattern in its underlying design, but it also works well and can be used along side MVC within the same web application.
However:
If you want to you can provide serialised objects from an MVC controller action using a simple Json serialiser package and simply returning the resulting string, ActionResult supports this scenario too, and for posts use the built in metadata infrastructure to validate your type specific data from the post giving you your desired type safety.
Treating the MVC controller like a rest endpoint is also possible its just a matter of contructing the controller in the right fashion to support typical REST calls.
I would however strongly recommend using WebAPI for this as it is the better fit for API scenarios.