Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Custom response from Web API

Posted on 2013-02-06
3
482 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

861 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