首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >odata和非odata路由能否共存

odata和非odata路由能否共存
EN

Stack Overflow用户
提问于 2020-02-22 10:31:22
回答 1查看 164关注 0票数 0

我使用的是asp.net核心3.x,并且我已经安装了.net核心的odata nuget。

在我的服务部分,我像这样设置了odata

代码语言:javascript
运行
复制
        services.AddOData();
        services.AddODataQueryFilter();

        services.AddMvc(options =>
        {
            options.EnableEndpointRouting = false;
        });

配置如下所示。

代码语言:javascript
运行
复制
 app.UseMvc(b =>
        {
            b.MapODataServiceRoute("odata", "odata", GetEdmModel());
        });
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();

        });

当我使用/ odata /Accounts时,我会得到odata响应,是否可以同时获取/api/Accounts?

代码语言:javascript
运行
复制
 //[ODataRoutePrefix("Account")]
[Route("api/[controller]")]
[ApiController]
public class AccountsController : ControllerBase
{
    private readonly IAccountService _accountService;
    public AccountsController(IAccountService accountService)
    {
        _accountService = accountService;
    }
    [ODataRoute]
    [EnableQuery(PageSize = 2, AllowedQueryOptions = AllowedQueryOptions.All)]
    [HttpGet]
    public async Task<ActionResult<IEnumerable<Account>>> Get()
    {
        return Ok(await _accountService.GetAllAccounts());
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-02-24 15:16:47

使用.net core 3.x中的System.Text.Json序列化程序时,Odata可能会遇到错误。以便在您场景中可以使用AddNewtonsoftJson作为变通方法。注意:如果AddNewtonsoftJson方法不可用,请确保安装了Microsoft.AspNetCore.Mvc.NewtonsoftJson包。下面的代码示例供您参考:

Student.cs:

代码语言:javascript
运行
复制
public class Student
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public int Score { get; set; }
}

StudentsController.cs:

代码语言:javascript
运行
复制
[Route("api/[controller]")]
[ApiController]
public class StudentsController : ControllerBase
{
    [HttpGet]
    [EnableQuery()]
    public IEnumerable<Student> Get()
    {
        return new List<Student>
        {
            CreateNewStudent("Cody Allen", 130),
            CreateNewStudent("Todd Ostermeier", 160),
            CreateNewStudent("Viral Pandya", 140)
        };
    }

    private static Student CreateNewStudent(string name, int score)
    {
        return new Student
        {
            Id = Guid.NewGuid(),
            Name = name,
            Score = score
        };
    }
}

ConfigureServices:

代码语言:javascript
运行
复制
services.AddControllers(mvcOptions =>
          mvcOptions.EnableEndpointRouting = false)
        .AddNewtonsoftJson();

services.AddOData();
services.AddODataQueryFilter();

配置:

代码语言:javascript
运行
复制
app.UseMvc(b =>
{
    b.Select().Filter();
    b.MapODataServiceRoute("odata", "odata", GetEdmModel());
    b.EnableDependencyInjection();  //add this line

});

GetEdmModel:

代码语言:javascript
运行
复制
IEdmModel GetEdmModel()
{
    var odataBuilder = new ODataConventionModelBuilder();
    odataBuilder.EntitySet<Student>("Students");

    return odataBuilder.GetEdmModel();
}

这样https://localhost:44334/odata/students?$select=namehttps://localhost:44334/api/students?$select=name都会返回数据。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60348515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档