问题
We want to set the Thread.CurrentCulture
- before the ASP.NET MVC model binder / validator runs (of course), but
- after the user has been authorized (as we want to load the culture from a UserSettings table)
What's the correct extension point we should go for?
An action filter is too late, Global.asax is too early (user not authorized yet). Anyone with a good idea?
回答1:
Global.asax is too early
Global.asax has events that occur at various points in the request life cycle, some too early (BeginRequest), some too late (EndRequest), and maybe one that is just right for your requirement.
Maybe a handler for PostAuthorizeRequest
, or if you want to store stuff in Session, PostAcquireRequestState
would do?
回答2:
You could use the AuthorizationFilter
and just extend the build in functionality. At the point of authorization you will be able to tell whether they are authorized or not and perform your logic accordingly.
public class CustomAuthAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
bool isAuthorized = base.AuthorizeCore(httpContext);
if (isAuthorized)
{
// set culture if user is authorized
}
else
{
// set culture if user is not authorized
}
return isAuthorized;
}
}
来源:https://stackoverflow.com/questions/18701065/asp-net-mvc-set-culture-after-authorization