繁体   English   中英

更改主键后的路由问题

[英]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似乎应该起作用,但是,我不确定路由是问题还是与视图有关。 单独的类(也更改了其主键)时,也会发生相同的问题。

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

@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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM