如何定位和调用控制器操作?

时间:2012-06-20 21:25:19

标签: asp.net-mvc asp.net-mvc-routing

我今天刚开始学习MVC3,所以请原谅任何愚蠢(特别是名称不好的名字)。

此网址不起作用:

.../Test

但是这个确实:

.../Index

这是我的代码:

public class MyController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

public static class MyExt
{
    public static ActionResult Test(this MyController test)
    {
        return test.Index();
    }
}

3 个答案:

答案 0 :(得分:3)

扩展方法不起作用,因为ControllerActionInvoker只调用控制器的方法。您可以阅读有关ASP.NET MVC管道here的更多信息。

答案 1 :(得分:0)

我不确定MVC管道是否能够“看到”扩展方法Test,以便使用默认路由表自动地映射它。 MVC使用基于约定的方法。它认为所有控制器都在特定的位置等。可能它不会考虑寻找扩展方法。

答案 2 :(得分:0)

要获得相同的结果,最好在global.asax

中设置新路线
routes.MapRoute(
    "Test",
    "Home/Test",
    new { controller = "Home", action = "Index" });

如果您想为所有控制器提供相同的

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

很抱歉,但我仍然无法弄清楚你要在这里实现的目标

查看以下链接以获取有关路由的概述: http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs

相关问题