更改主键后的路由问题

时间:2019-07-13 21:57:45

标签: c# asp.net asp.net-mvc asp.net-core routing

将主键从================================================================= Native Crash Reporting Got a SIGABRT while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries used by your application. ================================================================= Basic Fault Adddress Reporting Memory around native instruction pointer (0x1bfd920dc):0x1bfd920cc fd 7b c1 a8 c0 03 5f d6 10 29 80 d2 01 10 00 d4 .{......)...... 0x1bfd920dc c3 00 00 54 fd 7b bf a9 fd 03 00 91 55 d6 ff 97 ...T.{......U... 0x1bfd920ec bf 03 00 91 fd 7b c1 a8 c0 03 5f d6 90 29 80 d2 .....{......).. 0x1bfd920fc 01 10 00 d4 c3 00 00 54 fd 7b bf a9 fd 03 00 91 .......T.{...... ================================================================= 更改为ID后,看来PostId Post视图无法正确呈现URL。

如果我导航到Details.cshtml,则页面将正确呈现。但是,以前有效的https://localhost:xxxxx/Posts/Details/4视图中的@Html.ActionLink调用现在不再起作用。它指向Index.cshtml,这是一个空白页。 URL似乎应该起作用,但是,我不确定路由是问题还是与视图有关。单独的类也发生了同样的问题(它的主键也已更改)。

https://localhost:xxxxx/Posts/Details?PostId=4视图中的<a asp-action="Details" asp-route-id="@item.PostId">Details</a>链接通过指向Index.cshtml来起作用。

Index.cshtml

https://localhost:xxxxx/Posts/Details/4

PostsController.cs (仅限相关的@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> get方法)

Details

Post.cs

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);
        }
    }
}

在Startup.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; }
    }
}

1 个答案:

答案 0 :(得分:1)

您的动作参数名为id。当您将点与诸如https://localhost:xxxxx/Posts/Details?PostId=4这样的命名参数一起使用时,MVC会尝试将PostId序列化为具有相同名称的参数。您可以重命名参数或使用前缀。

Task<IActionResult> Details([Bind(Prefix="PostId")] int? id)