【发布时间】:2020-10-12 18:57:34
【问题描述】:
我正在使用 .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);
}
}
}
Startup.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();
});
}
}
}
【问题讨论】:
-
您至少需要发布 Startup.cs 以了解您如何启动系统。
-
我更新了我的问题
-
试试
http://localhost:5000/api/commands/GetAllComands,你指定的那个地址没有调用action方法。另请注意“命令”的拼写错误。 -
仍然没有找到 404
标签: c# asp.net-mvc .net-core backend rest