I am trying to create a odata api endpoint using odata-4(inside web api 2.2 project), where I need to have one of my GET action in odata controller to accept multiple string parameters(they are custom search properties apart from properties in my entity for which odata controller is created).
But with all the trials i have done so far, i always get into one or other error while accessing the specific action in browser. and so far I haven't been able to get a working combination of flow/syntax hence sharing query here to get suggestion on either how to achieve passing multiple param to odata action in odata-4 OR how to fix error coming for me.
Code as follow:
package.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.Mvc" version="5.1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.OData" version="5.3.1" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.OData" version="5.1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.1.2" targetFramework="net45" />
<package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net45" />
<package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net45" />
<package id="Microsoft.OData.Core" version="6.5.0" targetFramework="net45" />
<package id="Microsoft.OData.Edm" version="6.5.0" targetFramework="net45" />
</packages>
WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<DocumentsModel>("SampleData");
var function = builder.Function("SampleFunction");
function.Parameter<string>("catGUIDOrText");
function.Parameter<string>("type");
function.Parameter<string>("isAutoCompSearch");
function.ReturnsCollectionFromEntitySet<DocumentsModel>("SampleData");
config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Odata Controller:
[ODataRoutePrefix("SampleData")]
public class SampleDataController : ODataController
{
[EnableQuery]
[HttpGet]
[ODataRoute("SampleFunction(catGUIDOrText={catGUIDOrText},type={type},isAutoCompSearch={isAutoCompSearch})")]
public IEnumerable<DocumentsModel> GetSampleData([FromODataUri] string catGUIDOrText, [FromODataUri] string type, [FromODataUri] string isAutoCompSearch)
{
return new List<DocumentsModel>().AsQueryable<DocumentsModel>();
}
}
Note: - DocumentsModel is a class with all string properties.
Error details
Now while accessing this action in browser via below URL, i am getting error:
URL: http://localhost/VirtualDirectoryNameInIIS/odata/SampleData/SampleFunction(catGUIDOrText='752',type='230',isAutoCompSearch='false')
Error which i am getting:
The path template 'SampleData/SampleFunction(catGUIDOrText={catGUIDOrText},type={type},isAutoCompSearch={isAutoCompSearch})' on the action 'GetSampleData' in controller 'SampleData' is not a valid OData path template. The request URI is not valid. Since the segment 'SampleData' refers to a collection, this must be the last segment in the request URI or it must be followed by an function or action that can be bound to it otherwise all intermediate segments must refer to a single resource.
Please help me with any inputs you may have around this, either in code or in url i am using to access the given method. Thanks.
Some references i followed for resolving this or to ensure i am following right direction/syntax: