Home > Software design >  500 error response swagger ASP.NET web API query SQL Server
500 error response swagger ASP.NET web API query SQL Server

Time:09-29

[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. 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

CodePudding user response:

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.)

If you are using MS SQL Server try 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.

CodePudding user response:

Alright, thank you guys I have known the answer. Apparently, the error message gives this swagger JSON link that pointed to the error. 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

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