When my json gets returned from the server, it is in CamelCase, but I need lowercase. I have seen a lot of solutions for ASP.NET Web API and Core, but nothing for ASP.NET MVC 5.
[HttpGet]
public JsonResult Method()
{
var vms = new List<MyViewModel>()
{
new MyViewModel()
{
Name = "John Smith",
}
};
return Json(new { results = vms }, JsonRequestBehavior.AllowGet);
}
I want "Names" to be lowercase.
The best solution I have for this is to override the default Json method to use Newtonsoft.Json and set it to use camelcase by default.
First thing is you need to make a base controller if you don't have one already and make your controllers inherit that.
public class BaseController : Controller {
}
Next you create a JsonResult class that will use Newtonsoft.Json :
public class JsonCamelcaseResult : JsonResult
{
private static readonly JsonSerializerSettings _settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new List<JsonConverter> { new StringEnumConverter() }
};
public override void ExecuteResult(ControllerContext context)
{
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = !String.IsNullOrEmpty(this.ContentType) ? this.ContentType : "application/json";
response.ContentEncoding = this.ContentEncoding ?? response.ContentEncoding;
if (this.Data == null)
return;
response.Write(JsonConvert.SerializeObject(this.Data, _settings));
}
}
Then in your BaseController you override the Json method :
protected new JsonResult Json(object data)
{
return new JsonCamelcaseResult
{
Data = data,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
So in the end in your original action returning JSON you just keep it the same and the properties will be camelcased (propertyName) instead of pascalcased (PropertyName) :
[HttpGet]
public JsonResult Method()
{
var vms = new List<MyViewModel>()
{
new MyViewModel()
{
Name = "John Smith",
}
};
return Json(new { results = vms });
}
Related
my deserialize Dictionary's key results in "brand[0]" when I send in "brand" to the api.
I have a class like this:
public class SearchRequest
{
public bool Html { get; set; } = false;
public Dictionary<string, HashSet<string>> Tags { get; set; }
}
// MVC Controller
[HttpPost]
public ActionResult Index(SearchRequest searchRequest)
{
...
}
And a json request like this that I post to the controller:
{
"html": true,
"tags": {
"brand": [
"bareminerals"
]
}
}
The binding seams to work and the searchRequest object is created but the resulting dictionary dose not have the key "brand" in it but insted the key "brand[0]" how can I preserve the real values I send in?
Edit: I need tags to be able to contain multiple tags, with multiple options, this was a simpel example.
One soulution to my problem is to create a custom model bind, so this is what am using now, but I dont understand why I need to, and I feel like there should be a easyer way? But am gonna leve It here anyhow.
public class FromJsonBodyAttribute : CustomModelBinderAttribute
{
public override IModelBinder GetBinder()
{
return new JsonModelBinder();
}
private class JsonModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var stream = controllerContext.HttpContext.Request.InputStream;
stream.Position = 0;
using (var reader = new StreamReader(stream))
{
var checkoutOrderDataStr = reader.ReadToEnd();
return JsonConvert.DeserializeObject(checkoutOrderDataStr, bindingContext.ModelType);
}
}
}
}
I'm not sure what is going on with your setup. You should not need a custom binder. I still think the problem is most likely with your calling code - whatever you're using as a client.
I'm using Asp.net Core 3.1. Here's what I threw together as a quick test.
Created Asp.net Core web application template with MVC. I declared two classes - a request POCO and a result POCO. The request was your class:
public class SearchRequest
{
public bool Html { get; set; } = false;
public Dictionary<string, HashSet<string>> Tags { get; set; }
}
The result was the same thing with a datetime field added just for the heck of it:
public class SearchResult : SearchRequest
{
public SearchResult(SearchRequest r)
{
this.Html = r.Html;
this.Tags = r.Tags;
}
public DateTime RequestedAt { get; set; } = DateTime.Now;
}
I Added a simple post method on the default HomeController.
[HttpPost]
public IActionResult Index([FromBody] SearchRequest searchRequest)
{
return new ObjectResult(new SearchResult(searchRequest));
}
I added a console Application to the solution to act as a client. I copied the two class definitions into that project.
I added this as the main method. Note you can either have the camel casing options on the request or not - asp.net accepted either.
static async Task Main(string[] _)
{
var tags = new[] { new { k = "brand", tags = new string[] { "bareminerals" } } }
.ToDictionary(x => x.k, v => new HashSet<string>(v.tags));
var request = new SearchRequest() { Html = true, Tags = tags };
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
var json = JsonSerializer.Serialize(request, options);
Console.WriteLine(json);
using (var client = new HttpClient())
{
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync("http://localhost:59276", content);
response.EnsureSuccessStatusCode();
var data = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<SearchResult>(data, options);
Console.WriteLine(data);
var keysSame = Enumerable.SequenceEqual(request.Tags.Keys, result.Tags.Keys);
var valuesSame = Enumerable.SequenceEqual(request.Tags.Values.SelectMany(x => x),
result.Tags.Values.SelectMany(x=>x));
Console.WriteLine($"Keys: {keysSame} Values: {valuesSame}");
}
}
This outputs:
{"html":true,"tags":{"brand":["bareminerals"]}}
{"requestedAt":"2020-10-30T19:22:17.8525982-04:00","html":true,"tags":{"brand":["bareminerals"]}}
Keys: True Values: True
My Custom Action Filter Attribute to convert a JSON response of MVC core webApi from "camelCase" to "pascalCase" is not working.
Tried using:
services.AddMvc()
.AddJsonOptions(options =>
options.SerializerSettings.ContractResolver = new DefaultContractResolver());
However, this kind of global setting changes all response to Pascal case. I want to change only a limited API response to Pascal case.
Custom ActionFilterAttribute:
public class CancelCamelCaseResolverConfigurationAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext context)
{
base.OnResultExecuted(context);
var objectResult = context.Result as ObjectResult;
if (objectResult != null)
{
objectResult.Formatters.Clear();
objectResult.Formatters.Add(new JsonOutputFormatter(
new JsonSerializerSettings()
{
Formatting = Formatting.None,
ContractResolver = new DefaultContractResolver()
}, ArrayPool<char>.Shared));
}
}
}
And use in the webApi controller:
[CancelCamelCaseResolverConfiguration]
public class FrmMainSearchController : AtlasApiController<FrmMainSearchController>
{
/*Api*/
}
Expected result:
searchCriteria = [{Key: "xx", Value: "yy"}]
Actual result:
searchCriteria = [{key: "xx", value: "yy"}]
You're almost there: You need override the OnActionExecuted() method instead of the OnResultExecuted().
It's too late to change the formatters when the OnResultExecuted() filter method is invoked.
How to fix:
Override the OnResultExecuted method so that the formatter is changed before the result execution:
public override void OnResultExecuted(ResultExecutedContext context)
public override void OnActionExecuted(ActionExecutedContext context)
{
...
}
As a side note, you didn't check for type JsonResult. To make it work with Json() or JsonResult(), you need check the result type dynamically:
public class CancelCamelCaseResolverConfigurationAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext context)
{
base.OnActionExecuted(context);
switch(context.Result){
case JsonResult j:
var result = new ObjectResult(j.Value);
context.Result = result;
ChangeFormatting(result);
break;
case ObjectResult o:
ChangeFormatting(o);
break;
default:
return;
}
}
private void ChangeFormatting(ObjectResult result){
if (result == null){ return; }
result.Formatters.Clear();
result.Formatters.Add(new JsonOutputFormatter(
new JsonSerializerSettings()
{
Formatting = Formatting.None,
ContractResolver = new DefaultContractResolver()
}, ArrayPool<char>.Shared)
);
}
}
I am using ASP.NET web api. To provide support for camel case for the properties that an end point returns, I have added this code:
//Support camel casing
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().FirstOrDefault();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
This is working fine but I want to add an exception for one of the endpoints. Which will ensure when the data is returned from that end point, properties are not camel cased. How can I add this exception or a single endpoint?
It's not possible to make control if you are applying a global camel case configuration
AFAK the only way to achieve this is by using ActionFilterAttribute
something like the following
public class CamelCasingFilterAttribute:ActionFilterAttribute
{
private JsonMediaTypeFormatter _camelCasingFormatter = new JsonMediaTypeFormatter();
public CamelCasingFilterAttribute()
{
_camelCasingFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
ObjectContent content = actionExecutedContext.Response.Content as ObjectContent;
if (content != null)
{
if (content.Formatter is JsonMediaTypeFormatter)
{
actionExecutedContext.Response.Content = new ObjectContent(content.ObjectType, content.Value, _camelCasingFormatter);
}
}
}
}
public class ValuesController : ApiController
{
// GET api/values
[CamelCasingFilter]
public IEnumerable<Test> Get()
{
return new Test[] {new Test() {Prop1 = "123", Prop2 = "3ERr"}, new Test() {Prop1 = "123", Prop2 = "3ERr"}};
}
// GET api/values/5
public Test Get(int id)
{
return new Test() {Prop1 = "123", Prop2 = "3ERr"};
}
}
public class Test
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
if you try to call the first action the answer will like the follwing
[{"prop1":"123","prop2":"3ERr"},{"prop1":"123","prop2":"3ERr"}]
and for the second action given that there is no filter you will get something like this
{
"prop1": "123",
"prop2": "3ERr"
}
Note if you want to make it easy controlling camelCase over an entire controller, try to put your action that you want it to send back the answer in a not CamelCase in a controller apart but, for the rest apply this Filter on a controller level if you want.
More you should delete the GlobalConfiguration to get this
I am attempting to write a filter that wraps data to follow the JSON API spec and so far I've got it working on all cases where I directly return an ActionResult, such as ComplexTypeJSON. I am trying to get it to work in situations like ComplexType where I do not have to run the Json function constantly.
[JSONAPIFilter]
public IEnumerable<string> ComplexType()
{
return new List<string>() { "hello", "world" };
}
[JSONAPIFilter]
public JsonResult ComplexTypeJSON()
{
return Json(new List<string>() { "hello", "world" });
}
However, by the time public override void OnActionExecuted(ActionExecutedContext filterContext) runs when I navigate to ComplexType, the filterContext.Result is a Content Result, that is just a string where filterContext.Result.Content is simply:
"System.Collections.Generic.List`1[System.String]"
Is there a way I can set something up to make ComplexType become JsonResult rather than ContentResult?
For context, here are the exact files:
TestController.cs
namespace MyProject.Controllers
{
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using MyProject.Filters;
public class TestController : Controller
{
[JSONAPIFilter]
public IEnumerable<string> ComplexType()
{
return new List<string>() { "hello", "world" };
}
[JSONAPIFilter]
public JsonResult ComplexTypeJSON()
{
return Json(new List<string>() { "hello", "world" });
}
// GET: Test
[JSONAPIFilter]
public ActionResult Index()
{
return Json(new { foo = "bar", bizz = "buzz" });
}
[JSONAPIFilter]
public string SimpleType()
{
return "foo";
}
[JSONAPIFilter]
public ActionResult Throw()
{
throw new InvalidOperationException("Some issue");
}
}
}
JSONApiFilter.cs
namespace MyProject.Filters
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MyProject.Exceptions;
using MyProject.Models.JSONAPI;
public class JSONAPIFilterAttribute : ActionFilterAttribute, IExceptionFilter
{
private static readonly ISet<Type> IgnoredTypes = new HashSet<Type>()
{
typeof(FileResult),
typeof(JavaScriptResult),
typeof(HttpStatusCodeResult),
typeof(EmptyResult),
typeof(RedirectResult),
typeof(ViewResultBase),
typeof(RedirectToRouteResult)
};
private static readonly Type JsonErrorType = typeof(ErrorModel);
private static readonly Type JsonModelType = typeof(ResultModel);
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (IgnoredTypes.Any(x => x.IsInstanceOfType(filterContext.Result)))
{
base.OnActionExecuted(filterContext);
return;
}
var resultModel = ComposeResultModel(filterContext.Result);
var newJsonResult = new JsonResult()
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = resultModel
};
filterContext.Result = newJsonResult;
base.OnActionExecuted(filterContext);
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var modelState = filterContext.Controller.ViewData.ModelState;
if (modelState == null || modelState.IsValid)
{
base.OnActionExecuting(filterContext);
}
else
{
throw new ModelStateException("Errors in ModelState");
}
}
public virtual void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.Exception == null) return;
// Todo: if modelstate error, do not provide that message
// set status code to 404
var errors = new List<string>();
if (!(filterContext.Exception is ModelStateException))
{
errors.Add(filterContext.Exception.Message);
}
var modelState = filterContext.Controller.ViewData.ModelState;
var modelStateErrors = modelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage).ToList();
if (modelStateErrors.Any()) errors.AddRange(modelStateErrors);
var errorCode = (int)System.Net.HttpStatusCode.InternalServerError;
var errorModel = new ErrorModel()
{
status = errorCode.ToString(),
detail = filterContext.Exception.StackTrace,
errors = errors,
id = Guid.NewGuid(),
title = filterContext.Exception.GetType().ToString()
};
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
filterContext.HttpContext.Response.StatusCode = errorCode;
var newResult = new JsonResult() { Data = errorModel, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
filterContext.Result = newResult;
}
private ResultModel ComposeResultModel(ActionResult actionResult)
{
var newModelData = new ResultModel() { };
var asContentResult = actionResult as ContentResult;
if (asContentResult != null)
{
newModelData.data = asContentResult.Content;
return newModelData;
}
var asJsonResult = actionResult as JsonResult;
if (asJsonResult == null) return newModelData;
var dataType = asJsonResult.Data.GetType();
if (dataType != JsonModelType)
{
newModelData.data = asJsonResult.Data;
}
else
{
newModelData = asJsonResult.Data as ResultModel;
}
return newModelData;
}
}
}
There are two options:
1.use ApiController instead of Controller
The apicontroller will return json result,and the default serializer is Newtonsoft.json(here),so you can use like this below:
//the response type
public class SimpleRes
{
[JsonProperty(PropertyName = "result")]
public string Result;
}
//the controller
public class TestController : ApiController
{
[HttpGet]
[HttpPost]
[JSONAPIFilter]
public SimpleRes TestAction()
{
return new SimpleRes(){Result = "hello world!"};
}
}
2.wrap your response with your own ActionResult if you insist using Controller:
//json container
public class AjaxMessageContainer<T>
{
[JsonProperty(PropertyName = "result")]
public T Result { set; get; }
}
//your own actionresult
public class AjaxResult<T> : ActionResult
{
private readonly T _result;
public AjaxResult(T result)
{
_result = result;
}
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Clear();
context.HttpContext.Response.ContentType = "application/json";
var result = JsonConvert.SerializeObject(new AjaxMessageContainer<T>
{
Result = _result,
});
var bytes =
new UTF8Encoding().GetBytes(result);
context.HttpContext.Response.OutputStream.Write(bytes, 0, bytes.Length);
}
}
//your controller
[JSONAPIFilter]
public AjaxResult<List<String>> TestSimple()
{
return AjaxResult<List<String>>(new List<string>() { "hello", "world" });
}
and if you wanna get response string from filter for log or something:
var result = filterContext.Response.Content.ReadAsStringAsync();
I think this is what you are looking for :
public class JSONAPIFilterAttribute : ActionFilterAttribute, IActionFilter
{
void IActionFilter.OnActionExecuted(ActionExecutedContext context)
{
context.Result = new JsonResult
{
Data = ((ViewResult)context.Result).ViewData.Model
};
}
}
From #roosteronacid : return jsonresult in actionfilter
I have just encountered the same issue and found a slightly different approach.
The basic idea was from NOtherDev.
I would introduce an IActionInvoker.
public class ControllerActionInvokerWithDefaultJsonResult : ControllerActionInvoker
{
public const string JsonContentType = "application/json";
protected override ActionResult CreateActionResult(ControllerContext controllerContext, ActionDescriptor actionDescriptor, object actionReturnValue)
{
if (controllerContext.HttpContext.Request.Path.StartsWith("/api/"))
{
return (actionReturnValue as ActionResult)
?? new JsonResult
{
Data = actionReturnValue,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
return base.CreateActionResult(controllerContext, actionDescriptor, actionReturnValue);
}
}
In this case every request starting with "/api/" will have transformed result to json, however only when the actionReturnValue is not a type inherited from ActionResult already.
IActionInvoker is resolved by DependencyResolver, so you need to define the registration in your favorite ioc container which you have set as DependencyResolver.
myFavoriteContainer.Register<IActionInvoker, ControllerActionInvokerWithDefaultJsonResult>(Lifestyle.Transient);
For JsonResult you could use the built-in or this.
In a case you are using async action methods you should inherit from AsyncControllerActionInvoker instead of ControllerActionInvoker and I assume you will need to add an another registration for IAsyncActionInvoker as well. I am not sure about changes in the async part of the invoker itself.
I have a web api, where the global configuration is configured to use:
XmlMediaTypeFormatter
My problem is I wont to extend this web api with a new controller, that uses the JsonMediaTypeFormatter instead.
Is it possible to change the MediaTypeFormatter to JSON for only one API Controller class?
My problem is not returning JSON, I have accumplished this with returning HttpResponseMessage:
return new HttpResponseMessage
{
Content = new ObjectContent<string>("Hello world", new JsonMediaTypeFormatter()),
StatusCode = HttpStatusCode.OK
};
It's on the request I get the problem. If I have an object with two properties:
public class VMRegistrant
{
public int MerchantId { get; set; }
public string Email { get; set; }
}
And my controller action takes the VMRegistrant as argument:
public HttpResponseMessage CreateRegistrant(VMRegistrant registrant)
{
// Save registrant in db...
}
But the problem is when I call the action with JSON it fails.
You can have your controller return an IHttpActionResult and use the extension method HttpRequestMessageExtensions.CreateResponse<T> and specify the formatter you want to use:
public IHttpActionResult Foo()
{
var bar = new Bar { Message = "Hello" };
return Request.CreateResponse(HttpStatusCode.OK, bar, new MediaTypeHeaderValue("application/json"));
}
Another possibility is to use the ApiController.Content method:
public IHttpActionResult Foo()
{
var bar = new Bar { Message = "Hello" };
return Content(HttpStatusCode.OK, bar, new JsonMediaTypeFormatter(), new MediaTypeHeaderValue("application/json"));
}
Edit:
One possibility is to read and deserialize the content yourself from the Request object via reading from the stream and using a JSON parser such as Json.NET to create the object from JSON:
public async Task<IHttpActionResult> FooAsync()
{
var json = await Request.Content.ReadAsStringAsync();
var content = JsonConvert.DeserializeObject<VMRegistrant>(json);
}
Yes, it's possible to change the MediaTypeFormatters for only one class/controller. If you want to save and restore the default formatters you can follow these steps:
In the beginning of the request save old formatters
Clear the formatters collection
Add the desired formatter
At the end of the request copy back the old formatters
I think this is easily done by an ActionFilterAttribute:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class ChangeFormatterAttribute : ActionFilterAttribute
{
private IEnumerable<MediaTypeFormatter> oldFormatters;
private MediaTypeFormatter desiredFormatter;
public ChangeFormatterAttribute(Type formatterType)
{
this.desiredFormatter = Activator.CreateInstance(formatterType) as MediaTypeFormatter;
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
var formatters = actionContext.ControllerContext.Configuration.Formatters;
oldFormatters = formatters.ToList();
formatters.Clear();
formatters.Add(desiredFormatter);
base.OnActionExecuting(actionContext);
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var formatters = actionExecutedContext.ActionContext.ControllerContext.Configuration.Formatters;
formatters.Clear();
formatters.AddRange(oldFormatters);
base.OnActionExecuted(actionExecutedContext);
}
}
And the usage:
[ChangeFormatterAttribute(typeof(JsonMediaTypeFormatter))]
public class HomeController : ApiController
{
public string Get()
{
return "ok";
}
}
// ...
[ChangeFormatterAttribute(typeof(XmlMediaTypeFormatter))]
public class ValuesController : ApiController
{
public string Get()
{
return "ok";
}
}
Maybe you could have your media type formatter only accept the type that is handled by your controller:
public class Dog
{
public string Name { get; set; }
}
public class DogMediaTypeFormatter : JsonMediaTypeFormatter
{
public override bool CanReadType(Type type)
{
return type == typeof (Dog);
}
}
Probably not the best solution though :I