return JSON from Asp.net web api 2 过滤器属性响应的正确方法
Right way to return JSON from Asp.net web api 2 filter attribute response
我正在使用 ASP.NET Web Api 2. 我创建了一个操作过滤器,用于检查传入请求,然后 return 根据特定条件返回响应。
public override void OnAuthorization(HttpActionContext actionContext)
{
var req = actionContext.Request;
if (!req.Headers.Contains("x-key") || req.Headers.GetValues("x-key") == null)
{
actionContext.Response = req.CreateResponse(HttpStatusCode.Unauthorized);
actionContext.Response.Content = new StringContent("Token required", Encoding.UTF8, "text/html");
}
}
我想知道这是 return JSON 响应 的正确方法吗?我想 return 一个自定义对象 (
var rebBody = new {message = "Unauthorized", payload = "", response = "401"};
) 作为响应正文中的 JSON。
使用这样的东西有意义吗:
var v = new {message = "Unauthorized", payload = "", response = "401"};
actionContext.Response.Content = new ObjectContent<object>(v, new JsonMediaTypeFormatter(), "application/json");
也许是这样,
public override void OnAuthorization(HttpActionContext actionContext)
{
var req = actionContext.Request;
if (!req.Headers.Contains("x-key") || req.Headers.GetValues("x-key") == null)
{
HttpResponseMessage responseMessage = new HttpResponseMessage()
{
Content = new StringContent("{\"message\":\"Unauthorized\", \"payload\":\"\",\"response\":\"401\"}")
};
responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
actionContext.Response = responseMessage;
}
}
或者像这样:
public override void OnAuthorization(HttpActionContext actionContext)
{
var req = actionContext.Request;
if (!req.Headers.Contains("x-key") || req.Headers.GetValues("x-key") == null)
{
var v = new { message = "Unauthorized", payload = "", response = "401" };
HttpResponseMessage responseMessage = new HttpResponseMessage()
{
StatusCode = HttpStatusCodes.Unauthorized,
Content = new StringContent(JsonConvert.SerializeObject(v))
};
responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
actionContext.Response = responseMessage;
}
}
您可以使用 CreateResponse
的另一个重载:
public static HttpResponseMessage CreateResponse<T>(
this HttpRequestMessage request,
T value)
例如:
var content = new { Property = 1 };
request.CreateResponse(content);
我正在使用 ASP.NET Web Api 2. 我创建了一个操作过滤器,用于检查传入请求,然后 return 根据特定条件返回响应。
public override void OnAuthorization(HttpActionContext actionContext)
{
var req = actionContext.Request;
if (!req.Headers.Contains("x-key") || req.Headers.GetValues("x-key") == null)
{
actionContext.Response = req.CreateResponse(HttpStatusCode.Unauthorized);
actionContext.Response.Content = new StringContent("Token required", Encoding.UTF8, "text/html");
}
}
我想知道这是 return JSON 响应 的正确方法吗?我想 return 一个自定义对象 (
var rebBody = new {message = "Unauthorized", payload = "", response = "401"};
) 作为响应正文中的 JSON。
使用这样的东西有意义吗:
var v = new {message = "Unauthorized", payload = "", response = "401"};
actionContext.Response.Content = new ObjectContent<object>(v, new JsonMediaTypeFormatter(), "application/json");
也许是这样,
public override void OnAuthorization(HttpActionContext actionContext)
{
var req = actionContext.Request;
if (!req.Headers.Contains("x-key") || req.Headers.GetValues("x-key") == null)
{
HttpResponseMessage responseMessage = new HttpResponseMessage()
{
Content = new StringContent("{\"message\":\"Unauthorized\", \"payload\":\"\",\"response\":\"401\"}")
};
responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
actionContext.Response = responseMessage;
}
}
或者像这样:
public override void OnAuthorization(HttpActionContext actionContext)
{
var req = actionContext.Request;
if (!req.Headers.Contains("x-key") || req.Headers.GetValues("x-key") == null)
{
var v = new { message = "Unauthorized", payload = "", response = "401" };
HttpResponseMessage responseMessage = new HttpResponseMessage()
{
StatusCode = HttpStatusCodes.Unauthorized,
Content = new StringContent(JsonConvert.SerializeObject(v))
};
responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
actionContext.Response = responseMessage;
}
}
您可以使用 CreateResponse
的另一个重载:
public static HttpResponseMessage CreateResponse<T>(
this HttpRequestMessage request,
T value)
例如:
var content = new { Property = 1 };
request.CreateResponse(content);