从Route Data获取操作名称

时间:2012-04-13 05:39:34

标签: c# asp.net-mvc-2

我知道我是否想要获得当前可以使用的动作;

ControllerContext.RouteData.GetRequiredString("action")

但是,如果我的路线允许http://mydomain/this has spaces

,该怎么办?

我如何获得“This Has Spaces”而不是“ThisHasSpaces”?

以下是路线表

的摘录
        routes.MapRoute(
            "ThisHasSpaces", // Route name
            "This Has Spaces", // URL with parameters
            new { controller = "Home", action = "ThisHasSpaces", id = UrlParameter.Optional } // Parameter defaults
        );

1 个答案:

答案 0 :(得分:2)

如果您的路线看起来没有任何东西可以为您提供原始字符串(Request.Uri除外),因为您实际上并未将uri映射到路线参数。

由于您对路线进行了硬编码,因此可以添加其他值:

    routes.MapRoute(
        "ThisHasSpaces", // Route name
        "This Has Spaces", // URL with parameters
        new { controller = "Home", action = "ThisHasSpaces", orgstring = "This Has Spaces", id = UrlParameter.Optional } // Parameter defaults
    );

获取它:

ControllerContext.RouteData.Values["orgstring"]
相关问题