【发布时间】:2018-04-01 21:11:14
【问题描述】:
每当我向我的控制器添加任何类型的路由时,每个请求都以 404 结束。当没有 [Route] 时应用程序正常工作,但当我添加它时它会中断。我之前下载的项目工作正常,在不同的机器上工作正常,我的旧项目不再工作,所以可能是更新了一些东西/我破坏了一些东西。
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();
}
【问题讨论】:
-
你的action方法叫什么?例如,URL
http://localhost:57279:/api/values/index是否有效? -
我已经添加了所有代码并尝试了 URL,没有工作。
-
好吧,你的操作方法不叫
Index,改用http://localhost:57279:/api/values/returnvalues。 -
HttpGet 默认为控制器的根目录,因此提供代码的路径将是
http://localhost:57279:/api/values给定使用的属性路由。动作也需要公开 -
哦,是的,行动不公开 - 好地方@Nkosi
标签: c# asp.net-core-2.0 asp.net-core-routing