【问题标题】:Razor page <A> Label takes ViewContext.Routing.Value id instead of given IDRazor 页面 <A> 标签采用 ViewContext.Routing.Value id 而不是给定的 ID
【发布时间】:2019-09-05 14:15:14
【问题描述】:

正如我的标题已经解释的那样 我正在为一个学校项目构建一个小型 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>

【问题讨论】:

    标签: c# asp.net asp.net-core-mvc razor-pages


    【解决方案1】:

    问题是你的路由参数名称和动作参数名称不一致。你的路线中有{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 将为空。

    【讨论】:

    • 好吧,我试过了。它现在这样做了,遗憾的是它再也找不到 /profile/ 的剃须刀页面,因为它会寻找以我发送的 GUID 命名的页面跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 2014-09-02
    • 2021-04-08
    • 1970-01-01
    相关资源
    最近更新 更多