0
votes

I am trying to get OData to work in ASP.Net 2.1. The main Get is ok as I get the results from the DB.

When I try to call the second Get with a Parameter it returns with a 404. I put a breakpoint on the second get and it never hits. The http "get" statement http://localhost:5000/odata/userrole('Admin')

Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/odata/userrole('Admin')
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 423.7117ms 404

What am I not doing right here? Thank you.

Microsoft.AspNetCore.Odata = v7.1.0

namespace MyApp.Controllers
{
    public class UserRoleController : ODataController
    {

        private IMyDB _db;

        public UserRoleController(IMyDB db)
        {
            _db = db;
        }

        [EnableQuery(PageSize = 20)]
        public IActionResult Get()
        {
            return Ok(_db.UserRole().AsQueryable());
        }

        [EnableQuery]
        public IActionResult Get([FromODataUri] string roletype)
        {
            return Ok(_db.UserRole().Find(roletype));
        }
    }
}
1

1 Answers

0
votes

I have found out what is the problem. In the future if someone is working with OData this might help in countless hours of debugging.

The UserRole Class I have looks like below. Notice the data annotation [Key], that is what is causing the OData to ignore the path. I need to change the data Type to match the UserRole class "key" public IActionResult Get([FromODataUri] Guid roleid) That fixes the problem. Hope this helps people in the future.

public class UserRole
{
   [Key]
   public Guid RoleId { get; set; }
   public Role RoleType { get; set; }
   public string RoleName { get; set; }
}