c# - REST-Api 不工作 ControllerBase - 404 错误(C# .Net Core)

标签 c# .net asp.net-core model-view-controller controller

我正在尝试为我的 C# 和 .Net Core Web 应用程序设置 Rest-API。在使用标准的 ASP.NET Core Web 应用程序模板并将 Controller 添加到具有读/写操作的 API Controller 的包中时,我认为 URL:https://localhost:5001/api/values 会给我一个响应。相反,它返回一个错误 (404)。
ValuesController.cs 代码:

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// For more information on enabling Web API for empty projects, visit 
https://go.microsoft.com/fwlink/?LinkID=397860

namespace CSharp_met_database.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    // GET: api/<ValuesController>
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/<ValuesController>/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
        return "value";
    }

    // POST api/<ValuesController>
    [HttpPost]
    public void Post([FromBody] string value)
    {
    }

    // PUT api/<ValuesController>/5
    [HttpPut("{id}")]
    public void Put(int id, [FromBody] string value)
    {
    }

    // DELETE api/<ValuesController>/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
    }
  }
}
也许我错过了一些东西,因为我是 C# 和 .Net Core 的新手。
编辑
Startup.cs 代码:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CSharp_met_database
{
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 中,您缺少 ConfigureServices 中的 Controller 调用,如下所示:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
        services.AddControllers();
    }
在 Configure 方法中也缺少它:
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();
            endpoints.MapControllers();
        });
    }

关于c# - REST-Api 不工作 ControllerBase - 404 错误(C# .Net Core),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68452729/

相关文章:

c# - 尝试全局启用 CORS 时出现异常

c# - 应用程序不会以 0 个线程退出

c# - 机器人框架 : Why can't I use SendAsync?

.net - Form_Load() 'event' 或覆盖 OnLoad()

c# - LINQ to SQLite无法更新:“存储更新,插入或删除语句影响了意外的行数”

asp.net-core - Visual Studio 2017错误:找不到 “”的项目信息这可能表明缺少项目引用

c# - OrderBy 忽略重音字母

c# - 将网站和类库部署到 Azure

c# - 64 位 Windows 服务器中的 Sybase 驱动程序错误

c# - MVC 6 中的 CorrelationID 在哪里