用于 ASP.NET MVC 2 异步操作的强类型 API

发布于 2024-09-01 18:27:29 字数 66 浏览 9 评论 0原文

有人尝试过为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

但可醉心 2024-09-08 18:27:29

如果我没理解错的话,我相信我们最近做了类似的事情。我们使用 ASP.NET MVC 2 Futures 库中的 JsonValueProviderFactory 来实现强类型操作(更多信息请参见 haaked.com)。

对于异步操作,我们有与此类似的东西:

[HandleError]
public class HomeController : AsyncController
{
    [HttpPost]
    public void IndexAsync(Person person)
    {
        DoSomething();
    }

    public ActionResult IndexCompleted()
    {
        return View();
    }
}

public class Person
{
    public string Forename { get; set; }
    public string Surname { get; set; }
}

只需使用以下命令对操作进行 POST:

{"Forename": "Cheesy", "Surname": "Goat"}

有一个很棒的 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:

[HandleError]
public class HomeController : AsyncController
{
    [HttpPost]
    public void IndexAsync(Person person)
    {
        DoSomething();
    }

    public ActionResult IndexCompleted()
    {
        return View();
    }
}

public class Person
{
    public string Forename { get; set; }
    public string Surname { get; set; }
}

and just make a POST to the action with:

{"Forename": "Cheesy", "Surname": "Goat"}

There is a great Firefox plug-in to help with testing this called 'REST Client' that I would recommend also.

Hope this helps.

纵情客 2024-09-08 18:27:29

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文