更改主键后的路由问题
Routing issue after changing primary keys
将主键从 ID
更改为 PostId
后,Post
Details.cshtml
视图似乎无法正确呈现 URL。
如果我导航到 https://localhost:xxxxx/Posts/Details/4
,页面会正确呈现。但是,Index.cshtml
视图中的 @Html.ActionLink
调用以前有效,现在不再有效。它指向 https://localhost:xxxxx/Posts/Details?PostId=4
,这是一个空白页。不过,似乎 URL 应该可以工作,所以我不确定路由是问题还是与视图有关。单独的 class(也更改了主键)也出现了同样的问题。
Index.cshtml
视图中的 <a asp-action="Details" asp-route-id="@item.PostId">Details</a>
link 通过指向 https://localhost:xxxxx/Posts/Details/4
来工作。
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(仅相关 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
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; }
}
}
在Startup.cs中,路由信息(从未更改过,只是默认):
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
您的操作参数名为 id
。当您使用带有命名参数的点时,如 https://localhost:xxxxx/Posts/Details?PostId=4
MVC 尝试将 PostId
序列化为具有相同名称的参数。您可以重命名参数或使用前缀。
Task<IActionResult> Details([Bind(Prefix="PostId")] int? id)
将主键从 ID
更改为 PostId
后,Post
Details.cshtml
视图似乎无法正确呈现 URL。
如果我导航到 https://localhost:xxxxx/Posts/Details/4
,页面会正确呈现。但是,Index.cshtml
视图中的 @Html.ActionLink
调用以前有效,现在不再有效。它指向 https://localhost:xxxxx/Posts/Details?PostId=4
,这是一个空白页。不过,似乎 URL 应该可以工作,所以我不确定路由是问题还是与视图有关。单独的 class(也更改了主键)也出现了同样的问题。
Index.cshtml
视图中的 <a asp-action="Details" asp-route-id="@item.PostId">Details</a>
link 通过指向 https://localhost:xxxxx/Posts/Details/4
来工作。
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(仅相关 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
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; }
}
}
在Startup.cs中,路由信息(从未更改过,只是默认):
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
您的操作参数名为 id
。当您使用带有命名参数的点时,如 https://localhost:xxxxx/Posts/Details?PostId=4
MVC 尝试将 PostId
序列化为具有相同名称的参数。您可以重命名参数或使用前缀。
Task<IActionResult> Details([Bind(Prefix="PostId")] int? id)