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 9100507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:53:03+00:00 2026-06-17T00:53:03+00:00

I used this below code on my asp.net controller to return Json object on

  • 0

I used this below code on my asp.net controller to return Json object on my Ajax on javascript

public JsonResult myMethod()
{
    // return a Json Object, you could define a new class
    return Json(new
    {
        Success = true, //error
        Message = "Success" //return exception
    });
}

Jquery-Ajax:

$.ajax({
    type: "POST",
    url: url_ ,
    data: search,
    success: function(data) {   
        //Show Json Properties from Controller ( If Success == false show exception Message from controller )
        if (data.Success)  
        {
            alert(data.Message); //display success 
        }
        else
        {
            alert(data.Message) //display exception
        }
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert("error: " + XMLHttpRequest.responseText);
    },
    dataType: 'json'
});

How can this be done on Web Api Controller?

Can you give me some examples or url as reference.

Thanks and Regards

  • 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-17T00:53:04+00:00Added an answer on June 17, 2026 at 12:53 am

    If you create yourself a new HttpContent class for delivering JSON, like…

     public class JsonContent : HttpContent {
    
        private readonly MemoryStream _Stream = new MemoryStream();
        public JsonContent(object value) {
    
            Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var jw = new JsonTextWriter( new StreamWriter(_Stream));
            jw.Formatting = Formatting.Indented;
            var serializer = new JsonSerializer();
            serializer.Serialize(jw, value);
            jw.Flush();
            _Stream.Position = 0;
    
        }
        protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) {
            return _Stream.CopyToAsync(stream);
        }
    
        protected override bool TryComputeLength(out long length) {
            length = _Stream.Length;
            return true;
        }
    }
    

    Then you can do,

          public HttpResponseMessage Get() {
                return new HttpResponseMessage() {
                    Content = new JsonContent(new
                    {
                        Success = true, //error
                        Message = "Success" //return exception
                    })
                };
            }
    

    just like you do with JsonResult.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used below code in ASp.NET web forms code behined. Its passing parameter in
below is the code i'm using and i've used this 100x's before. And now
I get this error when I run the code below. I have normally used
I have a controller in C# using the ASP.Net MVC framework public class HomeController:Controller{
I am using AJAX.NET's update panels in the below manner. My code works perfect
i used this code: List<string> lists=new List<string>(apple,orange,banana,apple,mang0,orange); string names; names=lists.Distinct() is that correct?
I used this tutorial to create a basic Javascript function for implementing change during
I used this tutorial to rename a view controller http://iosdeveloperzone.com/2011/05/14/how-to-rename-a-class-using-refactoring-in-xcode-4/ Basically just switch to
strong text Thanks to Tim who provided me the code below in this other
I've an annoying ASP.NET problem: I have a Perl script (see below), which gets

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.