asp.net mvc 4 - Return custom error objects in Web API -


I have a web API that I am working on using the MVC 4 web API framework. If there is an exception, then I am currently throwing a new HTTPPRESSpassion exception. I.e.:

  (! Int32.TryParse (ID, Out UserID)) New HTTPPRESS Pax Expression (requested. CRETERRSpons (HTTPITS code.badreestest, "invalid ID"));   

This gives the customer an object that is simply {"message": "invalid id"}

I have the advantage to get more Further control over this response of exceptions by returning the detailed object.

  {"status": - 1, "substatus": 3, "message": "user could not be found"}   

How would I do this Am going to do? What is the best way to sort my error object and set it in a response message?

I've also seen a bit more in ModelStateDictionary and it came with a "hack", but it's still not a clean output: var msd = new ModelStateDictionary (); Msd.AddModelError ("Status", "-1"); Msd.AddModelError ("substatus", "3"); Msd.AddModelError ("Message", "Invalid Content"); New HttpResponseException Throw (Request.CreateErrorResponse (HttpStatusCode.BadRequest, MSD));

edit
looks like a custom html , what I want it to do is move, now To extend it from your business layer ...

  var error = new HTTPER ("illegal content") {{"{status}", {"status", -1} {"persatas", 3}} ; Throw new HTTPPranspace exemption (request. CreteRespons (HTTTcc.com.badreview, error));    

More complicated than these answers public static class WebApiConfig {register public static void (HTTP configuration configuration) {config.Filters.Add (new hand APIEpactionAttivity ()); // ...}} public class handle apia exception properties: exception filter attribute {public override zero on expansion (hppxextended contact reference) {var request = context.ActionContext.Request; Var response = new {// property go here ...}; Reference. Response = Request Create Response (HTTP Status Code. Bad Requests, Response); }}

This is also good and it is easy to test the unit:

  [test] public async zero on expansion_shouldedbibleprafeirrrowspace () {var Expected = New {// Properties Go Here ...}; // Setup var target = new handle API expansection () var contextMock = BuildContextMock (); // Act Target. Hypothesis (reference mark); Dynamic genuine = wait for references Mock. response. Content Readsink & lt; ExpandoObject & gt; (); Decide. First (expected presentation, actual .performance); } Private HttpActionExecutedContext BuildContextMock () {var requestMock = new HttpRequestMessage (); RequestMock.Properties.Add (HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration ()); New HTTP Acquired Return () {ActionContext = new HttpActionContext {Controller Contact = New HTTP Controller Contact {Request = Request Mock}}, Exception = New Exception ()}; }    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -