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

  • SEARCH
  • Home
  • 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 8181947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:41:40+00:00 2026-06-07T00:41:40+00:00

sorry for the dummy question but I cannot find a simple and clean way

  • 0

sorry for the dummy question but I cannot find a simple and clean way for doing something as simple as that. I have an MVC controller which should return a JSON object to be consumed by some JavaScript; if I set its return type to JsonResult and return Json(objecttoserialize) I can see through Firebug that the JSON code is returned and interpreted correctly. Anyway, I have to use a manually encoded JSON string because:

  • the component which serializes the object I want to return is hosted
    in an external library and I should not touch it.
  • this component serializes on its own because it has a Dictionary
    member representing the property NAME and VALUE of the corresponding JS object.

For instance, an entry in the dictionary like “width” for the key and “20” for the value must be serialized as { width: “20” }, i.e. as if the .NET object had a property Width with value 20, whereas it simply has a dictionary with a variable number of such property/value pairs to be represented by object properties in JS objects. This is why the component has its own JSON serialization method. Thus, I should just return the JSON generated by it.

As the Json method serializes a .NET input object, I googled around I find I could rather use a ContentResult. Thus I tried by returning a ContentResult with Content=the serialized string and ContentType = “application/json”; anyway the JS client seems not able to understand this is a JSON object and fails. If instead I return a JsonResult it works as expected, but of course the properties represented by its Dictionary member are lost. I was expecting JsonResult to be equivalent to the ContentResult above, but this does not seem the case. The JS code is like:

request: function (nodeId, level, onComplete) {
$.ajax({
    url: "/Node/Get", type: "POST", dataType: "json",
    data: { id: nodeId, level: level, depth: 3 },
    success: function (data) {
        var ans = data;
        onComplete.onComplete(nodeId, ans);
    }
});

If I place a breakpoint in the script in Firebug, When I return JsonResult the success function is hit; when I return ContentResult, it is never hit and the page remains stuck loading the requested object. (This JS refers to the SpaceTree of http://www.thejit.org). Could anyone give a hint?

  • 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-07T00:41:43+00:00Added an answer on June 7, 2026 at 12:41 am

    I managed to get it working with a sort of trick, but I’d like to know if there is a better solution for this, and at any rate if it is true that I really need to use JsonResult (or a derived class, like in this trick) to get JS work properly. I derived a class from JsonResult and changed the ExecuteResult method so that it just passes through the received JSON string:

    public sealed class PassthroughJsonResult : JsonResult
    {
      public string Json { get; set; }
    
      public override void ExecuteResult(ControllerContext context)
      {
        if (context == null)
          throw new ArgumentNullException("context");
    
        HttpResponseBase response = context.HttpContext.Response;
    
        if (!String.IsNullOrEmpty(ContentType))
          response.ContentType = ContentType;
        else
          response.ContentType = "application/json";
    
        if (ContentEncoding != null)
          response.ContentEncoding = ContentEncoding;
    
        if (Json != null) response.Write(Json);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the simple question, but I'm trying to find an elegant way to
Sorry to ask something that others have already asked. But when I use the
Sorry for posting something that's probably obvious, but I don't have much database experience.
Sorry for question but I can't find answer anywhere on internet. I couldn't find
Sorry for this simple question, but I can't solve it... There is an example:
Sorry if this is a stupid question, but is there an easy way to
Sorry for the probably dummy question but I'm lost. can Wi-fi network strength (dBm)
Sorry for the Dummy Question , i know :( ,, but it's only the
Sorry if is a dummy question but I'm not really clear with it. Is
sorry for lamer question, but I really could not found subject. I have a

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.