Route mapping does not work in ASP.NET Core 2.2

Asked

Viewed 438 times

2

I have always used ASP.NET MVC 4 and now I am learning ASP.NET Core in version 2.2, I am doing some tests and I noticed that the routing is not working.

I am using the standard design that is already configured.

inserir a descrição da imagem aqui

Within Startup.cs configure the route as follows:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseMvc(routes =>
        {
            routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
        });
 }

Then insert the following on the page index.cshtml:

@Html.ActionLink("Link para a Action About no mesmo controlador", "About")

But when I see the result in View Index.cshml is returned to me:

inserir a descrição da imagem aqui

I’m doing something wrong?

  • 1

    Are you using "normal" ASP.NET with . NET Core? O.o

  • ASP.NET Core 2.2

  • In Asp.net core you have a more "clean" way of creating links <a Asp-action="About">Link to Action About in the same controller</a> .

  • Another important thing on. net core (which was already in MVC 4) is to respect conventions (code rules), the compiler/interpreter respects those rules when interpreting your code, I mention this because in the structure of your project some of the folders of the MVC convention are missing... for Actionlink the controller and the action must exist and respect the convention to be recognized as such. Only then when generating the link the . net core will know the correct route to generate the <a href=...

2 answers

0


The project you are working on is not from the template MVC, the route you say does not work because there are no controllers or views in that structure. You started your project by choosing the Web Application option, also known as Razor Pages which has by default initial route to page index.

Adicionando novo projeto

Estrutura RazorPages

Taking as an example the page About, to where you wanted to create the ActionLink, note that it is composed of a Razor template (.cshmtl) with the bind to a class of PageModel (cshtml.Cs), which will control your actions in the form of events... as if they were the actions of a controller.

About.cshml

@page
@model AboutModel
@{
    ViewData["Title"] = "About";
}
<h2>@ViewData["Title"]</h2>
<h3>@Model.Message</h3>

<p>Use this area to provide additional information.</p>

About.cshtml.Cs

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebApplication.Razor.Pages
{
    public class AboutModel : PageModel
    {
        public string Message { get; set; }

        public void OnGet()
        {
            Message = "Your application description page.";
        }
    }
}

And in this structure, for you to add a link in the Index to about, you just add in the Index.cshml the following markup:

<a asp-page="/About">About</a>

However, if you want to continue working with MVC in Asp.net core, just create your project using the templante "Web Application (Model-View-Controller)".

Criando projeto MVC

inserir a descrição da imagem aqui

The structure is almost the same as the one you’re used to in MVC4, but implementation the available resources are somewhat different. And follows below recommended way of creating your ActionLink in that version.

<a asp-area="" asp-controller="Home" asp-action="About">About</a>

0

The Controller and the Views are missing, I suggest you create another project. And then you configure the controller to render the pages you want

Browser other questions tagged

You are not signed in. Login or sign up in order to post.