如何在.net核心中定义多个区域的路由

问题描述 投票:0回答:1

最初我只有一个区域,我希望它作为默认路由,所以我配置它像:

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

它工作正常。现在我想要包含另一个区域“Order”并配置路线如:

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

并且在订单区域的家庭控制器中:

[Area("Order")]
public class HomeController : Controller
{

现在,当我击中https://localhost:44632/order时,我找不到404,但https://localhost:44632/product工作正常。我还尝试在默认路由之前配置orderRoute但仍然得到相同的结果。我究竟做错了什么?

c# asp.net-core asp.net-core-2.0
1个回答
0
投票
It looks correct. Just needs a change. You need to add default route in the end not as the first route. just interchange and it should work.


With .net core, following is needed to be added in the startup file if you are adding an area:

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

After that you can just simply mark your area and route in the controller, i.e
     [Area("Order")]
     [Route("order")]
© www.soinside.com 2019 - 2024. All rights reserved.