Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Missing Ok() in Controller #2825

Closed
Bartmax opened this issue Jul 17, 2015 · 5 comments
Closed

Missing Ok() in Controller #2825

Bartmax opened this issue Jul 17, 2015 · 5 comments

Comments

@Bartmax
Copy link

Bartmax commented Jul 17, 2015

I think there's a missing helper in controller to return an object. or I can't find it :)

public virtual ObjectResult Ok(object value);

so

[HttpGet("{id}")]
public IActionResult Detail(int id)
{
    var ret = xxx.GetById(id);
    if (ret == null) return HttpNotFound($"id {id} does not exist");
    return Ok(ret);
}

instead of:

return new ObjectResult(ret);
@dougbu
Copy link
Member

dougbu commented Jul 18, 2015

@Bartmax this feature is available in the Microsoft.AspNet.Mvc.WebApiCompatShim package. Have a look at the ApiController class.

One alternative is the HttpStatusCodeResult class, which is similar to Web API's OkResult and available in both MVC 5 and 6.

@Bartmax
Copy link
Author

Bartmax commented Jul 20, 2015

I'm not doing any compatibility stuff here, while Ok is there, I'm not doing any webapi2, so I don't think importing that would actually fit.

I found that ObjectResult (instead of HttpStatusCodeResult) being more accurate, also there's no method on base controller that returns either ObjectResult or HttpStatusCodeResult.

I think there should be a public virtual ObjectResult Ok(object value); in the Controller class on the framework or HttpOk like the other methods for the common scenarios.

It feels weird to have HttpBadRequest, HttpNotFound, HttpUnauthorized, Created, but not an HttpOk on base abstract class controller.

This was kind of a question/suggestion.

@danroth27 danroth27 added this to the 6.0.0-beta7 milestone Jul 21, 2015
dougbu added a commit that referenced this issue Jul 22, 2015
…roller`

- #2825
- new class names align with existing types such as `HttpNotFoundResult` and `HttpNotFoundObjectResult`
- remove similar types from WebApiCompatShim and use replacements in `ApiController`
 - `NegotiatedContentResult<T>` remains because Core doesn't have an exact replacement

nits:
- add missing periods in some `Controller` doc comments
@dougbu dougbu changed the title Missing Ok in controller ? Missing Ok() in Controller Jul 22, 2015
@dougbu
Copy link
Member

dougbu commented Jul 22, 2015

Updated subject because this is no longer a question.

@Bartmax
Copy link
Author

Bartmax commented Jul 22, 2015

:shipit: hehe nice work @dougbu

dougbu added a commit that referenced this issue Jul 22, 2015
…roller`

- #2825
- new class names align with existing types such as `HttpNotFoundResult` and `HttpNotFoundObjectResult`
- remove similar types from WebApiCompatShim and use replacements in `ApiController`
 - `NegotiatedContentResult<T>` remains because Core doesn't have an exact replacement

nits:
- add missing periods in some `Controller` doc comments
dougbu added a commit that referenced this issue Jul 24, 2015
…roller`

- #2825
- new class names align with existing types such as `HttpNotFoundResult` and `HttpNotFoundObjectResult`
- remove similar types from WebApiCompatShim and use replacements in `ApiController`
 - `NegotiatedContentResult<T>` remains because Core doesn't have an exact replacement

nits:
- add missing periods in some `Controller` doc comments
@dougbu
Copy link
Member

dougbu commented Jul 24, 2015

6106375

@dougbu dougbu closed this as completed Jul 24, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants