Solved

Custom response from Web API

Posted on 2013-02-06
3
480 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
ID: 38861910
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
ID: 38863233
0
 

Author Closing Comment

by:gswitz
ID: 38863606
Perfect! Just what I needed!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

773 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