Home > Enterprise >  Why is this ActionLink to another area not working?
Why is this ActionLink to another area not working?

Time:06-17

I am testing how to build an action link to a page in another area, and every source I find says to use

Html.ActionLink("[Link Text]", "[action name]", "[controller]", new { area = "[areaName]" }, null)

except for one source I found which suggested

Html.ActionLink("[Link Text]", "[action name]",new { area = "[areaName]", controller = "[controllerName]" })

The problem is, neither of these work for me. In my MVC application I have an area called "Uploader", which contains its own Home controller, and Index page. So, in the main Index page of my MVC project, I create an ActionLink that looks like this:

@Html.ActionLink("Area Test", "Index", "Home", new { area = "Uploader" }, null)

If the link works correctly, I'll get taken to a page that reads "this is the uploader", the text on my area Index page, but instead, the main home/index page of my application just reloads.

The address I see in my browser after this reload is "https://localhost:44352/?area=Uploader".

The HomeController for the area is correctly formatted with an "Area" tag, like so:

namespace TestProject.Areas.Uploader.Controllers
{
    [Area("Uploader")]
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

I'm using .Net 5. What am I doing wrong?

CodePudding user response:

Finally found the answer here. It turns out I had my area configuration backwards: the "default" route should come last.

It should be:

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

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

...instead of the other way around.

  • Related