Skip to content

NullReferenceException in UnversionedODataPathRouteConstraint line 54 #531

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

Closed
luanle1106 opened this issue Aug 10, 2019 · 3 comments
Closed

Comments

@luanle1106
Copy link

I have this issue when request query below:

http://localhost:60365/odata/Orders(20F37D86-3427-4A9E-8D63-16AEDDDB28FC,'BE')

NullReferenceException: Object reference not set to an instance of an object.
Microsoft.AspNet.OData.Routing.UnversionedODataPathRouteConstraint.Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) in UnversionedODataPathRouteConstraint.cs, line 54

I'm using

Microsoft.AspNetCore.OData 7.2.1

and

Microsoft.AspNetCore.OData.Versioning 3.2.0.

This is my api config:

services.AddApiVersioning(options =>
             {
                 options.AssumeDefaultVersionWhenUnspecified = true;
                 options.ApiVersionReader = ASOFTApiVersions.CombineApiVersion;
                 setupAction?.Invoke(options);
             });
services.AddMvcCore(options => options.EnableEndpointRouting = false);

            // Add odata and enable Apiversion support
services.AddOData().EnableApiVersioning();

services.AddSingleton(sp => new ODataUriResolver
            {
                EnableNoDollarQueryOptions = true
            });

My model configuration:

public class OrdersModelConfiguration : IModelConfiguration
    {
        public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
        {
            var orderEntityBuilder = builder.EntitySet<POST0016Entity>("Orders");

            orderEntityBuilder.EntityType.HasKey(m => new { m.APK, m.DivisionID });

            orderEntityBuilder.EntityType.Ignore(m => new { m.DeleteFlg });

            orderEntityBuilder.EntityType.Expand(SelectExpandType.Allowed, "Items");

            builder.EntityType<POST00161Entity>().HasKey(e => new { e.APK, e.DivisionID });
        }
    }

And my action in controller:

[EnableQuery]
public IActionResult Get([FromODataUri] Guid keyAPK, [FromODataUri] string keyDivisionID)
{
            return Ok(SingleResult.Create(_context.Orders
                .Where(e => e.APK == keyAPK && e.DivisionID == keyDivisionID).AsNoTracking()));
 }
@commonsensesoftware
Copy link
Collaborator

I thought I had patched this already, but I guess I forgot or otherwise missed it. This is the same issue as described in #515. This will definitely be fixed and go out in the patch within the next few days.

The fix will properly produce 400 instead of a 500 from a NullReferenceException. This issue ultimately happens because no API version was specified and you allowed implicit versioning. Based on what I can discern from you configuration, that means the selected API version will be options.DefaultApiVersion, which is will have a default value of 1.0. The route will only match a controller mapped to 1.0 or is API version-neutral. It appears that neither of these were the case, which led down the path to produce a 400. Obviously, things should not blow up, but I'm willing to bet that either your route configuration is still incorrect or the client is not requesting the correct URL. If everything lines up, you shouldn't see this error, which should unblock you while I work on getting the patch out.

Thanks

@luanle1106
Copy link
Author

Thanks so much

@commonsensesoftware
Copy link
Collaborator

FYI ... the patch for this has been published.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants