ASP.net MVC 3 - 使用JSON发布的数据OnActionExecuting [英] ASP.net MVC 3 - Getting posted JSON data in OnActionExecuting

查看:119
本文介绍了ASP.net MVC 3 - 使用JSON发布的数据OnActionExecuting的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用数据字段传递JSON字符串化值公布进出口数据发布使用jQuery中的$就法,指定数据的肌动蛋白。

Im posting data to an actin using the $.ajax method in jquery, specifying the data to be posted using the data field to pass the JSON stringified values.

这些张贴的行动OK,但我不能在OnActionExecuting动作过滤器让他们(它们不是窗体或PARAMS集合的一部分)。有没有什么办法让他们,如果没有,你能告诉份额为什么不呢?

These are posted to the action OK but I cant get them in an OnActionExecuting action filter (they're not part of the Forms or Params collections). Is there any way to get them and if not, could you tell share why not?

推荐答案

如果你的动作发生的模型:

If your action takes a model:

[HttpPost]
public ActionResult About(SomeViewModel model)
{
    return Json(model);
}

您可以直接将此参数的值,因为<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonvalueproviderfactory.aspx\">JsonValueProviderFactory早就解析吧:

you could directly this parameter value because the JsonValueProviderFactory would have parsed it already:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);
    SomeViewModel model = filterContext.ActionParameters["model"] as SomeViewModel;
}

如果没有模型(为什么不存在呢?),你可以从请求流中读取的JSON:

If there is no model (why wouldn't there be?) you could read the JSON from the request stream:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);
    filterContext.HttpContext.Request.InputStream.Position = 0;
    using (var reader = new StreamReader(filterContext.HttpContext.Request.InputStream))
    {
        string json = reader.ReadToEnd();
    }
}

这篇关于ASP.net MVC 3 - 使用JSON发布的数据OnActionExecuting的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆