c# - ASP.NET MVC : read route param inside controller

标签 c# asp.net-mvc

我正在尝试从 Controller 内部读取我在路由中定义的参数。

路线:

routes.MapRoute(
"BusinessVoice", // Route name
"business/{controller}/{action}/{id}", // URL with parameters
new { controller = "Voice", action = "Index",
id = UrlParameter.Optional, locale = "business" } // Parameter defaults
);

我希望能够从 Controller 内部读取路由参数 locale,但不知道在哪里寻找它。

Controller :

namespace www.WebUI.Controllers
{
    public class VoiceController : Controller
    {
        public VoiceController()
        {
            ... want to read the locale param here
        }


        public ViewResult Index(string locale)
        {
            return View();
        }

    }
}

感谢任何帮助!

最佳答案

戴夫,

这是来 self 的基础 Controller ,但您也应该能够从顶级 Controller 中执行完全相同的操作:

    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        var locale = requestContext.RouteData.Values["locale"].ToString() ?? System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
        base.Initialize(requestContext);
    }

祝你好运

吉姆

关于c# - ASP.NET MVC : read route param inside controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3400126/

相关文章:

asp.net-mvc - 为位于子域上的图像在 http 和 https 之间切换

c# - 从部分 View 单击按钮重定向到父操作方法

c# - jquery 主题生成器 CSS 问题

c# - 将值四舍五入为 .0 或 .5 的数学函数

asp.net-mvc - ASP MVC 3 cookie 丢失 HttpOnly 和 Secure 标志

c# - 无法从 Asp MVC 6 Web Api 使用 HttpClient

c# - 加密/解密初学者问题

c# - 继承 WPF 窗口

c# - 禁用排序 ListView

c# - 使用 ASP.Net MVC3 显示 byte[] 中包含的图像