Associating an ASP.NET MVC4 Model

Does anyone know when model binding occurs in the query life cycle? The reason I ask is because I am facing some localization issues.

Is model binding performed before OnActionExecuting?

I am currently setting the current culture in the OnActionExecuting global action filter, but this is not respected when performing model binding. A request is a POST.

Thanks in advance.

+7
source share
4 answers

BindModel comes first . Can your localization change upon request? If possible, you can override the default mediator and, if necessary, set your locale. Follow the link below about creating a custom mediator

ASP.NET MVC Model Binder for universal type

(to prove that you just put two breakpoints and you will see the order)

I think it’s better to set localization, but more specific information will be needed, and this may be another question.

0
source

I would suggest you set the culture at a very early point, and not in the action filter. In my current project, I set the culture in the Application_AcquireRequestState event in Global.asax.cs. You can try this.

 protected void Application_AcquireRequestState(Object sender, EventArgs e) { // set the culture } 
+3
source

I found that in an MVC application, the best way is to use a custom route manipulator and set the culture in this handler. It works flawlessly with ModelBinders and localized resources in data annotations.

 public class MultiCultureMvcRouteHandler : MvcRouteHandler { protected override IHttpHandler GetHttpHandler(RequestContext requestContext) { // get culture from route data var culture = requestContext.RouteData.Values["culture"].ToString(); var ci = new CultureInfo(culture); Thread.CurrentThread.CurrentUICulture = ci; Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name); return base.GetHttpHandler(requestContext); } } 

There is an excellent article in a blog post by Alex Adaman describing this technique.

See also this question and answers to SO.

+2
source

I am also facing the same problem. When model binding has invalid data, it starts before ActionFilter (s).

I did not like the proposed solutions because the mess with routing was not my preferred solution. Listening to Application_AcquireRequestState is problematic because this event fires for every request, and not just for requests that will be routed to MVC controllers.

In the end, I wrote a custom implementation of IControllerFactory that uses the DefaultControllerFactory internally and executes the localization code inside the CreateController method.
This is not ideal either, hope this helps.

  public class PluggableControllerFactory : IControllerFactory { private readonly IControllerFactory innerControllerFactory; public PluggableControllerFactory() { innerControllerFactory = new DefaultControllerFactory(); } public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName) { // Run your culture localization here return innerControllerFactory.CreateController(requestContext, controllerName); } public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName) { return innerControllerFactory.GetControllerSessionBehavior(requestContext, controllerName); } public void ReleaseController(IController controller) { innerControllerFactory.ReleaseController(controller); } } } 
0
source

All Articles