pmhsfelix / drum Goto Github PK
View Code? Open in Web Editor NEWDirect Route URI Maker for ASP.NET Web API
License: Apache License 2.0
Direct Route URI Maker for ASP.NET Web API
License: Apache License 2.0
Currently for a configuration like this:
[Route("api/product/{searchCriteria}]
[Route("api/product/type/id/{guid}"]
public async Task<IHttpActionResult> GetProduct()
{
}
you will receive the exception:
Multiple routes for the same action. Action: <action>, previous route <route1>, new route: <route2>
Is it possible to actually support multiple routes?
In case of more than 1 route is found use say DefaultAmbiguousRouteResolver
which returns routes.First()
, or an implementation of IAmbiguousRouteResolver
passed to MapHttpAttributeRoutesAndUseUriMaker()
if any.
Other way to support such scenario is to respect the RouteAttribute.Name property, they should be different when applied to the same action.
Please add to the documentation a sample how to register using Unity.
I'm sorry my English is bad.
First of all, please let me to thank you for your work.
I use UrlMaker conveniently.
Let’s get down to business.
How about adding "HttpConfigration.MapHttpAttributeRoutesAndUseUriMaker" overload method with "IInlineConstraintResolver"?
I want to use both UrlMaker and custom IRouteConstraint.
Kind Regards.
Attach context to config, in order to avoid having a message handler to propagate the context on the request (as suggested by https://github.com/filipw)
When an action has an IEnumerable parameter the code to handle complex objects is iterating over the properties instead of the items within the array.
Simple test:
[Route("")]
public void GetByIds(int[] id) { }
[Fact]
public void Can_make_uri_for_action_with_array()
{
var uri = _uriMaker.UriFor(c => c.GetByIds(new []{ 1, 2, 3 }));
Assert.Equal("http://example.org/api/UriMakerTests/resources?id=1&id=2&id=3",uri.ToString());
}
Also would be helpful if it worked for an object parameter that has list properties like:
public class PageInfo
{
public int page { get; set; }
public int count { get; set; }
public List<int> someList { get; set; }
}
I took a crack at a fix but hit a wall building the RouteValueHandler. Thanks for the work on this, link generation in web api is really weak without drum.
I am using a custom DirectRouteProvider that overrides the GetActionRouteFactories method of the DefaultDirectRouteProvider in order to allow inheritance in attribute routing.
This allows me to use attribute routing in a BaseController to define route for every action that should be available to all the inheriting controllers (TestController : Basecontroller).
This works fine until I try to generate links to generic routes.
I can't name the routes dynamically based on the generic type as this is not allowed in attributes in C#.
I can't define a name for these routes as there would be many routes with the same name (for all inheriting controllers).
I thought I could use your package as Drum works well with unnamed routes, but I can't seem to find a way to use the uriMaker from inside the BaseController to reference the route from the inheriting controller in a generic way.
Is there a way out?
Currently it has the following signature:
public Uri UriFor(Expression<Action<TController>> action);
so if action is async such as
public async Task<IHttpActionResult> Get();
then a compiler warning appears:
Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
So maybe Expression<Task<Action<TController>>
could be added?
Say you have a controller with two actions:
[Route("api/user/{searchCriteria}")]
public async Task<IHttpActionResult> GetUser([FromUri]UserSearchCriteria searchCriteria)
and
[Route("api/user")]
public async Task<IHttpActionResult> PutUser(UserModel userModel)
In second you want to return a uri to first:
var uriMaker = Request.TryGetUriMakerFor<UserController>();
var uri = uriMaker.UriFor(c => c.GetUser(new UserSearchCriteria { UserId = user.UserId }));
return Created(uri, user);
Actual result:
http://localhost:8086/api/user/Models.UserSearchCriteria
Expected result:
http://localhost:8086/api/user/???
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.