每当我向控制器添加任何类型的路由时,每个请求都以404结尾。当没有[路由]时应用程序正常工作但是当我添加它时它会中断。我之前下载的项目在不同的机器上工作正常,我的旧项目不再工作,所以可能有些东西得到了更新/我弄坏了什么。
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();
}
答案 0 :(得分:2)
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());
}
}