我正在尝试创建Web API,但是当我执行该程序时,会弹出以下消息: 找不到此本地主机页面。找不到网址“ TheAdress”的网页。
我正在学习,所以不确定要发布的代码是否会有所帮助(这是我编写的所有代码)。
控制器:
namespace ApiPractice.Controllers
{
[Route("api/[controller]")] //puts the endpoint communication in "Auto" (It takes it from AutoController)
[ApiController] //automate code
public class AutoController : ControllerBase
{
private readonly AplicationDbContext context;
public AutoController(AplicationDbContext context)
{
this.context = context;
}
[HttpGet]
public ActionResult<IEnumerable<Usuarios>> Get()
{
return context.Users.ToList();
}
}
}
实体:
namespace ApiPractice.Entities
{
public class Usuarios
{
public int Id { get; set; }
public string Nombre { get; set; }
}
}
上下文
namespace ApiPractice.Context
{
public class AplicationDbContext: DbContext
{
public AplicationDbContext (DbContextOptions<AplicationDbContext> options) : base(options) //does some weird shit, but it works.
{ }
public DbSet<Usuarios> Users { get; set; } //Somehow the database creates a table based on Users.
}
}
Startup.cs,ConfigureServices方法:
public void ConfigureServices(IServiceCollection services)
{
//No idea what this does, but I needed for the creation of the table
services.AddDbContext<AplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddControllers();
}
我还在api / autores项目属性内的调试菜单上更改了启动浏览器属性...不知道为什么我必须这样做...
仅此而已,如果您还有其他需要,我将在收到您的请求后立即提供给您,非常感谢您的宝贵时间,希望大家度过愉快的一天。 (:
答案 0 :(得分:0)
@mason指出我使用了错误的URL。