简体   繁体   English

区域路线映射问题

[英]areas routes mapping issue

I'm working with areas controllers as well as simple default controller. I want to make areas controller (Public) as a default route ie public/home/home but when I go to simple controller ie account/login it returns wrong url which is public/account/login instead of home/index.我正在使用区域控制器以及简单的默认值 controller。我想将区域 controller(公共)设置为默认路由,即公共/家庭/家庭,但是当我将 go 转换为简单的 controller,即帐户/登录时,它返回错误的 url,这是public/account/login 而不是 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?}"
                );

Asp.net core will not show the full route in default route, It will just show defualt route like https://localhost:xxxxx . Asp.net 核心不会在默认路由中显示完整路由,它只会显示默认路由,如https://localhost:xxxxx If you want show the full route, you need to use url Rewrite .如果你想显示完整的路线,你需要使用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那么默认的url就是https://localhost:xxxxx/Service/Home/Privacy

The other method is add launchUrl in your lunchSettings.json .另一种方法是在lunchSettings.json launchUrl

Fisrt you need to add [Route("service/Home/Privacy")] in privacy action, then add "launchUrl": "https://localhost:xxxxx/service/home/privacy" in lunchSettings.json .首先你需要在隐私操作中添加[Route("service/Home/Privacy")] ,然后在lunchSettings.json中添加"launchUrl": "https://localhost:xxxxx/service/home/privacy"

When you run the project, The defualt url is also https://localhost:xxxxx/Service/Home/Privacy运行项目时,默认的 url 也是https://localhost:xxxxx/Service/Home/Privacy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM