asp.net-mvcasp.net-core-mvc

(ERROR 404) Can't find this page using @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers


I was migrating my project into MVC from razor pages, my links were just asp-page="/Privacidade and they worked perfectly, but now, I was implementing the @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers and in the _layout I'm using asp-controller="Home" asp-action="Privacidade"

When i try to click a link in the navbar, it shows me a 404 ERROR can't find this page

This is my _layout code (Main problem is in the navbar):

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <!--Compativel com Internet explorer com esta função-->
        <meta http-equiv="X-UA-Compatible" content="IE=11" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>@ViewData["Title"] - hdsportal</title>
        <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
        <!-- Folha CSS -->
        <link rel="stylesheet" href="~/css/site.css" />
        <!-- Folha Bootstrap -->
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />

    </head>
    <body style="background: linear-gradient(90deg, rgba(50,93,149,1) 0%, rgba(14,47,89,1) 100%); padding-bottom: 75px ">
        <header>
            <!-- Barra de navegação -->
            <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-light">
                <a class="navbar-brand border border-black p-0 ml-2" href="/">
                    <img class="img1" style="width: 178px; height: 47px;" src="/img/hds-logo.png">
                </a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <!-- Logotipo do HDS -->
                <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                    <a class="navbar-brand p-0 ml-2" href="/">
                        <img class="img2" style="width: 122px;height: 64px;" src="/img/hds-logo2.png">
                    </a>
                    <!--Mais tarde subsituido pelo nome do utilizador e não o nome da sessão-->
                    <p class="nav navbar-text navbar-right p-3">Bem-Vindo, @User.Identity.Name!</p>
                    <a href="#myModal" class="trigger-btn" data-toggle="modal">Alterar Identidade?</a>
                    <!-- Links da barra de navegação -->
                    <ul class="navbar-nav flex-grow-1 rounded-0">
                        <li class="nav-item rounded-0">
                            <a class="nav-link text-dark rounded-0" asp-area="" asp-controller="Home" asp-action="Privacidade">Privacidade</a>
                        </li>
                        <li class="nav-item rounded-0">
                            <a class="nav-link text-dark rounded-0" asp-area="" asp-controller="Home" asp-action="QuemSomos">Quem Somos</a>
                        </li>
                        <li class="nav-item rounded-0">
                            <a class="nav-link text-dark rounded-0" asp-area="" asp-controller="Home" asp-action="Contatos">Contatos</a>
                        </li>
                        <li class="nav-item dropdown rounded-0">
                            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                Administração
                            </a>
                            <div class="dropdown-menu py-0 py-0 m-0 rounded-0" aria-labelledby="navbarDropdown">
                                <!-- Dropdown da parte da Administração (links) -->

                                <a class="dropdown-item rounded-0" asp-controller="Home" asp-action="Gestao_Alertas">Gestão de Alertas</a>
                                <div class="dropdown-divider m-0"></div>
                                <a class="dropdown-item rounded-0" asp-area="" asp-controller="Home" asp-action="Gestao_Utilizadores">Gestão de Utilizadores</a>
                                <div class="dropdown-divider m-0"></div>
                                <a class="dropdown-item rounded-0" href="#">Gestão de Inventário</a>
                                <div class="dropdown-divider m-0"></div>
                                <a class="dropdown-item rounded-0" href="#">Gestão de Inventário - Consulta</a>
                                <div class="dropdown-divider m-0"></div>
                                <a class="dropdown-item rounded-0" href="#">Reporting Services</a>
                            </div>
                        </li>
                    </ul>
                </div>
            </nav>
        </header>
        <!-- Pop-up do Login (modal) -->
        <div id="myModal" class="modal fade show" aria-modal="true">
            <div class="modal-dialog modal-login">
                <div class="modal-content">
                    <form action="/examples/actions/confirmation.php" method="post">
                        <div class="modal-header">
                            <h4 class="modal-title">Login</h4>
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        </div>
                        <div class="modal-body">
                            <!-- Utilizador -->
                            <div class="form-group">
                                <label>Username</label>
                                <input type="text" class="form-control" required="required">
                            </div>
                            <!-- Password -->
                            <div class="form-group">
                                <div class="clearfix">
                                    <label>Password</label>
                                    <a href="#" class="float-right text-muted"><small>Forgot?</small></a>
                                </div>

                                <input type="password" class="form-control" required="required">
                            </div>
                        </div>
                        <div class="modal-footer justify-content-between">
                            <label class="form-check-label"><input type="checkbox"> Remember me</label>
                            <input type="submit" class="btn btn-primary" value="Login">
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Body (código utilizado no INDEX) -->
        <main role="main">
            @RenderBody()
        </main>

        <!-- Barra rodapé -->
        <footer class="py-3 px-sm-3 mt-xl-5 fixed-bottom text-muted bg-light ">
            &copy; 2021 - hdsportal - <a asp-area="" asp-page="/Privacidade">Privacidade</a>
        </footer>

        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
        <script src="~/js/site.js" asp-append-version="true"></script>
        <script src="https://use.fontawesome.com/b6e7b019b4.js"></script>
        @RenderSection("Scripts", required: false)

    </body>
</html>

My Startup:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace hdsportal
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

This is my pages setup

Link I'm trying to get into

The error when i click the page


Solution

  • Just change your action to:

    public IActionResult Privacidade() 
    { 
      return View();
    }