C# 控制器的正确url
你能告诉我属性为'id'的控制器方法GetProfileById的正确url是什么吗?我尝试了许多url,如','',但邮递员显示消息“无法获取和我的url” 以下是代码:C# 控制器的正确url,c#,asp.net,url,asp.net-core,controllers,C#,Asp.net,Url,Asp.net Core,Controllers,你能告诉我属性为'id'的控制器方法GetProfileById的正确url是什么吗?我尝试了许多url,如','',但邮递员显示消息“无法获取和我的url” 以下是代码: using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SocialNetwork.Repositories; using SocialNetwork.Repos
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();
}
}
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Authorization;
使用Microsoft.AspNetCore.Mvc;
使用SocialNetwork.Repositories;
使用SocialNetwork.Repositories.GenericRepository;
命名空间SocialNetwork.Controllers
{
[路由(“/api/[controller]”)
公共类ProfileController:控制器
{
私有只读档案库;
公共配置文件控制器(IProfileRepository存储库)
{
this.repository=(ProfileRepository)存储库;
}
[HttpGet(“{id}”)]
[ProductsResponseType(200,Type=typeof(Profile))]
[产品响应类型(404)]
公共异步任务GetProfileById(int id)
{
var profile=await repository.GetById(id);
if(profile!=null)
{
返回新的OkObjectResult(Json(profile));
}
返回NotFound();
}
}
}应该是
/api/profile/
。您可以通过方法上方的[Route(“/api/[controller]”)
指令来判断这一点。然后,在斜杠后面再加上一个id
因此,您使用localhost
(或您的域)的完整路径将是“,”是用于此目的的好文章
在您的情况下,正确的URL是“”
注意:如果您仍然对Postman有问题,请搜索如何配置它。是否为
/api/profile/
?为了提高精度,将Route
属性与HttpGet
参数结合起来组成URL。您实际上可以有[HttpGet(“{id}/json”)]
,URL将是http://localhost:5000/api/Profile/1/json
。