简体   繁体   中英

asp.net mvc: read route param inside controller

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 able to read the route parameter , 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"];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM