[英]Query string and attribute routing together for controller .NET core web API
我試圖實現這樣的目標
namespace CoreAPI.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
[HttpGet]
public string GetValue(string name,string surname)
{
return "Hello " + name;
}
}
}
我想通過使用這兩個 URL 來調用這個控制器方法:
您可以通過在控制器方法之上定義多個路由來解決此問題
[HttpGet("GetValues")]
[HttpGet("GetValues/{name}/{surname}")]
public string GetValue(string name, string surname)
{
return "Hi" + name;
}
這將適用於http://localhost:11979/api/values/GetValues/John/lawrance
和http://localhost:11979/api/values/GetValues?name=john&surname=lawrance
添加更多:
[HttpGet]
[Route("GetValues")]
[Route("GetValues/{name}/{surname}")]
public string GetValue(string name,string surname)
{
return "Hello " + name + " " + surname;
}
這也有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.