2

I have created an ASP.NET Core 5 MVC project and I tried to add an identity area to my project to handle all authentication and authorization tasks in it. I also registered the area in Startup.cs and I am able to route to register action using /identity/action/register.

But on the Register page when I want to change the target controller in identity area using code below:

<form asp-area="Identity" asp-controller="Account" asp-action="Register" 
      asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" 
      class="form-horizontal" role="form">

then what I get in my rendered HTML is this:

<form method="post" class="form-horizontal" role="form" 
      action="/Account/Register?area=Identity" novalidate="novalidate">

The form doesn't target the identity area.

I've been looking for a solution all day, but none of what I found on the Web has worked for me.

Thanks

2 Answers 2

1

As far as I know, the form tag helper will check the route endpoints rule is exists or not when it rendered as html.

If you set the wrong endpoint routing, it couldn't render the right url as you excepted.

I suggest you could try to follow below steps to check if your application route and area folder structure is right.

1.You should make sure your area folder structure like this:

Project name
  Areas
    Identity
      Controllers
        HomeController.cs
        ManageController.cs
      Views
        Home
          Index.cshtml
        Manage
          Index.cshtml
          About.cshtml

2.You should make sure you have add the right area route in the app.UseEndpoints method.

        app.UseEndpoints(endpoints =>
        {


            endpoints.MapControllerRoute(
                  name: "areas",
                  pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
                );

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


        });
Sign up to request clarification or add additional context in comments.

1 Comment

The fix for me was to order the "areas" route above the "default" route.
0

In addition to Brando Zhang's answer please also make sure that you specify area attribute to your area controllers like below

[Area("Identity")]
public class AccountController : Controller
{
   //Your code here
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.