控制器在子文件 [英] Controller in Sub Folder

查看:124
本文介绍了控制器在子文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我区下方。只有关心的部分突出显示。

路由表

 公共静态无效的RegisterRoutes(RouteCollection路线)
{
    routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);    routes.MapRoute(
        子文件夹,//路线名称
        子文件夹/ ChildController
        新{控制器=ChildController,行动=索引},
        新的[] {Practise.Areas.SOProblems.Controllers.SubFolder});
    routes.MapRoute(
        默认,//路线名称
        {控制器} / {行动},// URL带参数
        新{控制器=家,行动=索引} //参数默认
    );
}


 这仅适用于当URL是这样的
本地主机:2474 / SOProblems / ChildController /指数


 这不工作当URL是这样的
本地主机:2474 / SOProblems /子文件夹/ ChildController /指数

能否请你告诉我什么是缺少什么?


解决方案

  

这不工作当URL是这样的
  本地主机:2474 / SOProblems /子文件夹/ ChildController /指数


这是正常的。你的路由模式是这样的:子文件夹/ ChildController ,而不是子文件夹/ ChildController /指数。另外,你在错误的地方定义您的路线。你在你的主路由定义,而不是在您的区域路线定义来定义它。因此,摆脱自定义路线从您的主要道路,​​并把它添加到 SOProblemsAreaRegistration.cs 文件(这是你的 SOProblems 路线应进行登记):

 公共覆盖无效RegisterArea(AreaRegistrationContext上下文)
{
    context.MapRoute(
        SubFolderRoute
        SOProblems /子文件夹/ ChildController
        新{控制器=ChildController,行动=索引},
        新的[] {Practise.Areas.SOProblems.Controllers.SubFolder}
    );    context.MapRoute(
        SOProblems_default
        SOProblems / {控制器} / {行动} / {ID}
        新{行动=索引,ID = UrlParameter.Optional}
    );
}

此外,由于你的路线图( SOProblems /子文件夹/ ChildController )没有指定一个动作名称的可能性,你只能拥有这件控制器上一个动作和这将是(首页),在这种情况下,你注册的默认操作。

如果你想有此控制器上更多的行动,但指数是默认的,你应该包括在你的路由模式:

  context.MapRoute(
    子文件夹,
    SOProblems /子文件夹/ ChildController / {}行动,
    新{控制器=ChildController,行动=索引},
    新的[] {Practise.Areas.SOProblems.Controllers.SubFolder}
);

在这两种情况下,你的主要途径定义可以保持与它们的默认值:

 公共静态无效的RegisterRoutes(RouteCollection路线)
{
    routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);    routes.MapRoute(
        默认,
        {控制器} / {行动},
        新{控制器=家,行动=索引}
    );
}

My area is below. Only the concerned part is highlighted.

Route Table

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "SubFolder", // Route name
        "SubFolder/ChildController",
        new { controller = "ChildController", action = "Index" },
        new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" });


    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}", // URL with parameters
        new { controller = "Home", action = "Index" } // Parameter defaults
    );
}


This only works when the url is like this 
localhost:2474/SOProblems/ChildController/index 


This does not works when the url is like this 
localhost:2474/SOProblems/SubFolder/ChildController/index

Can you please tell me what is missing?

解决方案

This does not works when the url is like this localhost:2474/SOProblems/SubFolder/ChildController/index

That's normal. You route pattern looks like this: SubFolder/ChildController and not SubFolder/ChildController/index. In addition to that you defined your route in the WRONG place. You defined it in your main route definitions and not in your area route definitions. So get rid of the custom route definition from your main routes and add it to the SOProblemsAreaRegistration.cs file (which is where your SOProblems routes should be registered):

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "SubFolderRoute", 
        "SOProblems/SubFolder/ChildController",
        new { controller = "ChildController", action = "Index" },
        new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" }
    );

    context.MapRoute(
        "SOProblems_default",
        "SOProblems/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}

Also since your route pattern (SOProblems/SubFolder/ChildController) doesn't have the possibility to specify an action name, you can only have one action on this controller and that would be the default action that you registered (index) in this case.

If you wanted to have more actions on this controller and yet index be the default one you should include that in your route pattern:

context.MapRoute(
    "SubFolder", 
    "SOProblems/SubFolder/ChildController/{action}",
    new { controller = "ChildController", action = "Index" },
    new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" }
);

In both cases your main route definition could remain with their default values:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}",
        new { controller = "Home", action = "Index" }
    );
}

这篇关于控制器在子文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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