Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Thanks for the article!
- I seem to be having mixed results trying to get my code working.
- I'm running .net core 2.1 and Microsoft.AspNetCore.OData Version=7.1.0.
- My startup extensions:
- // invoked from startup, ConfigureServices()
- public static IServiceCollection AddODataSupport(this IServiceCollection services)
- {
- services.AddOData();
- //Workaround: https://github.com/OData/WebApi/issues/1177
- services.AddMvcCore(options =>
- {
- foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>()
- .Where(_ => _.SupportedMediaTypes.Count == 0))
- {
- outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
- }
- foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>()
- .Where(_ => _.SupportedMediaTypes.Count == 0))
- {
- inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
- }
- });
- return services;
- }
- // invoked from startup, app.UseMVC()
- public static void ConfigureODataRouting(this IRouteBuilder routeBuilder)
- {
- routeBuilder.EnableDependencyInjection();
- routeBuilder.Select().Filter().OrderBy().Expand().Count().MaxTop(100);routeBuilder.MapODataServiceRoute("odata", "odata", GetEdmModel());
- }
- private static IEdmModel GetEdmModel()
- {
- var obuilder = new ODataConventionModelBuilder();
- obuilder.EntitySet<TransactionEntity>("Transactions");
- return obuilder.GetEdmModel();
- }
- Here's my controller:
- public class ODataController : ControllerBase
- {
- private readonly ITmDataContext dataContext;
- public ODataController(ITmDataContext dataContext)
- {
- this.dataContext = dataContext;
- }
- [Route("odata/transactions")]
- [HttpGet]
- [EnableQuery]
- public ActionResult<IEnumerable<object>> GetTransactions()
- {
- return dataContext.Transactions;
- }
- }
- I can hit the $metadata endpoint and get my edm xml.
- <?xml version="1.0" encoding="utf-8"?><edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx"><edmx:DataServices><Schema Namespace="TMS.DataManagement.Service.Data.Entities" xmlns="http://docs.oasis-open.org/odata/ns/edm"><EntityType Name="TransactionEntity"><Key><PropertyRef Name="Id" /></Key><Property Name="Id" Type="Edm.Int32" Nullable="false" /> ...
- and I can hit the odata/Transactions endpoint.
- HTTP GET https://localhost:5001/odata/Transactions returns data like
- [
- {
- "id": 2860,
- "school_id": 3,
- "type_code": 1,
- "post_status_code": 0,
- "post_date": "2019-07-01T00:00:00",
- "deleted": true,
- "description": "fee name",
- "comments": "",
- "insert_date": "2019-07-01T11:35:48.2343931",
- "modified_date": "2019-07-02T17:24:35.5232986",
- "last_modify_user_id": 100169
- }
- ,... etc
- ]
- BUT:
- 1) my results never have the edm metadata (payload is missing the "@odata.context" node)
- 2) some operations like $expand and $filter work, but others like $count=true don't include the count node.
- 3) If I try to consume my odata feed in a client application (like excel), I get an error
- 'StartArray' node was found when reading from the JSON reader. A 'StartObject' node was expected.
- If I remove the [Route] attribute on my endpoint as you suggested, I can't hit the odata/transactions endpoint. I get a 404.
- All of the above makes me think I haven't implemented the OData routing correctly, but I'm not sure where I've messed up.
- Any thoughts?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement