-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attribute routing controllers/endpoints end up in Non-OData Endpoint Mappings #805
Comments
I'm not seeing where you are declaring the "odata" prefix in the OData setup. Also, are you trying to use the EDM-less flow? |
As in example it is set like this:
however setting full path with
What I'm trying to do handle a bit more complex scenario, like optionally passing some extra params and/or filters.
|
I think you misunderstood my comment. I understand you are adding the "odata" via the attribute routing in the controller, but you are not registering this prefix when registering OData in the startup at all. If you don't pass anything to OData, it will assume that your endpoints all start at the root of the server and not at You should call
I'm not seeing extra parameters or filters in that code though. Can you elaborate? |
@serrnovik As @julealgon mentioned, you'd set up the route component using the prefix and model. services.AddControllers() |
@xuzhg @julealgon thank you for your answers,
When I used conventionional routes, I've in fact used
Here the usage is clear. I confirm, in fact in simple case if I add model routes are getting mapped. So, in the example bellow: public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
[Route("odata")]
public class CustomersController : ODataController
{
[EnableQuery]
[HttpGet("Customers")]
public ActionResult List()
{
return Ok(new List<Customer>
{
new Customer { Id = 1, Name = "Customer 1" },
new Customer { Id = 2, Name = "Customer 2" }
});
}
[HttpGet("Customers({key})")]
public ActionResult Details([FromRoute] int key)
{
return Ok(new Customer { Id = key, Name = $"Customer {key}" });
}
[HttpGet("Customers({key})/Address")]
public ActionResult Address([FromRoute] int key)
{
return Ok("Sample street 1");
}
[HttpGet("BestCustomers")]
public ActionResult ListBest()
{
return Ok(new List<Customer>
{
new Customer { Id = 2, Name = "Customer 2" }
});
}
} With simple model public static class CustomersModel
{
public static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Customer>("Customers");
var model = builder.GetEdmModel();
return model;
}
} and services.AddControllers()
.AddOData(
options =>
{
options.Count().Filter().Select().OrderBy().SetMaxTop(500);
options.AddRouteComponents(routePrefix: "odata",
model: custormesDataModel);
options.EnableAttributeRouting = true;
}); We have two routes build (as non conventional) In my application I would have controller that requests data from server with it's own business logic and I would build a model like c#
However the The questionSo maybe in fact, the question was not exact enough, from the blog and documentation I had an idea that I can, using custom routes and maybe some extra steps have endpoints like
and be able to feed them to odata aware client. Can this be done? Right now I've created separate controller for the case like BestCustomers in example above, but this doesn't look like elegant solution. And for the example with Adress it is even less clean to create separate model/controller |
@serrnovik That Similarly, the
(or, if you are willing to rename the route):
The code to declare such function would look similar to the following: var customerType = builder.EntityType<Customer>();
customerType.Collection
.Function("BestCustomers")
.ReturnsCollectionFromEntitySet<Customer>("Customers"); EDIT: Also, on an unrelated note, I'd strongly recommend you start leveraging the |
@julealgon Thank you for this detailed explanation. Actually it fully answers my question/problem, especially with the use of custom function. Thanks for the |
The MS examples of OData 8 seem to suggest using Ok(x), see https://learn.microsoft.com/en-us/odata/webapi-8/fundamentals/navigation-routing?tabs=net60%2Cvisual-studio for example? |
That's not good... the examples in the doc should definitely be updated IMHO. @xuzhg FYI |
@serrnovik @broomfn @julealgon For the benefit of other users, the return type of |
Not all flows will work correctly with Take this problem for example:
This is the key aspect that makes Using just It is valid syntax for sure, but since it can lead to these hard to debug problems, I'd recommend discouraging it on new code in the documentation, or find a way to change how OData works to support both. |
Using
I'm unable to make Attribute routes work properly. Having problem with my own controller I've tried following to the letter example in (newly updated) documentation:
Endpoints are working, but without odata (no
"@odata.context": "..."
and most importantly"@odata.count"
)Requesting:
http://localhost:5001/odata/customers/
which is coherent with the fact that routes fall under Non-OData Endpoint Mappings
Tried:
options.EnableAttributeRouting
true
/false
Route
attributeConvention based routes work just fine.
Documentation clearly says that is it not expected behavior.
What could be the issue?
The text was updated successfully, but these errors were encountered: