Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8309669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:11:37+00:00 2026-06-08T19:11:37+00:00

When returning Json from a controller (MVC 4 RC) I would like to modify

  • 0

When returning Json from a controller (MVC 4 RC) I would like to modify the Json to use camel-casing for the properties and to do this I tried setting the GlobalConfiguration.Formatters.JsonFormatter (not sure if this is correct…don’t have the code in front of me), but this does not appear to affect the Json outputted by the Controller.Json method.

After looking around it appears that this approach would only affect Web API controllers, etc. Is this true? Also, is it possible to alter the Controller.Json() method to acheive this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:11:38+00:00Added an answer on June 8, 2026 at 7:11 pm

    Like @rouen suggests, created your own JsonDotNetResult.

    This is the one I have in my project:

    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();
        }
    }
    

    I then have my own baseController that I inherit from to give me JsonDotNet(object viewModel) type methods.

    E.g.

    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 });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am returning this from a json result from a controller, var genericResult =
I'm returning JSON from my controller to my view in order to populate a
I'm returning some Json via a C# MVC Controller. Other browsers work fine, but
I have a controller returning a json structure like so: def show # .......
I am currently returning JSON data like this: return new JsonResult { Data =
I'm returning a JsonResult from an MVC controller action , and have been trying
I am returning Json data from a controller's action. On the client, I am
Android programming. I have a json returning the date from a database as a
It seems that when returning an object containing a type attribute as JSON from
I'm using Asp.Net-Mvc, I have this method in my controller: [AcceptVerbs(HttpVerbs.Post)] public ActionResult LinkAccount(string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.