c# - .NetCore Api 总是得到 404 Not Found
问题描述
我正在使用 .NetCore 开发 RestApi,但是当我尝试使用 Postman 对其进行测试时,我总是收到 404 Not Found 响应。我正在使用 MVC 模型,我将在下面留下我的模型、视图和控制器。这是我用来调用我的 Api http://localhost:5000/api/commands 的链接
模型
namespace Commander.Models
{
public class Command
{
public int Id { get; set; }
public string HowTo { get; set; }
public string Line { get; set; }
public string Platform { get; set; }
}
}
界面
using System.Collections.Generic;
using Commander.Models;
namespace Commander.Data
{
public interface ICommanderRepo
{
IEnumerable<Command> getAppCommands();
Command GetCommandById(int id);
}
}
实现接口的类
using System.Collections.Generic;
using Commander.Models;
namespace Commander.Data
{
public class MockCommanderRepo : ICommanderRepo
{
public IEnumerable<Command> getAppCommands()
{
var commands = new List<Command>
{
new Command{Id=0,HowTo="Boil an egg",Line="Boil water",Platform="Kettle & Pan"},
new Command{Id=1,HowTo="Cut bread",Line="Get a knife",Platform="Knife & chopping board"},
new Command{Id=2,HowTo="Make cup of tea",Line="Placce teabag in cup",Platform="Kettle & Cup"}
};
return commands;
}
public Command GetCommandById(int id)
{
return new Command { Id = 0, HowTo = "Boil an egg", Line = "Boil water", Platform = "Kettle & Pan" };
}
}
}
控制器
using System.Collections.Generic;
using Commander.Data;
using Commander.Models;
using Microsoft.AspNetCore.Mvc;
namespace Commander.Controllers
{
[Route("api/commands")]
[ApiController]
public class CommandsController : ControllerBase
{
private readonly MockCommanderRepo _repository = new MockCommanderRepo();
[HttpGet]
public ActionResult<IEnumerable<Command>> GetAllComands()
{
var commandItems = _repository.getAppCommands();
return Ok(commandItems);
}
[HttpGet("{id}")]
public ActionResult<Command> GetCommandById(int id)
{
var commandItem = _repository.GetCommandById(id);
return Ok(commandItem);
}
}
}
启动.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Commander
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
解决方案
尝试这个:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers(); // add this line
endpoints.MapRazorPages();
});
推荐阅读
- c# - 如何确定我正在执行的程序集是否在 Program Files 特殊文件夹中?
- javascript - 禁止水平滚动(jquery.甘特图)
- r - data.table `:=` 调用卡住了
- sql-server - SSRS 中用于从地址中删除前缀的函数
- spring - 在配置 Alfresco LDAP 集成时提供自定义命名空间
- javascript - 基于日期在 Google Sheet 脚本中第一列合并单元格中的颜色行
- javascript - 如何从 JQuery 函数中提取特定数量的元素?
- apache-spark - YARN doesn't recognize increased 'yarn.scheduler.maximum-allocation-mb' and 'yarn.nodemanager.resource.memory-mb' values
- git - git revert 行尾仅更改
- python - 以 API 为数据源的 Django 模型