Error when using RouteAttribute and HttpPost together on same action #1194
Comments
We should definitely improve the error message and also see if we can come up with a behavior that is more compatible with the MVC 5 behavior. |
The current behavior is by design, [Route] defines an attribute routed action without a constraint. [HttpGet] without a valid template defines an IHttpConstraint. In the new behavior a method can define attributed actions or non attributed actions, but not both at the same time. The same behavior can be achieved by just doing [HttpPost("foo")] We can discuss changing this when @rynowak is around. |
@javiercn was it really intended that |
@DamianEdwards Sorry, I didn't explain myself properly. Let me try to reword it in clearer terms. An action like Foo can define one or more actions that can either be conventionally routed or attribute routed, but not both at the same time. If a method defines a valid attribute routed action (like the one produced by route) we consider that all the actions defined by that method have to be attribute routed (we don't allow mixing conventionally routed and attribute routed actions on the same method, that's the error message). That said, If during action discovery we see that you don't define a valid route template (by providing a template using [Route] on the controller, or [Http_] or [Route] on the action) we just treat [Http_] as plain IHttpConstraints so that you can still have conventionally routed actions that use [HttpGet], [AcceptVerbs], etc. The recomendation is not to do [Route("template")]
[Http*]
public void MyAction(){} but to just do [Http*("template")]
public void MyAction(){} or [AcceptVerbs("POST, GET", Route = "template")]
public void MyAction(){} if you wan to support multiple HTTP methods. If you want to enable this scenario [Route("template")]
[HttpPost]
public void MyAction(){} you can easily do so by writing your own IActionDiscoveryConvention or by writing an IReflectedApplicationModelConvention that tweaks the orginal model we produce. The reason why we chose this approach is that you can achieve the scenario above more succinctly by just using [Http*("template")] and with the other option, scenarios like the following [HttpPut("v1/template/{id}")]
[HttpPatch("v2/template/{id}")]
public void MyAction(int id, ...){} won't work as expected out of the box. I hope this makes things clearer, but I completely agree with you that the error message could be improved. |
Suggested change here is to change the way grouping of attributes is done for routing Rules: Examples: - Note that scenario 1 below is the only change from current code 1). Back compat
Route - Verb Rule 1 says that 2). More routes/verbs
Route - Verb Rule 1 says that 3). Separate urls/verbs
Route - Verb Rule 1 says that 4). Something that's still broken
Route - Verb Rule 1 says that |
Thanks for addressing the back compat scenario. To make sure I understand, will combining separate methods on the same route continue working too? Rough example:
|
This change enables some compatibility scenarios with MVC 5 by expanding the set of legal ways to configure attribute routing. Most promiently, the following example is now legal: [HttpPost] [Route("Products")] public void MyAction() { } This will define a single action that accepts POST on route "Products". See the comments in #1194 for a more detailed description of what changed with more examples.
@Encosia glad we could help! Yes, your example is valid. You'll end up with two actions
Action selection will disambiguate based on the HTTP verb of the request. The following are also valid if you want multiple verbs in the same action.
Does that answer your question? |
Yes. That's perfect. Just wanted to be sure I understood. |
This change enables some compatibility scenarios with MVC 5 by expanding the set of legal ways to configure attribute routing. Most promiently, the following example is now legal: [HttpPost] [Route("Products")] public void MyAction() { } This will define a single action that accepts POST on route "Products". See the comments in #1194 for a more detailed description of what changed with more examples.
Microsoft.AspNet.Mvc.Routing (1.0.0-beta1-10401)
Repro Steps
Expected Results
The site runs
Actual Results
A Helios error is displayed with the following details:
The text was updated successfully, but these errors were encountered: