Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Web API Custom Bad Request Response

Posted on 2013-02-07
2
Medium Priority
?
629 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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…
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 …
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

876 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