gpt4 book ai didi

c# - 从路由数据中获取 Action 名称

转载 作者:太空宇宙 更新时间:2023-11-03 11:23:48 25 4
gpt4 key购买 nike

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

ControllerContext.RouteData.GetRequiredString("action")

但是如果我的路由允许 http://mydomain/this 有空格

如何获得“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
);

最佳答案

如果您的路由看起来像那样,则没有任何东西可以为您提供原始字符串(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"]

关于c# - 从路由数据中获取 Action 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com