目前,我正在使用Microsoft MVC框架开发Web API。在他们的文档中,我阅读了以下内容(https://docs.microsoft.com/zh-cn/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1):
为方便起见,属性路由通过将令牌括在方括号([,])中来支持令牌替换。标记[action],[area]和[controller]将被定义路径的动作中的动作名称,区域名称和控制器名称的值替换。在此示例中,操作可以匹配注释中所述的URL路径:
[Route("[controller]/[action]")]
public class ProductsController : Controller
{
[HttpGet] // Matches '/Products/List'
public IActionResult List() {
// ...
}
[HttpGet("{id}")] // Matches '/Products/Edit/{id}'
public IActionResult Edit(int id) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
令牌替换是构建属性路由的最后一步。上面的示例的行为与以下代码相同:
public class ProductsController : Controller
{
[HttpGet("[controller]/[action]")] // Matches '/Products/List'
public IActionResult List() {
// ...
}
[HttpGet("[controller]/[action]/{id}")] // Matches '/Products/Edit/{id}'
public IActionResult Edit(int id) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
但是,每当我尝试使用该[HttpGet("my/route")]属性时,Visual Studio都会不断告诉我“ HttpGetAttribute不包含带有1个参数的构造函数”。我已经读过我应该Microsoft.AspNet.WebApi.WebHost使用软件包管理器进行安装,但是错误仍然存在。
我的问题是,我如何才能开始使用适当的属性?我没有在Visual Studio中安装软件包的经验。
感谢您的协助。