剃刀页<a> Label takes ViewContext.Routing.Value id instead of given ID

时间:2019-04-15 14:53:36

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

As my title already explains I am building a small Twitter Clone for a school project and while i want to go to someones profile page if i click on the poster name.

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

This only gets me so far that i can get the logged in username id via url and looking at it's own profile. well nice but i need to be able to get to other profiles aswell

I've also tried this

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

But everytime i do that my url routing is off it will be like this:

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

And it immediatly errors ofcourse.

Does anybody know how I can resolve this issue because i've tried searching for it on the Interwebz but i dont know how to fomulate the question so im not getting any results

Controller code:

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

Razor Page

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

1 个答案:

答案 0 :(得分:0)

问题是您的路线参数名称和操作参数名称不一致。您的路线中有{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>

这将为您提供一个网址,例如:

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

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