具有区域的 ASP.NET Core 2 默认路由 [英] ASP.NET Core 2 default route having areas

查看:24
本文介绍了具有区域的 ASP.NET Core 2 默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c我浏览了有关区域路由的各种帖子,但仍然无法解决我的问题.

cI went through various posts regarding the Areas routing but still I am not able to resolve my issue.

我想以有两条路线的方式拆分我的应用程序.

I would like to split my application in the way that there are two routes.

  • /区域/面板
  • /区域/网站

在每个区域内有 HomeController 和对应于动作的适当方法.

Inside each area there is HomeController and appropriate methods that correspond to actions.

我希望每当用户降落到任何 1 级路线时,即:

I would like that whenever user lands to any level 1 route i.e.:

  • /home
  • /联系方式
  • /关于/公司

指向/Areas/Website/{controller}/{action}

或者

  • /面板
  • /panel/home
  • /panel/users/2

/Areas/Panel/{controller}/{action}

我目前的MVC路线是:

My current MVC route is:

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

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

但它不起作用,可能是因为我不完全理解如何告诉路由器使用 Website 区域作为默认值.我在控制器本身之前尝试了各种指令,但没有奏效.

but it isn't working, probably because I don't fully understand how tell the router to use Website area as default. I tried various directives right before the controller itself but it did not work.

我可以征求你的意见吗?

Could I ask for your advice?

提前致谢.

推荐答案

它不起作用的一个原因是您以错误的顺序注册了路由.路由从路由表的顶部到底部进行评估,第一个匹配获胜.

One reason it doesn't work because you have the routes registered in the wrong order. The routes are evaluated from the top to the bottom of the route table and the first match wins.

另一个问题是,如果您希望将请求定向到 WebsiteMapAreaRoute 扩展方法)> 区域.

Another issue is that you need to make the "default" route into an area route (using the MapAreaRoute extension method) if you want it to direct requests to the Website area.

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

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

参考:为什么在asp.net mvc中先映射特殊路由,再映射普通路由?

这篇关于具有区域的 ASP.NET Core 2 默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆