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

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 子文件夹中的控制器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。



我的区域在下面.只有有关部分被突出显示.

路线表

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

你能告诉我什么是失踪的吗?

解决方法

@H_403_26@

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

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

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

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

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

如果你想在这个控制器上有更多的动作,而索引是默认的,你应该把它放在你的路由模式中:

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

在这两种情况下,您的主路由定义可以保留其默认值:

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

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

猜你在找的asp.Net相关文章