【发布时间】:2019-01-17 02:51:49
【问题描述】:
您能否告诉控制器方法 GetProfileById 的正确 url 与属性 'id' 我尝试了许多 url,如 'http://localhost:5000/api/Profile/GetProfileById/1'、'http://localhost:5000/api/Profile/ProfileById/1'、'http://localhost:5000/api/ProfileById/1' 但邮递员显示消息'无法获取和我的网址'
代码如下:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SocialNetwork.Repositories;
using SocialNetwork.Repositories.GenericRepository;
namespace SocialNetwork.Controllers
{
[Route("/api/[controller]")]
public class ProfileController : Controller
{
private readonly ProfileRepository repository;
public ProfileController(IProfileRepository repository)
{
this.repository = (ProfileRepository)repository;
}
[HttpGet("{id}")]
[ProducesResponseType(200, Type = typeof(Profile))]
[ProducesResponseType(404)]
public async Task<ActionResult<Profile>> GetProfileById(int id)
{
var profile = await repository.GetById(id);
if(profile != null)
{
return new OkObjectResult(Json(profile));
}
return NotFound();
}
}
}
【问题讨论】:
-
不会是
/api/profile/<ID>吗?
标签: c# asp.net url asp.net-core controllers