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

Add some WebAPI-like action results/helpers to Controller #1378

Closed
rynowak opened this issue Oct 16, 2014 · 11 comments
Closed

Add some WebAPI-like action results/helpers to Controller #1378

rynowak opened this issue Oct 16, 2014 · 11 comments

Comments

@rynowak
Copy link
Member

rynowak commented Oct 16, 2014

We could do with a few enhancements for API scenarios using Controller. Some ideas shamelessly stolen from WebAPI.

There are more of these action result methods in ApiController but these seem like the useful ones that are gaps in the core framework.

@danroth27 danroth27 modified the milestones: 6.0.0-beta1, 6.0.0-rc1 Oct 16, 2014
@danroth27 danroth27 modified the milestones: 6.0.0-beta2, 6.0.0-rc1 Oct 30, 2014
@danroth27
Copy link
Member

Let's discuss BadRequest(ModelState) as part of #1377

@danroth27
Copy link
Member

Depends on outcome of discussion from #657.

@rynowak
Copy link
Member Author

rynowak commented Nov 21, 2014

Recommendation for the scope of this item:

Enhancements to ObjectResult

  • Add a status code property to ObjectResult
  • Add a protected virtual OnFormatting(...) method to ObjectResult that's called before writing to the body. This will be overridden by various result types.

BadRequest()

  • Move the BadRequestResult from WebAPI compat shim to Mvc.Core
  • Add an HttpBadRequest() method to Controller
  • The design/code is very consistent with HttpNotFoundResult

Created(string location, object content)

  • Move CreatedNegotiatedContentResult<T> from the WebAPI compat shim to Mvc.Core
  • It should be called CreatedResult and no longer be generic
  • It should inherit from ObjectResult directly
  • Set a status code and location header
  • Add a Created(string uri, object value) and Created(Uri uri, object value) method to Controller

CreatedAtAction(...)

  • Create a result called CreatedAtActionResult
  • It should inherit from ObjectResult directly
  • Set a status code and location header
  • Use RedirectToActionResult as inspiration
  • Add the same methods to controller as RedirectToAction(...)

CreatedAtRoute(...)

  • Move CreatedAtRouteNegotiatedContentResult<T> from the WebAPI compat shim to Mvc.Core
  • It should be called CreatedAtResult and no longer be generic
  • It should inherit from ObjectResult directly
  • Set a status code and location header
  • Use RedirectToRouteResult as inspiration
  • Add the same methods to controller as RedirectToRoute(...)

@ashmind
Copy link

ashmind commented Nov 25, 2014

Not sure if this should be separate, but please also add a helper for HttpUnprocessableEntity (422).
It is annoying to create manually as it is not in HttpStatusCode enum.

See

@yishaigalatzer yishaigalatzer modified the milestones: 6.0.0-rc1, 6.0.0-beta2 Dec 12, 2014
@yishaigalatzer
Copy link
Contributor

@DamianEdwards there are the helpers we plan to add to the controller for API scenarios (other than improving the pattern), if there is anything missing please add here.

@ashmind we are not planning to add support for (422) in a helper, and we do not own the HttpStatusCode enum. Since we are only picking the most common responses. Of course you are free to add your own controller base class with the set of helpers relevant to your app. In fact in MVC 5, you can just start from a POCO class and just include what you need.

@ashmind
Copy link

ashmind commented Dec 12, 2014

@yishaigalatzer

we do not own the HttpStatusCode enum

I realize this, that's why I ask for a helper and not for an enum entry.

we are only picking the most common responses

By this logic 422 is a more useful helper than 400. 400 is a malformed request, which should not really happen after you got inside the controller action (handled by ModelBinders/MediaFormatters etc).

422 is most of semantic validation, which is pretty common inside controller actions.

@yishaigalatzer
Copy link
Contributor

400 is what you return when you get model errors, so you are already in the controller.

Like I mentioned above the helper method is trivial to write, is purely additive and we would like to keep the controller api surface minimal, as its already pretty large.

We can take a second look, and if we change our mind we will post here.

@ashmind
Copy link

ashmind commented Dec 12, 2014

@yishaigalatzer
Copy link
Contributor

@ashmind Thanks for the samples. Reading the RFC, it is still a big ambiguous when it comes to invalid data in the query (or the url for that matter), but I do see your point.

I suggest you open a separate issue, and we can follow up on this specifically.

@ajaybhargavb
Copy link
Contributor

eb7283f

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

6 participants