Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Closed Issue: Web API attribute routing invocation and effect on other runtime features [1165]

$
0
0
This is in regards to user experience when using Web API’s attribute routing.

In the following scenario, the PrincipalParameterBinding is not invoked and the GetData action would see ‘principal’ as null. However if we switch the order of statements below to have parameter binding rules registered first and then call ‘MapHttpAttributeRoutes’, things work as expected. This behavior is because when attribute routing is generating routes, it tries to find out all action descriptors on a controller which triggers a path where the parameter binding rules of an action are tried to be retrieved. Since here the rules are registered after the call to “MapHttpAttributeRoutes”, these rules are not considered/added to the action binding which would be cached for future purposes.

The concern here is that the end user has to be aware of these intricacies and make sure to call “MapHttpAttributeRoutes()” as the last step to make things work as expected. This kind of behavior also applies for users replacing IHttpControllerSelector service, IActionValueBinder etc., which attribute routing uses internally.

__Question__: Can we improve the user experience here so that a user doesn’t have to worry as to where he is making the call to “MapHttpAttributeRoutes()”?

```
public void Configuration(IAppBuilder appBuilder)
{
var config = new HttpConfiguration();

config.MapHttpAttributeRoutes();

ParameterBindingRulesCollection pb = config.ParameterBindingRules;
pb.Insert(0, typeof(IPrincipal), param => new PrincipalParameterBinding(param));

appBuilder.UseWebApi(config);
}
}

[RoutePrefix("api/greetings")]
public class TestController : ApiController
{
[HttpGet("")]
public string GetData(IPrincipal principal)
{
string result = "Hello!";

if(principal != null && principal.Identity != null)
{
result += " from " + principal.Identity.Name;
}

return result;
}
}
```

__Attached__ a katana selfhost repro.
Comments: Verified.

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>