0

I'm working with areas controllers as well as simple default controller. I want to make areas controller (Public) as a default route i.e public/home/home but when I go to simple controller i.e account/login it returns wrong url which is public/account/login instead of home/index.

endpoints.MapAreaControllerRoute
                (
                    name: "default",
                    areaName: "Public",
                    pattern: "{controller=Home}/{action=Home}/{id?}"
                );
                endpoints.MapControllerRoute
                (
                    name: "withOutArea",
                    pattern: "{controller=Home}/{action=Index}"
                );
                endpoints.MapControllerRoute
                (
                    name: "area",
                    pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
                );
Serge
  • 40,935
  • 4
  • 18
  • 45
  • what's `when I go to simple controller i.e account/login it returns wrong url which is public/account/login instead of home/index.` mean? When you go to account/login, the url is `account/login`, why it will be `home/index`? You need to describe your problem more clearly. – Xinran Shen Feb 28 '22 at 05:51
  • How to make a default route by using area. I want to make /Public/Home/Index make as default route. – Waqas Akhtar Feb 28 '22 at 19:08

1 Answers1

1

Asp.net core will not show the full route in default route, It will just show defualt route like https://localhost:xxxxx. If you want show the full route, you need to use url Rewrite. I write a simple demo here:

//url rewrite
var rules = new RewriteOptions()
        .AddRedirect(@"^.{0}$", "Service/Home/Privacy");
app.UseRewriter(rules);

//..........

app.UseEndpoints(endpoints =>
{

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

    //set the default route 
    endpoints.MapControllerRoute
    (    
        name: "default",
        pattern: "{area=Service}/{controller=Home}/{action=privacy}/{id?}"
    );

});
//......

Then the default url is https://localhost:xxxxx/Service/Home/Privacy

The other method is add launchUrl in your lunchSettings.json.

Fisrt you need to add [Route("service/Home/Privacy")] in privacy action, then add "launchUrl": "https://localhost:xxxxx/service/home/privacy" in lunchSettings.json.

When you run the project, The defualt url is also https://localhost:xxxxx/Service/Home/Privacy

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12