[英]Routing issue after changing primary keys
After changing primary key from ID
to PostId
, it seems that the Post
Details.cshtml
view does not render the URL correctly. 将主键从ID
更改为PostId
,似乎Post
Details.cshtml
视图无法正确呈现URL。
If I navigate to https://localhost:xxxxx/Posts/Details/4
, the page renders correctly. 如果我导航到https://localhost:xxxxx/Posts/Details/4
,则页面将正确呈现。 However, the @Html.ActionLink
call in the Index.cshtml
view that was working previously now no longer works. 但是,以前运行的Index.cshtml
视图中的@Html.ActionLink
调用现在不再起作用。 It points to https://localhost:xxxxx/Posts/Details?PostId=4
, which is a blank page. 它指向https://localhost:xxxxx/Posts/Details?PostId=4
,这是一个空白页。 It seems like that URL should work, though, so I'm not sure whether the routing is the issue or it's something to do with the view. URL似乎应该起作用,但是,我不确定路由是问题还是与视图有关。 The same issue is occurring with a separate class (which also had its primary key changed). 单独的类(也更改了其主键)时,也会发生相同的问题。
The <a asp-action="Details" asp-route-id="@item.PostId">Details</a>
link in the Index.cshtml
view does work by pointing to https://localhost:xxxxx/Posts/Details/4
. Index.cshtml
视图中的<a asp-action="Details" asp-route-id="@item.PostId">Details</a>
链接通过指向https://localhost:xxxxx/Posts/Details/4
起作用https://localhost:xxxxx/Posts/Details/4
。
Index.cshtml Index.cshtml
@model IEnumerable<Website.Models.Post>
<table class="table">
<thead>
<tr>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["TitleSortParm"]">@Html.DisplayNameFor(model => model.Title)</a>
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["AuthorSortParm"]">@Html.DisplayNameFor(model => model.Author.LastName)</a>
</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.ActionLink(item.Title, "Details", "Posts", new { item.PostId })
</td>
<td>
@Html.ActionLink(item.Author.FullName, "Details", "Authors", new { item.Author.AuthorId })
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.PostId">Edit</a> |
<a asp-action="Details" asp-route-id="@item.PostId">Details</a> |
<a asp-action="Delete" asp-route-id="@item.PostId">Delete</a>
</td>
</tr>
}
</tbody>
</table>
PostsController.cs (relevant Details
get method only) PostsController.cs (仅相关Details
get方法)
namespace Website.Controllers
{
public class PostsController : Controller
{
private readonly Website.Data.ApplicationDbContext _context;
public PostsController(Website.Data.ApplicationDbContext context)
{
_context = context;
}
// GET: Post/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var post = await _context.Post
.Include(p => p.Author)
.FirstOrDefaultAsync(m => m.PostId == id);
if (post == null)
{
return NotFound();
}
return View(post);
}
}
}
Post.cs Post.cs
namespace Website.Models
{
public class Post // dependent on Author
{
public int PostId { get; set; } // primary key
public string Title { get; set; }
public string Description { get; set; }
public DateTime PublicationDate { get; set; }
public int AuthorId { get; set; } // author foreign key
public Author Author { get; set; } // author navigation property
public ICollection<Tag> Tags { get; set; }
}
}
In Startup.cs, routing info (never changed this, just default): 在Startup.cs中,路由信息(从未更改,只是默认设置):
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Your action parameter named id
. 您的动作参数名为id
。 When you use the point with named parameters like this https://localhost:xxxxx/Posts/Details?PostId=4
MVC tries to serialize the PostId
to the parameter with the same name. 当您将点与命名参数一起使用时,例如https://localhost:xxxxx/Posts/Details?PostId=4
MVC会尝试将PostId
序列化为具有相同名称的参数。 You can rename your parameter or use prefix. 您可以重命名参数或使用前缀。
Task<IActionResult> Details([Bind(Prefix="PostId")] int? id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.