Solved

Custom response from Web API

Posted on 2013-02-06
3
486 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
[X]
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
  • 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

630 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