asp.net-mvc 子文件夹中的控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17178688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:57:19  来源:igfitidea点击:

Controller in Sub Folder

asp.net-mvcasp.net-mvc-3

提问by Imad Alazani

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

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

enter image description here

在此处输入图片说明

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

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

localhost:2474/SOProblems/ChildController/index 


This does not works when the url is like this

当 url 是这样时,这不起作用

localhost:2474/SOProblems/SubFolder/ChildController/index

Can you please tell me what is missing?

你能告诉我缺少什么吗?

采纳答案by Darin Dimitrov

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

当 url 像这样 localhost:2474/SOProblems/SubFolder/ChildController/index 时,这不起作用

That's normal. You route pattern looks like this: SubFolder/ChildControllerand 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.csfile (which is where your SOProblemsroutes should be registered):

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

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.

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

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:

如果你想在这个控制器上有更多的动作,但 index 是默认的,你应该在你的路由模式中包含它:

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

回答by Matt Houser

Your new route "SubFolder" does not include the possibility of including an action in the route (in your case, "Index").

您的新路线“子文件夹”不包括在路线中包含操作的可能性(在您的情况下为“索引”)。

Your sample URL

您的示例网址

localhost:2474/SOProblems/SubFolder/ChildController/index

Wants to try to match a route like:

想尝试匹配如下路线:

"SubFolder/ChildController/{action}"

But you don't include the "{action}" in your route, so it won't match your route. It then tries the default route, which obviously fails.

但是您没有在路线中包含“{action}”,因此它与您的路线不匹配。然后它尝试默认路由,这显然失败了。

Try adding "{action}" to your route:

尝试将“{action}”添加到您的路线中:

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

or take "index" off your test URL.

或从您的测试 URL 中删除“索引”。

回答by Belmiris

For any future users looking to do this; Look into using Areas. Here's a helpful video. Organizing an application using Areas

对于任何希望这样做的未来用户;研究使用区域。这是一个有用的视频。 使用区域组织应用程序