路由控制器,它在一个子文件夹 [英] Route controller that is in a sub folder

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

问题描述

我有我的里面控制器文件夹中的子文件夹中的MVC3项目。现在我想创建一个路由到一个控制器,是子文件夹内。但我怎么能做到这一点?

I have an MVC3 project with a "sub" folder inside my Controllers folder. Now i want to create a Route to a controller that is inside that subfolder. But how can i do that?

这似乎并没有为我工作:

This doesn't seem to work for me:

context.MapRoute("Test", "SubFolder/Test",
            new { Controller = "SubFolder/Test", Action = "Index" });

所以,子文件夹的名称是子文件夹,并在那里我有一个名为控制器 TestController.cs 。如何创建一个图路线是什么?

So the name of the subfolder is SubFolder and in there i have a controller called TestController.cs. How can i create a MapRoute for that?

推荐答案

有没有这样的概念作为子文件夹控制器。控制器是哪个,无论你想,你也可以只把C#类。在您的路由配置,你应该只提控制器名称:

There's no such notion as subfolder for controllers. Controllers are just C# classes which you could store wherever you want. In your route configuration you should only mention the controller name:

context.MapRoute(
    "Test", 
    "SubFolder/Test",
    new { controller = "Test", action = "Index" }
);

如果你想有2个控制器具有相同的名称,则需要指定命名空间约束定义路由时:

and if you wanted to have 2 controllers with the same name, you need to specify the namespace constraint when defining the route:

context.MapRoute(
    "Test", 
    "SubFolder/Test",
    new { controller = "Test", action = "Index" },
    new[] { "MvcApplication.Controllers.SubFolder" }
);

所以,现在当你浏览到 http://example.com/subfolder/test 的的的TestController指数动作将被执行。

So now when you navigate to http://example.com/subfolder/test, the Index action of the TestController will be executed.

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

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