【问题标题】:REST-Api not working ControllerBase - 404 error (C# .Net Core)REST-Api 不工作 ControllerBase - 404 错误(C# .Net Core)
【发布时间】:2021-09-27 20:08:46
【问题描述】:

我正在尝试为我的 C# 和 .Net Core Web 应用程序设置一个 Rest-API。在使用标准的 ASP.NET Core Web 应用程序模板并使用 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中的所有设置都正确了吗?
  • 您介意发布您的 Startup.cs 内容吗?
  • @DavidG 这可能就是问题所在。我添加了启动代码。

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


【解决方案1】:

在您的 Startup.cs 中,您缺少 ConfigureServices 中的控制器调用,如下所示:

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();
        });
    }

【讨论】:

    【解决方案2】:

    在Startup#ConfigureServices中,请添加

    services.AddControllers()
    

    紧接着

    services.AddRazorPages();
    

    在 Startup#Configure 中,添加

    endpoints.MapControllers();
    

    紧接着

    endpoints.MapRazorPages();
    

    【讨论】:

      猜你喜欢
      • 2020-08-14
      • 1970-01-01
      • 2020-06-05
      • 2018-05-31
      • 2016-07-17
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-14
      相关资源
      最近更新 更多