[英]ASP.NET Core route attribute routing
My controller code looks like this:我的控制器代码如下所示:
[Route("api/[controller]")]
[ApiController]
public class PaymentDetailController : ControllerBase
{
[HttpGet]
public async Task<string> GetPaymentDetail()
{
return "Success";
}
}
I am trying to access like this我正在尝试像这样访问
http://localhost:47744/api/paymentdetail
http://localhost:47744/api/paymentdetail/GetPaymentDetail
I cant access my controller and method我无法访问我的控制器和方法
if you want to use route like this如果你想使用这样的路线
http://localhost:47744/api/paymentdetail/GetPaymentDetail
you need this controller route你需要这个控制器路由
[Route("api/[controller]/[action]")]
public class PaymentDetailController : ControllerBase
{
[HttpGet]
public async Task<string> GetPaymentDetail()
{
return "Success";
}
}
if you want to use route like this如果你想使用这样的路线
http://localhost:47744/api/GetPaymentDetail
you need this controller route你需要这个控制器路由
[Route("api/[action]")]
public class PaymentDetailController : ControllerBase
{
[HttpGet]
public async Task<string> GetPaymentDetail()
{
return "Success";
}
}
or you can use both routes或者你可以同时使用两条路线
[Route("api/[controller]")]
[ApiController]
public class PaymentDetailController : ControllerBase
{
[Route("~/api/PaymentDetail/GetPaymentDetail")]
[Route("~/api/GetPaymentDetail")]
public async Task<string> GetPaymentDetail()
{
return "Success";
}
}
[Route("api/[controller]")]
[ApiController]
public class PaymentDetailController : ControllerBase
{
[HttpGet]
[Route("GetPaymentDetail")]
public async Task<string> GetPaymentDetail()
{
return "Success";
}
}
Like this?像这样?
Your URL points to /api/paymentdetail
but your ActionMethod is called GetPaymentDetail()
.您的 URL 指向
/api/paymentdetail
但您的 ActionMethod 称为GetPaymentDetail()
。
The Url that should be working would most likely be /api/paymentdetail/getpaymentdetail
.应该工作的 URL 很可能是
/api/paymentdetail/getpaymentdetail
。
You might want to rename your ActionMethod to Get()
since it is already clear what resource you are getting.您可能希望将您的 ActionMethod 重命名为
Get()
因为您获取的资源已经很清楚了。
It can be accessed using http://localhost:47744/api/paymentdetail, and the route will look for the action of the first get method.可以使用 http://localhost:47744/api/paymentdetail 访问它,该路由将查找第一个 get 方法的操作。
Or like this:或者像这样:
[Route("api/[controller]")]
[ApiController]
public class PaymentDetailController : ControllerBase
{
[HttpGet("GetPaymentDetail")]
public async Task<string> GetPaymentDetail()
{
return "Success";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.