I am creating an application with .net core 6, but I have an error when performing the query, the first thing I did was connect to the database created with SQL Server, install the packages:
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Tools
- Create the Models folder
Since I will be using an existing database I opened the NuGet package manager console, and proceeded with the following command:
Scaffold-DbContext “Server=xxxxx;Database=xxxx;User ID=xxxx;Password=xxxxx” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
I then opened the File containing the context, and proceeded to send my connection string to the appsettings.json file , leaving it as follows:
{
"ConnectionStrings": {
"ConexionBD": "Server=xxxxx;Database=Cotizador;User ID=xxxx;Password=xxxxx"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
And since I'm working with .Net version 6, I've added the following lines of code in Program.cs , in order to keep the connection string out of the DBContext file, being like this:
builder.Services.AddDbContext<CotizadorContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("ConexionBD"));
});
From there add a blank MVC Controller in the Controllers folder, and add the following code:
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using CotizadorV3.Models;
using CotizadorV3.Clases;
namespace CotizadorV3.Controllers
{
public class ClientesController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpGet]
[Route("api/Clientes/listaClientes")]
public IEnumerable<ClienteCLS> listaClientes()
{
using (CotizadorContext bd = new CotizadorContext())
{
List<ClienteCLS> clientes = (from c in bd.Clientes
select new ClienteCLS
{
NombreCliente = c.NombreCliente,
}).ToList();
return clientes;
}
}
}
}
And put https://localhost:44458/api/listaClientes
in Postmat I get error 404
And I don't know what's wrong, since in 3.1 it works fine, performing these processes.
Update:
I made use of this command, Add-Migration "Version 1.1"
and if it connects and generates it without any problem.
Considering the data of the question.
In the controller:
In Postmann:
you are pointing to
api/listaClientes
Solution:
Use the URL
api/Clientes/listaClientes
SOLUTION
When you migrate/update or build a project with .NET CORE 6, it makes sweeping changes for projects like web apis and such. I had a project with .NET 2.1 but it started giving errors and I evaluated passing it to .net 6 the problem is that when generating it and starting to make queries with consumption of the api, when trying to consume it it never entered, reviewing and consulting various forums and documentation from Microsoft, this took me to the following link .
In the fundamental part of which I got an idea was in the paragraph:
It mentions that they must match the same with respect to the URL's, which angular already does with the line:
In order to get to the previous block of code, it is located in ClientApp and is named as proxy.conf.js , originally it is like this:
const { env } = require('process');
To add controllers, you must take the name placed before the controller, example: ClientController.cs , Client will be taken
and this is added to the block:
And so, every time Controllers are declared, this must be done, in Web Api's , before they were
api/{NombreControlador}/Acción
, now it is{NombreControlador}/Acción
and in your controller you must configure it like this, be it MVC/Api:
And finally the route to consult it in postmant would be like this:
and so you get the data without errors