Reputation: 525
I am creating a basic API with RavenDB and .NetCore 3.1 MVC. I am using the auto generated ID field which is "Modelnames/123-ClusterInstance". Since my controllers will default to myapi.com/api/ssolinks/{id}, the field will end up being myapi.com/api/ssolinks/SSOLinks/123-ClusterInstance (the id). How would you recommend I get around this? I found an old SO post here: Autogenerated Id in RavenDB but the documentation seems to be gone. I have worked around it by appending it in the HTTP request but it feels hacky. Thanks for any input!
namespace myAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class SSOLinksController : ControllerBase
{
[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult Get(string Id)
{
//I want to NOT do this if possible :D
Id = $"SSOLinks/{Id}";
var store = DocumentStoreHolder.CreateStore();
using (IDocumentSession session = store.OpenSession())
{
List<SSOLink> ssolinks = session
.Query<SSOLink>()
.Where(x => x.Id == Id)
.ToList();
return Ok(ssolinks);
}
}
}
}
Upvotes: 1
Views: 192
Reputation: 3839
Your current implementation seems ok - you get the Id from the URL params and then get it from the RavenDB server.
You can use startswith
& matches
in your URL params.
See in Query String Parameters in Get Documents By Prefix
From the Session you can use LoadStartingWith i.e.
var linkResult = session
.Advanced
.LoadStartingWith<SSOLink>("SSOLinks/", <the Id>)
.FirstOrDefault();
Upvotes: 2