I am developing a REST API using ASP.NET and i need to accept a parameter in the body of a random POST request in the API that represents another API resource.
As for an example, Suppose we have:
api/files
api/operations
Two resource representations that represent files and operations that can be performed on files.
Suppose i would like to accept an operation through POST request that merges two files, i would accept an object as following.
operation {
name: 'merge',
parameters:
['api/files/1', 'api/files/2']
}
I would like to verify the urls submitted in the parameters array. A first step would be match the urls to API routes so i can tell which resource the parameter target.
I created a sample ASP.NET Web API project using visual studio 2017 and debugged the following method. Nothing special except the "arrange" and "act" were taken from here.
// GET api/values
public IEnumerable<string> Get()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/api/values");
// Act
RouteData routeData = RouteTable.Routes.GetRouteData(context);
return new string[] { "value1", "value2" };
}
The routeData always returns a route that has an empty template which i don't know why it is there.
This is not a duplicate of Test if a request's URL is in the Route table. Because that solution didn't work for me.
Is there any solution or alternative for this (except writing a helper parser for string url to match with routes) ?