调用 ASP.Net 核心时 404 API
404 when calling ASP.Net Core API
我有一个带有 Web API 控制器的 ASP.Net 核心 MVC 应用程序,我相信我提供了正确的路由和所有内容,但在调用任何 API 邮递员中的控制器
控制器的外观如下:
[Route("api/[controller]")]
[ApiController]
public class PetsController : Controller
{
private readonly IPetService _petService;
public PetsController(IPetService _petService)
{
this._petService = _petService;
}
[HttpPost("Activation")]
public async Task<IActionResult> Activation(Guid id)
{
var serviceResult = await _petService.DeletePet(id);
return Ok(serviceResult);
}
[HttpGet("GetPet/{id}")]
public async Task<IActionResult> GetPet(Guid id)
{
var serviceResult = await _petService.GetPetDetails(id);
return Ok(serviceResult);
}
[HttpGet("GetPets/{pageSize}/{pageIndex}/{keyWord}")]
public async Task<IActionResult> GetPets(int pageSize, int pageIndex, string keyWord)
{
var serviceResult = await _petService.GetPets(pageSize, pageIndex, keyWord);
return Ok(serviceResult);
}
[HttpPost("SavePet")]
public async Task<IActionResult> SavePet(PetDTO pet)
{
var serviceResult = (Object)null;
if (!pet.Id.HasValue)
serviceResult = await _petService.CreatePet(pet);
else
serviceResult = await _petService.EditPet(pet);
return Ok(serviceResult);
}
[HttpGet("GetPetDues/{id}")]
public async Task<IActionResult> GetPetDues(Guid id)
{
var serviceResult = await _petService.GetPetDues(id);
return Ok(serviceResult);
}
[HttpGet("GetPetAppointments/{id}")]
public async Task<IActionResult> GetPetAppointments(Guid id)
{
var serviceResult = await _petService.GetPetAppointments(id);
return Ok(serviceResult);
}
[HttpGet("GetPetDiseases/{petId}")]
public async Task<IActionResult> GetPetDiseases(Guid petId)
{
var serviceResult = await _petService.GetPetDiseases(petId);
return Ok(serviceResult);
}
}
启动设置:
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50521",
"sslPort": 44310
}
这是我如何在 Postman 中调用 API 的示例:
http://localhost:50521/api/pets/GetPets/10/0/
我相信我应该得到 200 OK 但我仍然收到 404 错误!我该如何解决这个问题?
路线模板使用不当
[HttpGet("GetPets/{pageSize}/{pageIndex}/{keyWord?}")]
public async Task<IActionResult> GetPets(int pageSize, int pageIndex, string keyWord = null)
{
var serviceResult = await _petService.GetPets(pageSize, pageIndex, keyWord);
return Ok(serviceResult);
}
原来使用的路由缺少模板参数。
事实上,所有显示的路线都缺少路线模板中的参数。
Route template reference
Tokens within curly braces ({ ... }) define route parameters that are bound if the route is matched. You can
define more than one route parameter in a route segment, but they must
be separated by a literal value. For example,
{controller=Home}{action=Index} isn't a valid route, since there's no
literal value between {controller} and {action}. These route
parameters must have a name and may have additional attributes
specified.
Literal text other than route parameters (for example, {id}) and the
path separator / must match the text in the URL. Text matching is
case-insensitive and based on the decoded representation of the URLs
path. To match a literal route parameter delimiter ({ or }), escape
the delimiter by repeating the character ({{ or }}).
我有一个带有 Web API 控制器的 ASP.Net 核心 MVC 应用程序,我相信我提供了正确的路由和所有内容,但在调用任何 API 邮递员中的控制器
控制器的外观如下:
[Route("api/[controller]")]
[ApiController]
public class PetsController : Controller
{
private readonly IPetService _petService;
public PetsController(IPetService _petService)
{
this._petService = _petService;
}
[HttpPost("Activation")]
public async Task<IActionResult> Activation(Guid id)
{
var serviceResult = await _petService.DeletePet(id);
return Ok(serviceResult);
}
[HttpGet("GetPet/{id}")]
public async Task<IActionResult> GetPet(Guid id)
{
var serviceResult = await _petService.GetPetDetails(id);
return Ok(serviceResult);
}
[HttpGet("GetPets/{pageSize}/{pageIndex}/{keyWord}")]
public async Task<IActionResult> GetPets(int pageSize, int pageIndex, string keyWord)
{
var serviceResult = await _petService.GetPets(pageSize, pageIndex, keyWord);
return Ok(serviceResult);
}
[HttpPost("SavePet")]
public async Task<IActionResult> SavePet(PetDTO pet)
{
var serviceResult = (Object)null;
if (!pet.Id.HasValue)
serviceResult = await _petService.CreatePet(pet);
else
serviceResult = await _petService.EditPet(pet);
return Ok(serviceResult);
}
[HttpGet("GetPetDues/{id}")]
public async Task<IActionResult> GetPetDues(Guid id)
{
var serviceResult = await _petService.GetPetDues(id);
return Ok(serviceResult);
}
[HttpGet("GetPetAppointments/{id}")]
public async Task<IActionResult> GetPetAppointments(Guid id)
{
var serviceResult = await _petService.GetPetAppointments(id);
return Ok(serviceResult);
}
[HttpGet("GetPetDiseases/{petId}")]
public async Task<IActionResult> GetPetDiseases(Guid petId)
{
var serviceResult = await _petService.GetPetDiseases(petId);
return Ok(serviceResult);
}
}
启动设置:
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50521",
"sslPort": 44310
}
这是我如何在 Postman 中调用 API 的示例: http://localhost:50521/api/pets/GetPets/10/0/
我相信我应该得到 200 OK 但我仍然收到 404 错误!我该如何解决这个问题?
路线模板使用不当
[HttpGet("GetPets/{pageSize}/{pageIndex}/{keyWord?}")]
public async Task<IActionResult> GetPets(int pageSize, int pageIndex, string keyWord = null)
{
var serviceResult = await _petService.GetPets(pageSize, pageIndex, keyWord);
return Ok(serviceResult);
}
原来使用的路由缺少模板参数。
事实上,所有显示的路线都缺少路线模板中的参数。
Route template reference
Tokens within curly braces ({ ... }) define route parameters that are bound if the route is matched. You can define more than one route parameter in a route segment, but they must be separated by a literal value. For example, {controller=Home}{action=Index} isn't a valid route, since there's no literal value between {controller} and {action}. These route parameters must have a name and may have additional attributes specified.
Literal text other than route parameters (for example, {id}) and the path separator / must match the text in the URL. Text matching is case-insensitive and based on the decoded representation of the URLs path. To match a literal route parameter delimiter ({ or }), escape the delimiter by repeating the character ({{ or }}).