使用 ASP.NET 核心路由时本地主机 404

Localhost 404 when using ASP.NET Core routing

每当我向我的控制器添加任何类型的路由时,每个请求都以 404 结尾。当没有 [路由] 时应用程序工作正常,但当我添加它时它会中断。我下载的项目以前工作过,在不同的机器上工作正常,我的旧项目不再工作了,所以可能 updated/I 出了点问题。

ValuesController:

[Route("/api/[controller]")]
public class ValuesController : Controller
{
    private readonly ValuesService _valuesService;

    public ValuesController()
    {
        _valuesService = new ValuesService(); 
    }

    [HttpGet]
    IActionResult ReturnValues()
    {
        return Ok(_valuesService.ReturnValues());
    }
}

启动:

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IStudentResearchGroupData, StudentResearchGroupData>();
    services.AddMvc();

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme =
                                   JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme =
                                   JwtBearerDefaults.AuthenticationScheme;
    }).AddJwtBearer(o =>
    {
        o.Authority = "http://localhost:59418";
        o.Audience = "researchgroups";
        o.RequireHttpsMetadata = false;
    });
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseMvc();
    app.UseStaticFiles();
    app.UseAuthentication();
}

结果: 404 消息:

HttpGet 在没有给出路由模板时默认为控制器的根目录。

这意味着所提供代码的路径将是

http://localhost:57279:/api/values

给定使用的属性路由。

此外,操作需要 public 才能作为端点在外部可见。

[Route("api/[controller]")]
public class ValuesController : Controller {
    private readonly ValuesService _valuesService;

    public ValuesController() {
        _valuesService = new ValuesService(); 
    }

    //GET api/values
    [HttpGet]
    public IActionResult ReturnValues() {
        return Ok(_valuesService.ReturnValues());
    }
}

引用Routing to Controller Actions