Asp.net的Web API - 返回从actionfilter数据 [英] Asp.net Web API - return data from actionfilter

查看:325
本文介绍了Asp.net的Web API - 返回从actionfilter数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从WEP API actionfilter返回一个JSON对象。
我怎样才能做到这一点?

我可以返回从操作的对象,但我需要从actionfilter回报一定条件下的一些数据。

先谢谢了。


编辑:1
当我改变了code像下面这样,浏览器加载仍然没有任何反应和超时错误结束。

 公共类ValidationActionFilter:ActionFilterAttribute
{    公共覆盖无效OnActionExecuting(HttpActionContext ActionContext中)
    {
        VAR的ModelState = actionContext.ModelState;
        如果(!modelState.IsValid)
        {
            清单<串GT; ARR =新的List<串GT;();
            的foreach(在modelState.Keys VAR键)
            {
                VAR状态=的ModelState [关键]
                如果(state.Errors.Any())
                {
                    字符串ER = state.Errors.First()的ErrorMessage。
                    如果(!string.IsNullOrEmpty(ER))
                    {
                        arr.Add(ER);
                    }
                }
            }           无功输出=新的结果(){状态= Status.Error.ToString(),数据= NULL,消息= ARR};
            actionContext.Response = actionContext.Request.CreateResponse(的HTTPStatus code.BadRequest,输出actionContext.ControllerContext.Configuration.Formatters.JsonFormatter);
        }
    }
}


解决方案

所有你需要的是分配响应:

 公共类MyActionFilterAttribute:ActionFilterAttribute
{
    公共覆盖无效OnActionExecuting(HttpActionContext ActionContext中)
    {
        actionContext.Response = actionContext.Request.CreateResponse(
            的HTTPStatus code.OK,
            新富{=酒吧},
            actionContext.ControllerContext.Configuration.Formatters.JsonFormatter
        );
    }
}

假设下面的控制器动作:

  [MyActionFilter]
公共字符串获得()
{
    返回确定;
}

这个自定义操作过滤器会短路直接操作的执行,并返回我们提供的答复。

I want to return a json object from the wep api actionfilter. How can I achieve this?

I can return the object from action but I need to return some data from the actionfilter on some condition.

Thanks in advance.


Edit: 1 When I changed the code like the following, the browser still loading without any response and ends in timeout error.

  public class ValidationActionFilter : ActionFilterAttribute
{

    public override void OnActionExecuting(HttpActionContext actionContext)
    {


        var modelState = actionContext.ModelState;
        if (!modelState.IsValid)
        {
            List<string> arr = new List<string>();
            foreach (var key in modelState.Keys)
            {
                var state = modelState[key];
                if (state.Errors.Any())
                {
                    string er = state.Errors.First().ErrorMessage;
                    if (!string.IsNullOrEmpty(er))
                    {
                        arr.Add(er);
                    }
                }
            }               

           var output =  new Result() { Status = Status.Error.ToString(), Data = null, Message = arr };
            actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, output, actionContext.ControllerContext.Configuration.Formatters.JsonFormatter);
        }     


    }
}

解决方案

All you need is to assign the Response:

public class MyActionFilterAttribute: ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        actionContext.Response = actionContext.Request.CreateResponse(
            HttpStatusCode.OK, 
            new { foo = "bar" }, 
            actionContext.ControllerContext.Configuration.Formatters.JsonFormatter
        );
    }
}

Assuming the following controller action:

[MyActionFilter]
public string Get()
{
    return "OK";
}

this custom action filter will short-circuit the execution of the action and directly return the response we provided.

这篇关于Asp.net的Web API - 返回从actionfilter数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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