2

I have a RESTful service in .NET and i would like all actions return an object of type JsonResult, JsonResult is an object defined by me like this:

public class JsonResult<T>
{
    public Notify Notify {get; set;}
    public T Data {get; set;}

    public static CreateResponse(T Data, Notify Notify = null, HttpStatusCode Code = HttpStatusCode.OK)
    {
        //Code param not manage, at the moment

        return new JsonResult<T>
        {
            Data = Data,
            Notify = Notify
        };

    }
}

public class Notify
{
    public string Message {get; set;}
    public Severity Severity {get; set;}
}

public enum Severity
{
    Error,
    Info,
    Warning,
    Fatal,
}

so at the moment i have actions look like that:

public JsonResult<string> Get()
{

    return JsonResult<string>.CreateResponse("Ciao Mondo!");
}

it works and i like this form 'cause when i read the firm i already know what i want to return to the client (JsonResult, T is the type of my data)... but the problem is when i want to manage the status code.

Before to create this new project, to manage the status code i used the HttpResponseMessage and the method Request.CreateResponse to return some data to the client:

public HttpResponseMessage Get()
{

    return Request.CreateResponse(HttpStatusCode.BadRequest, "Ciao Mondo!");
}

but i don't like that 'cause it is not immediate to know the returned type.

so... my question is... how can i manage the status code into my JsonResult.CreateResponse(T, Notify[, HttpStatusCode]) method?

this method is moved into an CustomApiController (inherits from ApiController), so i have HttpContext and Request available.

Thanks so much to all

2
  • 1
    If you're returning a response, the implication is that the request found the entity that was requested, so the only reasonable status code is 200. Which status code other than 200 OK would you want to return and why? Commented May 15, 2018 at 11:13
  • it isn't always true... if i cannot (for thousand reasons) execute a part of my code i would like to manage the error and not throw it to the client. for example: i have caught a timeout and i want generate a notify that explain it, this wouldn't say that i have a good response and my request is satisfied. Commented May 15, 2018 at 13:43

3 Answers 3

2

You could call Content which accepts a HttpStatusCode as well as a generic object. It does require you to change your method's return type to IHttpActionResult which is generally preferred.

public IHttpActionResult Get()
{
  if(someErrorCondition)
     return Content(HttpStatusCode.BadRequest, JsonResult<string>.CreateResponse("Ciao Mondo!"));
  return Ok(JsonResult<string>.CreateResponse("Ciao Mondo!"));
}
Sign up to request clarification or add additional context in comments.

Comments

0

Its preferred to use IHttpActionResult, don't forcefully define it to JsonResult. This should be configurable through content-negotiation.

You can try as below:

     public IHttpActionResult Get()
     {
      if(error)
          BadRequest("Bad Request !!");
       return Ok("Ciao Mondo!");
     }

Comments

0

Your answer were be very helpful. I was looking for another solution, but you gave me a good idea and i have found this solution:

public class CustomApiController : ApiController
{

    public class JsonResult<Target> : NegotiatedContentResult<Target>
    {
        public JsonResult(HttpStatusCode statusCode, Json<Target> content, ApiController controller) : base(statusCode, content.Data, controller)
        {

            this.Content = content;
        }

        public JsonResult(HttpStatusCode statusCode, Target content, ApiController controller) : base(statusCode, content, controller)
        {

        }

        public JsonResult(HttpStatusCode statusCode, Target content, IContentNegotiator contentNegotiator, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
            : base(statusCode, content, contentNegotiator, request, formatters)
        {

        }

        public new Json<Target> Content { get; private set; }

    }


    public JsonResult<Target> CreateResponse<Target>(Target Data, string Notify, HttpStatusCode Code = HttpStatusCode.OK)
    {
        Json<Target> json = new Json<Target>
        {
            Notify = Notify,
            Data = Data
        };

        return new JsonResult<Target>(Code, json, this);
    }

}

so i can inherit from CustomApiController and write action like that:

    public JsonResult<IEnumerable<string>> Get(bool test)
    {
        if (test)
        {
            return this.CreateResponse(new string[] { "test1", "test2", "test3" } as IEnumerable<string>, null, System.Net.HttpStatusCode.OK);
        }
        else
        {
            return this.CreateResponse(new string[] { "test1", "test2", "test3" } as IEnumerable<string>, null, System.Net.HttpStatusCode.BadRequest);
        }
    }

thanks a lot!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.