Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Web API Custom Bad Request Response

Posted on 2013-02-07
2
Medium Priority
?
615 Views
Last Modified: 2013-02-11
I started with this question...
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28022765.html

And I came close to the right answer, but not quite there...

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

  var errorCollection = new ErrorCollection();
  errorCollection.Message = "The request is invalid.";
  errorCollection.ModelState = new Dictionary<string, string>() { { "pobox.Pobox_txt", "PO Box Data should be limited to 10 characters." } };
  var response = Request.CreateResponse<object>(HttpStatusCode.BadRequest, errorCollection);
  throw new HttpResponseException(response);

The JSON returned looks like this...
{"ModelState":{"pobox.Pobox_txt":"PO Box Data should be limited to 10 characters."},"Message":"The request is invalid."}

The JSON that would do what I need regarding populating the web page with validation failure messages would look like this
{"ModelState":{"pobox.Pobox_txt":["PO Box Data should be limited to 10 characters."]},"Message":"The request is invalid."}

Note the addition of the SQUARE BRACKETS in the return.

MY QUESTION
Is there a way to better define my class to get those square brackets to show up?
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
2 Comments
 

Author Comment

by:gswitz
ID: 38865272
The reason for my problem is that when the unobtrusive validation occurs on the page, it is only writing the first letter of the string into the field to show the error to the user, so where it should say "PO Box Data should be limited to 10 characters." it says only "P".

Thanks!!
0
 

Accepted Solution

by:
gswitz earned 0 total points
ID: 38877830
This was using jquery.validate and jquery.validate.unobtrusive.

    $.validator.addMethod("failure", function () { return false; });
    $.validator.unobtrusive.adapters.addBool("failure");
    $.validator.unobtrusive.revalidate = function (form, validationResult) {
        $.removeData(form[0], 'validator');
        var serverValidationErrors = [];
        for (var property in validationResult) {
            var elementId = property.toLowerCase();
            elementId = elementId.substr(elementId.indexOf('.') + 1);
            var item = form.find('#' + elementId);
            serverValidationErrors.push(item);
            item.attr('data-val-failure', validationResult[property]);
            jQuery.validator.unobtrusive.parseElement(item[0]);
        }

I never found the solution to my problem (specifically the brakets in the JSON return). Instead, I removed the array parameter that specified [0] for the item from the JSON to be written to the exception element.

So, my exception is being written as I want, but it wasn't the solution I preferred.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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