I cannot perform a Redirecttoaction for an Area

Asked

Viewed 124 times

-1

Print of what I’ve accomplished, but it didn’t work, this is my Logincontroller. inserir a descrição da imagem aqui

My structure, and I’m trying to point to the Patientecontroller/Index:

inserir a descrição da imagem aqui

One of the other options I tried was to add routes to startups.Cs.

app.UseMvc(routes =>
            {
                routes.MapRoute(
                  name: "areas",
                  template: "{area:exists}/{controller=Paciente}/{action=Index}/{id?}"
                );
            });
  • Testing RedirectToAction("Index", "Paciente", new { area = "Paciente" })

  • 1

    Note that ASP.NET MVC is not the same thing as ASP.NET Core. Another thing: always post the code in formatted text, not in images. This helps in visualization, indexing the question and does not prevent those who have blocks to understand the question.

  • 1

    Prefer to post code in text instead of image. The reason is here.

  • @Georgewurthmann did not work, no:C

  • You can add the area settings mapped to the startup routes?

  • I have tried, used this code: app.Usemvc(Routes => { Routes.Maproute( name: "areas", template: "{area:exists}/{controller=Patient}/action=Index}/{id? }" ); });

Show 1 more comment

1 answer

1


I’ll show you what I did to work, check on your project:

First add route in startup:

  app.UseMvc(routes =>
        {

            routes.MapRoute(
                    name: "MinhasAreas",
                    template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");


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

Assign controllers within their respective areas:

[Area("Paciente")]
public class PacienteController : Controller
{
    // GET: /<controller>/
    public IActionResult Index()
    {
        return View();
    }
}

Perform the redirect normally:

public class LoginController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    public IActionResult GravarCadastro(string perfil) {
        return RedirectToAction("Index", perfil, new { area = perfil });
    }
}

I added the project on github for consultation, just click login and choose the profile to be redirected:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Project link:

https://github.com/superrfm/aspnetcore_areas

  • Very good Rafael

Browser other questions tagged

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