Open komdil opened 3 years ago
@komdil
Your config is opt.AddModel(""
. So, you defined an OData route without prefix.
https://localhost:44383/mycontextToken/Student
works without OData metadata, that's because it goes to ASP.NET routing, not OData route.
If you enable "app.UseODataRouteDebug();", you can send ~/$odata
to understand the "endpoints".
For the "dynamic prefix", would you please file a new issue with detail information?
@xuzhg Ok, I made some changes:
mvcBuilder.AddOData(opt =>
{
opt.EnableContinueOnErrorHeader = true;
opt.AddModel("{contextToken}", edmModel, configureAction =>
{
configureAction.AddService(Microsoft.OData.ServiceLifetime.Singleton, typeof(ODataBatchHandler), s => new MyODataBatchHandler());
configureAction.AddService(Microsoft.OData.ServiceLifetime.Singleton, typeof(ODataSerializerProvider), sp => new MyODataSerializerProvider(sp));
configureAction.AddService(Microsoft.OData.ServiceLifetime.Singleton, typeof(IEdmModel), s => edmModel);
configureAction.AddService(Microsoft.OData.ServiceLifetime.Singleton, typeof(ODataUriResolver), s => new AlternateKeyPrefixFreeEnumODataUriResolver(edmModel));
});
opt.Filter().Select().Expand().SetMaxTop(null).Count().OrderBy();
});
And I removed it from controller:
[HttpGet("[controller]")]
public IEnumerable<Student> Get(ODataQueryOptions<TEntity> queryOptions, CancellationToken cancellationToken)
{
var list = new List<Student>
{
CreateNewStudent("Cody Allen", 130),
CreateNewStudent("Todd Ostermeier", 160),
CreateNewStudent("Viral Pandya", 140)
};
return list;
}
[HttpGet("[controller]/{key}")]
public IActionResult Get(string key)
{
var student = CreateNewStudentWithGuid("Cody Allen", 130);
return Ok(student);
}
It is working fine with: https://localhost:44383/mytoken/Student
But it is throwing null refence exception on https://localhost:44383/Student
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.OData.Routing.ODataPathExtensions.GetEdmType(ODataPath path)
at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.OnActionExecuting(ActionExecutingContext actionExecutingContext)
at Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute.OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- 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()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
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.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.OData.Batch.ODataBatchMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Here is my action:
My configuration:
When I send request like: https://localhost:44383/mycontextToken/Student
It is returning entities without @oDataContext:
And I am getting this error on making select query: https://localhost:44383/mycontextToken/Student?$select=Id
It is working fine when I request without prefix: https://localhost:44383/Student?$select=Id
Also, is there any way put dynamic prefix on controller? You can see project here: https://github.com/komdil/ODataCoreTest/tree/dynamicPrefixIssue