简体   繁体   English

500错误响应 swagger ASP.NET web API 查询 SQL 服务器

[英]500 error response swagger ASP.NET web API query SQL Server

[HttpGet]

public async Task<IActionResult> GetAllTransaction()
{
    var transaction = await _fullStackDbContext.Transactions.ToListAsync();
    return Ok(transaction);
}

[HttpGet]

public async Task<IActionResult> GetTotalItem()
{
    var transaction= await _fullStackDbContext.Transactions.FromSqlRaw("Select Name from Transactions").ToListAsync();
    return Ok(transaction);
}

I can list and try out the first HttpGet in swagger but in the second HttpGet, swagger returns a 500 error response in the browser console.我可以在 swagger 中列出并尝试第一个 HttpGet,但在第二个 HttpGet 中,swagger 在浏览器控制台中返回 500 错误响应。 Is my query syntax wrong or another problem?我的查询语法错误还是其他问题? Also, I'm not sure if this is the method to do custom SQL queries in ASP.NET Core Web API for SQL Server另外,我不确定这是否是在 ASP.NET Core Web API 中为 SQL 服务器执行自定义 SQL 查询的方法

Alright, thank you guys I have known the answer.好的,谢谢各位,我已经知道答案了。 Apparently, the error message gives this swagger JSON link that pointed to the error.显然,错误消息给出了指向错误的 swagger JSON 链接。 I thought every error appear in the browser console but this one must be clicked.我认为每个错误都会出现在浏览器控制台中,但必须单击此错误。

The error is SwaggerGeneratorException: Conflicting method/path combination which is solved by having错误是 SwaggerGeneratorException: Conflicting method/path combination 这是通过有

[HttpGet("~/getsomething")]
[HttpGet("~/getothersomething")]

Yes, you can use raw SQL like that (SQL queries are useful if the query you want can't be expressed using LINQ, or if a LINQ query causes EF to generate inefficient SQL.)是的,您可以像这样使用原始 SQL(如果您想要的查询无法使用 LINQ 表示,或者如果 LINQ 查询导致 EF 生成低效的 SQL,则 SQL 查询很有用。)

If you are using MS SQL Server try select * from dbo.Transactions如果您使用的是 MS SQL 服务器,请尝试select * from dbo.Transactions

Maybe there was an error because the returned query couldn't be casted to Transaction type, if you want to return just the name, run the query and Select after ToListAsync() is returned.可能有一个错误,因为返回的查询无法转换为Transaction类型,如果您只想返回名称,请在返回 ToListAsync() 后运行查询和 Select。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM