Search code examples
visual-studiorazorengine

How to redirect link to the controller an avoid the last visited link in Visual Studio MVC project?


I added a controller named Welcome to the sample project created by Visual Studio. This sample project comes with Home and Home/Privacy links. I modified the file _Layout.cshtml to add a link to the view Welcome/Index.cshtml as follows:

<ul class="navbar-nav flex-grow-1">
    <li class="nav-item">
        <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
    </li>
    <li class="nav-item">
        <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
    </li>
    <li class="nav-item">
        <a class="nav-link text-dark" asp-area="" asp-controller="Welcome">Welcome</a>
    </li>
</ul>

When I execute the project and I begin to navigate in the following order over the links:

  1. Home
  2. Welcome (fine)
  3. Home/Privacy
  4. Welcome/Privacy (wrong)

The problem is in the 4. step, the /Privacy remains instead of left /Welcome alone.

How can I solve this?


Solution

  • You should also define an asp-action for your link:

    <li class="nav-item">
        <a class="nav-link text-dark" asp-area="" asp-controller="Welcome" asp-action="Index">Welcome</a>
    </li>
    

    as seen in the first link (asp-controller="Home" asp-action="Index").