子文件夹中的控制器 [英] Controller in Sub Folder

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

问题描述

我的区域在下面.仅突出显示相关部分.

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

路由表

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
    );
}

<小时>

这只有在 url 是这样的时候才有效


This only works when the url is like this

localhost:2474/SOProblems/ChildController/index 

<小时>

当url是这样的时候这不起作用


This does not works when the url is like this

localhost:2474/SOProblems/SubFolder/ChildController/index

你能告诉我缺少什么吗?

Can you please tell me what is missing?

推荐答案

当url是这样的时候这不起作用本地主机:2474/SOProblems/SubFolder/ChildController/index

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

这很正常.您的路由模式如下所示:SubFolder/ChildController 而不是 SubFolder/ChildController/index.除此之外,您在错误的地方定义了路线.您在主路线定义中而不是在区域路线定义中定义了它.因此,从您的主要路由中删除自定义路由定义,并将其添加到 SOProblemsAreaRegistration.cs 文件(您的 SOProblems 路由应该在该文件中注册):

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 }
    );
}

此外,由于您的路由模式 (SOProblems/SubFolder/ChildController) 无法指定操作名称,因此您只能在此控制器上执行一个操作,这将是默认操作在这种情况下,您注册了 (index).

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天全站免登陆