【发布时间】:2019-12-09 00:56:36
【问题描述】:
我有 BooksController 方法 Get:
[HttpGet]
public async Task<IActionResult> Get(string name)
{
@books = await _booksService.BrowseAsync(name);
return View("books",@books);
}
并查看 books.cshtml:
@model IEnumerable<WebPart.Infrastructure.DTO.BooksDto>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
@Html.ActionLink("Edit", "get", new { id = item.Id })
<br />
</tr>
}
当我尝试“单击”链接“编辑”时,我遇到了问题二运行我的第二个 get 方法并打开视图 edit:
[HttpGet("{booksId}")]
public async Task<IActionResult> Get(Guid booksId)
{
var @books = await _booksService.GetAsync(booksId);
return View("edit", @books);
}
这两个方法GET在同一个BooksController中。
我在这里做错了什么?因为它总是运行第一个 GET 方法。
【问题讨论】:
-
部分猜测,但它需要一个名为
booksId的参数,而您正在发送一个名为id的参数。匹配吗? -
是的,我看到如下链接:Books?id=a40ab714-b96e-40db-a55e-e853bdf16a87,但它应该运行 _booksService.GetAsync和视图 edit 需要打开
-
对,当您发送一个名为
booksId而不是id的参数时会发生什么?
标签: asp.net asp.net-mvc web-services razor controller