找不到C#netcore控制器
我已在现有IdentityServer4项目中添加了netcore控制器。这是我的密码找不到C#netcore控制器,c#,asp.net-core,identityserver4,asp.net-core-webapi,C#,Asp.net Core,Identityserver4,Asp.net Core Webapi,我已在现有IdentityServer4项目中添加了netcore控制器。这是我的密码 namespace IdentityServer4.Quickstart.UI { public class VersionController : Controller { IVersionService _repository; public VersionController(IVersionService repository) { _repositor
namespace IdentityServer4.Quickstart.UI
{
public class VersionController : Controller
{
IVersionService _repository;
public VersionController(IVersionService repository)
{
_repository = repository;
}
[HttpGet(nameof(GetBackgroundId))]
public IActionResult GetBackgroundId()
{
return new OkObjectResult(_repository.GetBackgroundId());
}
[HttpPut(nameof(SetBackgroundId))]
public IActionResult SetBackgroundId([FromQuery]int id)
{
_repository.SetBackgroundId(id);
return new NoContentResult();
}
}
}
我在startup.cs中还有以下代码行
app.UseMvcWithDefaultRoute();
我可以通过以下url访问帐户控制器
http://localhost:5001/account/login
但是,我无法通过以下url访问版本控制器:
http://localhost:5001/version/GetBackgroundId
错误代码是404
怎么了?您缺少控制器的路由前缀。您正在使用属性路由,因此需要包含整个所需路由 当前
GetBackgroundId
控制器操作将映射到
http://localhost:5001/GetBackgroundId
将路由添加到控制器
[Route("[controller]")]
public class VersionController : Controller {
IVersionService _repository;
public VersionController(IVersionService repository) {
_repository = repository;
}
//Match GET version/GetBackgroundId
[HttpGet("[action]")]
public IActionResult GetBackgroundId() {
return Ok(_repository.GetBackgroundId());
}
//Match PUT version/SetBackgroundId?id=5
[HttpPut("[action]")]
public IActionResult SetBackgroundId([FromQuery]int id) {
_repository.SetBackgroundId(id);
return NoContent();
}
}
还要注意路由令牌的使用,并且,Controller
已经有了提供这些结果的助手方法,而不是更新响应
参考您能否显示routeconfig文件的内容没有routeconfig文件。我添加了
app.UseMvcWithDefaultRoute()代码>我在发布后发现了这一点。这就是解决办法。