I am trying to implement Create and update logic on my ODataController on ASP.Net core 5.0 using 8.0.0-beta. My controller looks like this:
[HttpPost]
[Route("users/{key}/{operation}")]
public async Task<IActionResult> UpdateUser([FromODataUri] string key, [FromODataUri] string operation, [FromBody] Delta<User> userPatch)
{
IActionResult result = BadRequest($"Key:{key} / Operation:{operation.ToLower()}");
if (operation.ToLower() == "update")
{
result = await Patch(key, userPatch);
return result;
}
else if (operation.ToLower() == "create")
{
var newUser = new User();
userPatch.Patch(newUser);
var entity = await AddNewEntity(newUser, key);
result = Created(entity);
}
return result;
}
The update is working fine, I am able to verify using the REST client(http://localhost:5000/users/MY-USER-6/update)
However when I call create method using the REST client (http://localhost:5000/users/MY-USER-6/create) I see that the method works and data is inserted in the database and function exits without an issue, however the result I get on the REST client the response says Status Code: 500 Internal Server Error with the following exception:
System.InvalidOperationException: The request must have an associated EDM model. Consider using the extension method HttpConfiguration.MapODataServiceRoute to register a route that parses the OData URI and attaches the model information.
at Microsoft.AspNetCore.OData.Results.ResultHelpers.GenerateODataLink(HttpRequest request, Object entity, Boolean isEntityId)
at Microsoft.AspNetCore.OData.Results.CreatedODataResult`1.GenerateLocationHeader(HttpRequest request)
at Microsoft.AspNetCore.OData.Results.CreatedODataResult`1.ExecuteResultAsync(ActionContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
BTW if I change result = Created(entity); to result = Ok(entity); its working fine!
As per some blogs and channel9 videos I am trying to add endpoints.EnableDependencyInjection(); but looks like this is no longer available or may be valid in the 8.0.0-beta.
What am I missing in this?