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

时间:2019-02-12 06:12:53

标签: c# asp.net-core asp.net-core-2.0

最初我只有一个区域,我希望将其作为默认路线,所以我将其配置为:

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

,它工作正常。现在,我想包括另一个“订单”区域,并将路由配置为:

 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,但仍然得到相同的结果。我在做什么错了?

1 个答案:

答案 0 :(得分: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")]