Solved

Web API Custom Bad Request Response

Posted on 2013-02-07
2
582 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
  • 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

708 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

18 Experts available now in Live!

Get 1:1 Help Now