Solved

Custom response from Web API

Posted on 2013-02-06
3
477 Views
Last Modified: 2013-02-07
My goal is to decide in code when to return a status code of 400 and pass the data necessary to populate validation issues on the web client.

If I could pass this..
{"Message":"The request is invalid.","ModelState":{"comment.Author":["Author is too long! This was validated on the server."]}}

in the message response I would achieve my goal.

This is the correct string...

var response = Request.CreateResponse(HttpStatusCode.BadRequest, "{\"Message\":\"The request is invalid.\",\"ModelState\":{\"comment.Author\":[\"Author is too long! This was validated on the server.\"]}}");
throw new HttpResponseException(response);

but the payload then comes back as a string rather than a JSON object...
like this...
"{\"Message\":\"The request is invalid.\",\"ModelState\":{\"comment.Author\":[\"Author is too long! This was validated on the server.\"]}}"

So... how can I return it as a JSON object where I custom build it?
0
Comment
Question by:gswitz
  • 2
3 Comments
 

Author Comment

by:gswitz
Comment Utility
It looks like I can fake it this way, but this isn't exactly what I wanted...

    public class xx
    {
        public Dictionary<string, string> ModelState;
        public string Message { get; set; }
    }


            var xy = new xx();
            xy.Message = "hello world";
            xy.ModelState = new Dictionary<string, string>() { { "comment.Author", "Author is too Long!!" }, { "a", "b" } };
            var response = Request.CreateResponse<xx>(HttpStatusCode.BadRequest, xy );
            throw new HttpResponseException(response);
0
 
LVL 18

Accepted Solution

by:
dj_alik earned 500 total points
Comment Utility
0
 

Author Closing Comment

by:gswitz
Comment Utility
Perfect! Just what I needed!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now