首页 > 解决方案 > .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();
            });
        }
    }
}

标签: c#asp.net-mvc.net-corebackendrest

解决方案


尝试这个:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers(); // add this line
    endpoints.MapRazorPages();
});

推荐阅读