向OData控制器添加函数或参数 [英] Add a function or parameter to OData controller

查看:258
本文介绍了向OData控制器添加函数或参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将ASP.NET Boilerplate框架用于ASP.NET Core.我按照 https://aspnetboilerplate.com/Pages/Documents/OData-AspNetCore-Integration .

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.

我想支持在GET方法或自定义OData函数中传递自定义参数.如何在AbpODataEntityController中执行此操作?

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?

关于, 大卫

推荐答案

查看源代码,看起来他们正在使用标准的"Microsoft.AspNet.OData" Version ="7.1.0".

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

因此,您可能有一个设置EdmModel的地方. 您应该像这样在控制器中创建一个函数:

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);
    }
}

然后,您可以像这样在EDM模型构建器中声明一个函数:

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");
}

从邮递员这样叫它:

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

希望这会有所帮助.

这篇关于向OData控制器添加函数或参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆