2

I switch from the old attribute routing library to the bundled asp.net MVC 5 routing. But now my lang route value is null in the Application_AcquireRequestState

// rootcontroller.cs       
[HttpGet]
[Route("")]
[Route("{lang}")]
// old attribute routing worked:
// [GET("/{lang}")]
public ActionResult Index(string lang =null)
{
    return View();
}

// global.asax
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
    var handler = Context.Handler as MvcHandler;

    if (handler == null)
       return;
    var routeData = handler.RequestContext.RouteData;

    var lang = routeData.Values["lang"];   // null instead for example 'de'
    // ... set current culture
}

1 Answer 1

4

Attribute routing in MVC 5 uses a special key named "MS_DirectRouteMatches" which contains a list of RouteData elements. I am not sure why they did this as it seems that only one RouteData element is possible. So you need to check for this key and use its first value if it exists.

var routeData = handler.RequestContext.RouteData;

if (routeData != null)
{
    if (routeData.Values.ContainsKey("MS_DirectRouteMatches"))
    {
        routeData = ((IEnumerable<RouteData>)routeData.Values["MS_DirectRouteMatches"]).First();
    }
}

var lang = routeData.Values["lang"];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.