Razor 页面<A>标签采用 ViewContext.Routing.Value ID 而不是给定的 ID



正如我的标题已经解释的那样我正在为一个学校项目构建一个小的 Twitter 克隆,而如果我点击海报名称,我想转到某人的个人资料页面。

<a asp-area="" asp-controller="Home" asp-action="Profile">@userController.GetUser(post.UserId).DisplayName</a><a asp-area="" asp-controller="Home" asp-action="Profile">@userController.GetUser(post.UserId).DisplayName</a>

这只能让我到目前为止,我可以通过 url 获取登录的用户名 ID 并查看它自己的个人资料。 很好,但我也需要能够访问其他配置文件

我也试过这个

<a asp-area="" asp-controller="Home" asp-action="Profile" asp-route-userId="@post.UserId">@userController.GetUser(post.UserId).DisplayName</a>

但是每次我这样做时,我的 url 路由都关闭了,它会是这样的:

Profile/3c126298-32f8-471e-8597-69c71c333df8?userId=f5c01788-97d9-4426-881a-53469e8c44f8

当然,它立即出错了。

有谁知道我如何解决这个问题,因为我尝试在 Interwebz 上搜索它,但我不知道如何提出这个问题,所以我没有得到任何结果

控制器代码:

        [Route("Profile/{id}")]
        public IActionResult Profile(string userId)
        {
            return View(userId);
        }

剃刀页面

@using SpackkApiMVC.Controllers
@model SpackkApiMVC.Models.PostModel
<style>
    .scrollable {
      background-color: rgba(255, 230, 204, 0.2);
    }
    .container {
      margin: 0 auto;
      width: 70%;
    }
</style>
<div class="container">
<div class="row">
      <div class="col-xs-12 col-sm-9">
        <!-- User profile -->
        <div class="panel panel-default">
          <div class="panel-body">
            <div class="profile__avatar">
              <img src="https://bootdey.com/img/Content/avatar/avatar5.png" alt="...">
            </div>
            <div class="profile__header">
              <h4>@user.DisplayName <small>Administrator</small></h4>
            </div>
          </div>
        </div>
        <!-- User info -->
        <div class="panel panel-default">
          <div class="panel-heading">
          <h4 class="panel-title">User info</h4>
          </div>
          <div class="panel-body">
            <table class="table profile__table">
              <tbody>
                <tr>
                  <th><strong>Location</strong></th>
                  <td>United States</td>
                </tr>
                <tr>
                  <th><strong>Company name</strong></th>
                  <td>Simpleqode.com</td>
                </tr>
                <tr>
                  <th><strong>Position</strong></th>
                  <td>Front-end developer</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
        <!-- Community -->
        <div class="panel panel-default">
          <div class="panel-heading">
          <h4 class="panel-title">Community</h4>
          </div>
          <div class="panel-body">
            <table class="table profile__table">
              <tbody>
                <tr>
                  <th><strong>Comments</strong></th>
                  <td>58584</td>
                </tr>
                <tr>
                  <th><strong>Member since</strong></th>
                  <td>Jan 01, 2016</td>
                </tr>
                <tr>
                  <th><strong>Last login</strong></th>
                  <td>1 day ago</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
        <!-- Latest posts -->
        <div class="panel panel-default">
          <div class="panel-heading">
            <h4 class="panel-title">Latest posts</h4>
          </div>
          @foreach (PostModel post in postController.GetPosts(user.Id))
          {
            <div class="scrollable">
              <h5 class="card-title"><a asp-area="" asp-controller="Home" asp-action="Profile" >@userController.GetUser(post.UserId).DisplayName</a></h5>
              <div class="card-body">
                <p class="card-text" name="post.id">
                  @post.Body <br/><br/>
                  <img src="../images/upvote.png" height="15" width="15"/> &nbsp;@postController.GetLikeCount(post.Id)
                  @if (postController.IsLiked(user.Id, post.Id))
                  {
                    <a asp-area="" asp-controller="Post" asp-action="UnLike" asp-route-userId="@user.Id" asp-route-postId="@post.Id"class="btn-link" >UnLike</a>
                  }
                  else
                  {
                    <a asp-area="" asp-controller="Post" asp-action="Like" asp-route-userId="@user.Id" asp-route-postId="@post.Id" class="btn-link" >Like</a>
                  }
                  <br/>
                  <small class="text-muted">@post.Date.ToShortDateString() - @post.Date.ToShortTimeString()</small>
                  <hr>
                </p>
              </div>
            </div>
          }
        </div>
      </div>
      <div class="col-xs-12 col-sm-3">

      </div>
    </div>
</div>

问题是您的路由参数名称和操作参数名称不一致。您的路由中有{id},但随后userId在您的操作签名中。它们应该被命名为相同。从技术上讲,如果您指定了 asp-route-id 而不是 asp-route-userId,则路由是正确的:

<a asp-area="" asp-controller="Home" asp-action="Profile" asp-route-id="@post.UserId">@userController.GetUser(post.UserId).DisplayName</a>

然后,这将为您提供一个URL,例如:

Profile/f5c01788-97d9-4426-881a-53469e8c44f8

但是,由于参数名称不对齐,因此userId将为空。

最新更新