Add a function or parameter to OData controller

╄→尐↘猪︶ㄣ 提交于 2019-12-11 15:14:24

问题


I'm using ASP.NET Boilerplate framework for ASP.NET Core. I have the boilerplate OData controllers as per https://aspnetboilerplate.com/Pages/Documents/OData-AspNetCore-Integration.

I want to support passing of a custom parameter in either the GET method or in a custom OData function. How do I do this in the AbpODataEntityController?

Regards, David


回答1:


Looking at the source code, it looks like they are using the standard "Microsoft.AspNet.OData" Version="7.1.0".

So you probably have a place where you set up your EdmModel. You should create a function in your controller like this:

// odata/Tenants/Default.IsDomainAvailable('<domain name here>')
[HttpGet]
public IActionResult IsDomainAvailable([FromODataUri] string domainName)
{
    if (!ModelState.IsValid) return BadRequest();
    try
    {
        var item = _unitOfWork.Tenants
            .FindByHostName(domainName)
            .FirstOrDefault();

        if (item == null) 
            return Ok(string.Format("{0} is available", domainName));

        return StatusCode(StatusCodes.Status409Conflict, string.Format("{0} is not available", domainName));
    }
    catch (Exception ex)
    {
        return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
    }
}

Then you can just declare a function in your EDM model builder like this:

private void BuildFunctions(ODataModelBuilder builder)
{
    builder.EntityType<TenantDTO>().Collection
        .Function("IsDomainAvailable")
        .Returns<IActionResult>()
        .Parameter<string>("domainName");
}

And call it from Postman like this:

odata/Tenants/Default.IsDomainAvailable('<domain name here>')

Hope this helps.



来源:https://stackoverflow.com/questions/51625228/add-a-function-or-parameter-to-odata-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!