一.直接使用ContentResult
return new ContentResult { ContentType = "application/json", Content = JsonConvert.SerializeObject(obj, new JsonSerializerSettings { ContractResolver = new DefaultContractResolver() }) };
二 使用MVC ResultFilter
public class JsonSerializeFilter : IResultFilter { public void OnResultExecuted(ResultExecutedContext context) { } public void OnResultExecuting(ResultExecutingContext context) { var result = context.Result as ObjectResult; context.Result = new ObjectResult(JsonConvert.SerializeObject( result?.Value, new JsonSerializerSettings { ContractResolver = new DefaultContractResolver() } )); } }
//使用
[TypeFilter(typeof(JsonSerializeFilter))]
public async Task<DataTable> Linkdata(string t, string f, string q = "")
{//todo
}