How do I bind multiple entity sets to one odata controller? - c#

I have a lot of different entities that I want to enable OData for. These entities are categorized into different groups based on their type. Currently, the default is to match the EntitySet with the controller name, but I don't want a controller for every entity type that I'll have. Is there a way I can map multiple EntitySets to one controller.
I've tried having the types I'm interested in implement a common interface and specified that interface as my entity set type. I also tried having two entities within one controller and with their own get requests, but no luck. I also tried defining my own routing class that extends EntitySetRoutingConvention, but haven't gotten that to work.
WebApiConfig
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<MyEntity1>("MyEntity1");
builder.EntitySet<MyEntity2>("MyEntity2");
config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
// Web API routes
config.MapHttpAttributeRoutes();
}
This looks for controllers named MyEntity1Controller and MyEntity2Controller.
What I want is something like:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<MyEntity1>("Generic");
builder.EntitySet<MyEntity2>("Generic"); // Throws an error since Generic is already registered to MyEntity1
config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
// Web API routes
config.MapHttpAttributeRoutes();
}
GenericController.cs
// GET: odata/myentity1
[EnableQuery]
public IQueryable<MyEntity1> GetMyEntity1()
{
return db.MyEntity1.AsQueryable();
}
// GET: odata/myentity2
[EnableQuery]
public IQueryable<myentity2> GetMyEntity2()
{
return db.MyEntity2.AsQueryable();
}
The expected results would be I can go to myurl/Generic/MyEntity1 and that would hit a GET request in my Generic Controller. I should also be able to perform odata operations such as myurl/Generic/MyEntity1?$select=Id.

Add the ODataRoute Attribute
/// MyController.cs
// GET: odata/myentity1
[EnableQuery]
[ODataRoute("myentity1")]
public IQueryable<MyEntity1> GetMyEntity1() => db.MyEntity1.AsQueryable();
// GET: odata/myentity2
[EnableQuery]
[ODataRoute("myentity1")]
public IQueryable<MyEntity2> GetMyEntity2() => db.MyEntity2.AsQueryable();

Related

Routing Post to a non standard uri web api

I´m writing a REST web api and I need to have an endpoint like /api/users/{id}/modify or http://localhost:8080/api/users/6/modify using a POST method.
I have a UsersController class with al read/write actions but I´m only able to access the post method by accessing /api/users, not /api/users/6/modify. I need to expand the hierarchy(if that is well said).
How can I do to achieve this?
You can use the Attribute Routing of asp.net web api.
The first thing is to enable it over the HttpConfiguration, in asp.net web api template, you can see it on the WebApiConfig.cs file.
using System.Web.Http;
namespace WebApplication
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
// Other Web API configuration not shown...
}
}
}
After that you can define a controller which should inherits from ApiController and you can use the Route attribute to define a custom route, for sample:
[RoutePrefix("api/users")]
public class UsersController : ApiController
{
[HttpPost]
[Route("{id}/modify")]
public HttpResponseMessage PostModify(int id)
{
// code ...
}
}
The RoutePrefix will define a prefix for all actions on the controller. So, to access the PostModify you should use a route like /api/users/6/modify in a post action. If you do not want it, just remove the RoutePrefix and define the complete url on the route attribute, like this: /api/users/{id}/modify.
You also can guarantee the type of the id argument defining a route like this:
[RoutePrefix("api/users")]
public class UsersController : ApiController
{
[HttpPost]
[Route("{id:int}/modify")]
public HttpResponseMessage PostModify(int id)
{
// code ...
}
}

OData, method get with key not found

With the code below, I can hit (using Fiddler):
GetCustomers via GET: odata/Customers
Post(CustomerModel customer) via POST: odata/Customers
Delete via DELETE: odata/Customers(5)
The delete method look like :
public IHttpActionResult Delete([FromODataUri] int key)
{
Console.WriteLine(key);
}
I hit the method and I get the key, no problem.
But I don't hit the get method with the key (no problem with the get method without the key, I get the full list) :
// GET: odata/Customers(5)
public IHttpActionResult GetCustomer([FromODataUri] int key)
{
Console.WriteLine(key);
}
I get this error (Response headers via Fiddler):
HTTP/1.1 404 Not Found
The WebApiConfig is :
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<CustomerModel>("Customers");
builder.EntitySet<EmployeeModel>("Employees");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "odata",
model: builder.GetEdmModel());
}
}
The method name needs to be Get to be picked up by the OData routing:
Get([FromODataUri] int key)
By Web API OData convention, it should support the following two rules:
HttpMethodName + entityTypeName
HttpMethodName
Convention #1 has high priority than convention #2.
Based on the conventions, you will get 404-NotFound if you only define the following actions in the controller:
GetCustomer([FromODataUri] int key)
GetCustomers([FromODataUri] int key)
Otherwise, it should work if you define at least one of the following actions in the controller:
GetCustomerModel([FromODataUri] int key)
Get([FromODataUri] int key)
https://learn.microsoft.com/en-gb/odata/webapi/built-in-routing-conventions lists the routing conventions used in Web API OData. Hope it can help you. Thanks.

OData v4 Custom Function

I'm trying to create a custom function in an OData v4 Web API solution. I need to return a collection of "Orders" based on unique logic that can't be handled natively by OData. I cannot seem to figure out how to create this custom function without destroying the entire OData service layer. When I decorate the Controller method with an ODataRoute attribute it all goes to hell. Any basic request produces the same error. Can someone please take a look at the code below and see if you notice something that I must be missing?
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.MapODataServiceRoute("odata", "odata", model: GetModel());
}
public static Microsoft.OData.Edm.IEdmModel GetModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Account>("Accounts");
builder.EntitySet<Email>("Emails");
builder.EntitySet<PhoneNumber>("PhoneNumbers");
builder.EntitySet<Account>("Accounts");
builder.EntitySet<Address>("Addresses");
builder.EntitySet<Order>("Orders");
builder.EntitySet<OrderDetail>("OrderDetails");
var orders = builder.EntityType<Order>();
var function = orders.Function("GetByExternalKey");
function.Parameter<long>("key");
function.ReturnsCollectionFromEntitySet<Order>("Orders");
return builder.GetEdmModel();
}
}
OrdersController.cs
public class OrdersController : ODataController
{
private SomeContext db = new SomeContext();
...Other Stuff...
[HttpGet]
[ODataRoute("GetByExternalKey(key={key})")]
public IHttpActionResult GetByExternalKey(long key)
{
return Ok(from o in db.Orders
where //SpecialCrazyStuff is done
select o);
}
}
}
When issuing ANY request against the OData layer I receive the following error response.
The path template 'GetByExternalKey(key={key})' on the action 'GetByExternalKey' in controller 'Orders' is not a valid OData path template. Resource not found for the segment 'GetByExternalKey'.
Per the model builder, the function GetByExternalKey is a bound function. According to the OData Protocol v4, a bound function is invoked through the namespace or alias qualified named, so you need to add more in the route attribute:
[HttpGet]
[ODataRoute("Orders({id})/Your.Namespace.GetByExternalKey(key={key})")]
public IHttpActionResult GetByExternalKey(long key)
{
return Ok(from o in db.Orders
where//SpecialCrazyStuff is done
select o);
}
If you don't know the namespace, just add below to the method GetModel():
builder.Namespace = typeof(Order).Namespace;
And replace "Your.Namespace" with the namespace of the type Order.
Here are 2 samples related to your question, just for your reference:
https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataFunctionSample/
https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataAttributeRoutingSample/

WebApi2 attribute routing inherited controllers

I'm trying to create basic REST api with a base controller like so:
Base class:
public abstract class WebApiEntityController<TEntity> : ApiController
where TEntity : EntityBase<TEntity, int>
{
private readonly IRepository<TEntity> _repository;
protected WebApiEntityController(IRepository<TEntity> repository)
{
_repository = repository;
}
[Route("")]
[WebApiUnitOfWork]
public HttpResponseMessage Get()
{
return Request.CreateResponse(HttpStatusCode.OK, _repository.ToList());
}
[..........]
Derived class:
[RoutePrefix("api/TimesheetTask")]
public class TimesheetTaskController : WebApiEntityController<TimesheetTask>
{
private readonly IRepository<TimesheetTask> _timeSheetTaskRepository;
public TimesheetTaskController(IRepository<TimesheetTask> timeSheetTaskRepository) : base(timeSheetTaskRepository)
{
_timeSheetTaskRepository = timeSheetTaskRepository;
}
}
but calling GET on the route ~/api/TimesheetTask/ results in a 404 not found.
According to this answer, attribute routes cannot be inherited. So my question is, how can I write a consistent API for all my domain models without having to copy and paste code?
I know I can do convention routing with this configuration:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
but then I'll have to specify the action, and my endpoints will be
/api/{controller]/Get
/api/{controller]/Post
and I don't want that. I can also remove the {action} part of the routeTemplate, but then how will I route to custom actions?
If anyone can help, that would be appreciated. Also, the next step for my domain model API would involve supporting querying, and that can easily get complicated. Is there a library that generates these routes for you? If anyone can help me find such a library it would be much appreciated.
The answer you quoted has since been updated. As of WebApi 2.2 they created an extensibility point to allow the feature you wanted.
Attribute rout can be inherited, but you need to configure it. I had the same requirement for a base API controller and after searching came across the same answer you quoted.
.NET WebAPI Attribute Routing and inheritance
You need to overwrite the DefaultDirectRoutePrivider:
public class WebApiCustomDirectRouteProvider : DefaultDirectRouteProvider {
protected override System.Collections.Generic.IReadOnlyList<IDirectRouteFactory>
GetActionRouteFactories(System.Web.Http.Controllers.HttpActionDescriptor actionDescriptor) {
// inherit route attributes decorated on base class controller's actions
return actionDescriptor.GetCustomAttributes<IDirectRouteFactory>(inherit: true);
}
}
With that done you then need to configure it in your web api configuration
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
.....
// Attribute routing. (with inheritance)
config.MapHttpAttributeRoutes(new WebApiCustomDirectRouteProvider());
....
}
}

ASP.NET Odata Web API CRUD Operations Not Working

I have spent a about 2 days setting up my Odata Web API project that was before a simple Asp.net mvc4 project.
But still I am not successful in operating even CRUD Operations.
It says this:
<m:message xml:lang="en-US">
No HTTP resource was found that matches the request URI 'http://localhost:53208/odata/Product'.
</m:message>
<m:innererror>
<m:message>
No action was found on the controller 'Product' that matches the request.
</m:message>
<m:type/>
<m:stacktrace/>
</m:innererror>
It means its reaching the Controller but not finding actions or there is some problem in my routing settings.
I have following in my WebApiConfig:
config.Routes.MapODataRoute("OData","odata",GetEdmModel());
My getEdmModel method:
private static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Product");
builder.EntitySet<OfferedProduct>("OfferedProduct");
IEdmModel model = builder.GetEdmModel();
return model;
}
My controller is like this:
public class ProductController : EntitySetController<Product,int>
{
private OfferAssistantDbContext db = new OfferAssistantDbContext();
List<Product> Products = new OfferAssistantDbContext().Products.ToList();
// GET api/Product
[Queryable(PageSize = 10)]
public override IQueryable<Product> Get()
{
return Products.AsQueryable();
}
// GET api/Product/5
public Product GetProduct([FromODataUri] int id)
{
return Products[id];
}
/// and so on... but for this time lets work only on GET operation
Now when I write this in my browser:
http://localhost:53208/odata/Product
it says the error I showed above..
Please guide me where is the problem?
I believe that your controller needs to inherit from ODataController:
public class ProductController : ODataController

Categories