gpt4 book ai didi

asp.net-mvc - 对普通 Controller 使用 JSON.NET 格式化程序

转载 作者:行者123 更新时间:2023-12-04 10:47:16 24 4
gpt4 key购买 nike

当从 Controller (MVC 4 RC)返回 Json 时,我想修改 Json 以对属性使用驼峰式 shell ,为此我尝试设置 GlobalConfiguration.Formatters.JsonFormatter(不确定这是否正确...don '没有代码在我面前),但这似乎不会影响 Controller.Json 方法输出的 Json。

环顾四周后,似乎这种方法只会影响 Web API Controller 等。这是真的吗?另外,是否可以更改 Controller.Json() 方法来实现这一点?

最佳答案

就像@rouen 建议的那样,创建了自己的 JsonDotNetResult。

这是我在我的项目中的一个:

public class JsonNetResult : ActionResult
{
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }
public object Data { get; set; }
public int StatusCode { get; set; }

public JsonSerializerSettings SerializerSettings { get; set; }

public JsonNetResult()
{
SerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
}

public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");

var response = context.HttpContext.Response;

response.StatusCode = StatusCode;
response.ContentType = string.IsNullOrEmpty(ContentType) ? "application/json" : ContentType;

if ((StatusCode >= 400) && (StatusCode <= 599))
response.TrySkipIisCustomErrors = true;

if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;

if (Data == null)
return;

var formatting = Formatting.None;

#if DEBUG
formatting = Formatting.Indented;
#endif

var writer = new JsonTextWriter(response.Output) { Formatting = formatting };

var serializer = JsonSerializer.Create(SerializerSettings);
serializer.Serialize(writer, Data);

writer.Flush();
}
}

然后我有我自己的 baseController 继承自给我 JsonDotNet(object viewModel) 类型的方法。

例如。
protected JsonNetResult JsonNet(object data = null, int statusCode = (int)HttpStatusCode.OK, string contentType = null)
{
return new JsonNetResult
{
Data = data,
StatusCode = statusCode,
ContentType = contentType
};
}

protected JsonNetResult JsonNetForbidden()
{
return JsonNet(statusCode: (int)HttpStatusCode.Forbidden);
}

protected JsonNetResult JsonNetNotFound()
{
return JsonNet(statusCode: (int)HttpStatusCode.NotFound);
}

protected JsonNetResult JsonNetNoContent()
{
return JsonNet(statusCode: (int)HttpStatusCode.NoContent);
}

protected JsonNetResult JsonNetCreated(object data)
{
return JsonNet(data, (int)HttpStatusCode.Created);
}

protected JsonNetResult JsonNetReload()
{
return JsonNet(new { reload = true });
}

protected JsonNetResult JsonNetRedirect(string url = null, string contentType = null)
{
return JsonNet(new { redirectUrl = url }, contentType: contentType);
}

protected JsonNetResult JsonNetClientError(ErrorDictionary errors)
{
return JsonNet(new { Errors = errors }, (int)HttpStatusCode.BadRequest);
}

protected JsonNetResult JsonNetUnauthorized()
{
return JsonNet(null, (int)HttpStatusCode.Unauthorized);
}

protected JsonNetResult JsonNetFlashMessage(string message)
{
return JsonNet(new { flashMessage = message });
}

关于asp.net-mvc - 对普通 Controller 使用 JSON.NET 格式化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11629328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com