开发者

asp.net mvc: read route param inside controller

开发者 https://www.devze.com 2023-01-09 21:55 出处:网络
I\'m trying to read the a parameter that I\'ve defined in a route from inside the controller. The route:

I'm trying to read the a parameter that I've defined in a route from inside the controller.

The route:

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

From inside the controller I'd like to be ab开发者_JS百科le to read the route parameter locale, but have not idea where to look for it.

The 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();
        }

    }
}

Any help is appreciated!


Dave,

This is from my basecontroller but you should be able to do exactly the same from a top level one too:

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

good luck

jim


    public VoiceController()
    {
        var locale = this.RouteData.Values["locale"];
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消