我正在构建一个ASP.NET Core 2.0 Web API。我的控制器有几个方法。但是,当我从Postman调用时,所有方法都无法工作。每次它都表现得像HTTP(Get、Put、Post和Delete)动词。那么,如何从客户端调用我的所需方法?或者,这种方式是否可行?
以下是我的代码:
以下是我的代码:
namespace DutchTreat.Controllers
{
[Route("api/[controller]")]
public class OrdersController : Controller
{
private readonly IDutchRepository _ctx;
private readonly ILogger<OrdersController> _logger;
public OrdersController(IDutchRepository _ctx, ILogger<OrdersController> logger)
{
this._ctx = _ctx;
_logger = logger;
}
[HttpGet]
public IActionResult Get()
{
try
{
return Ok(_ctx.GeAllOrders());
}
catch (Exception e)
{
return BadRequest("Something Went Wrong");
}
}
[HttpGet("{id:int}")]
public IActionResult GetOrdersById(int id)
{
try
{
var orders = _ctx.GetOrdersById(id);
if (orders != null)
return Ok(orders);
else
return NotFound();
}
catch (Exception e)
{
return BadRequest("Something Went Wrong");
}
}
[HttpPost]
public IActionResult Post(Order model)
{
try
{
_ctx.AddEntity(model);
_ctx.SaveChanges();
return Created($"/api/orders/{model.Id}", model);
}
catch (Exception e)
{
return BadRequest("Something Went Wrong");
}
}
public IActionResult SampleMethod()
{
// Sample Method Definition
return null;
}
}
}
我向Postman发出请求:http://localhost:8888/api/orders/SampleMethod,结果显示404未找到!