Localizing Default Error Messages in MVC

Recently in a (non-english) project i’m working on, I was surprised with the following message “The value ‘150000.00’ is not valid for Preço.”.  I thought that my project was already localized to Portuguese, but apparently no.

mvc

The problem is related with some messages that can’t be localized using validation attributes Like the RequiredAttribute found in System.ComponentModel.DataAnnotations namespace.

Ex:

[Required(ErrorMessageResourceName = "ValidacaoObrigatoria", ErrorMessageResourceType = typeof(MyProject.Properties.Resources))]
public int Prop1 { get; set; }

Messages like “The value ‘{0}’ is not valid for {1}” need a different approach.
For localize default error messages in MVC we will need to add the following code to the Global.asax Application_Start method.

DefaultModelBinder.ResourceClassKey = "ValidationResources";
ClientDataTypeModelValidatorProvider.ResourceClassKey = "ValidationResources";

Where “ValidationResources” is the the name of the resource file (class key) that contains localized string values.
Next create a resource file name ValidationResources.resx or ValidationResources.fr.resx or whatever culture you need to support, mark it embeded inside App and add the following:

FieldMustBeDate The field {0} must be a date.
FieldMustBeNumeric The field {0} must be a number.
PropertyValueInvalid The value ‘{0}’ is not valid for {1}.
PropertyValueRequired A value is required.

My translation to Portuguese will be something like this:

PropertyValueInvalid O valor ‘{0}’ não é válido para o campo ‘{1}’.
FieldMustBeDate O campo ‘{0}’ deve ser uma data válida.
FieldMustBeNumeric O campo ‘{0}’ deve ser um número válido.
PropertyValueRequired O preenchimento deste campo é obrigatório.

Note:
The resource file should be added to the App_GlobalResources folder.

Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter

MVC 5 Signout Fails

Recently I came across with a problem in the LogOff method that is generated with a new MVC 5 solution.

mvc

The problem was:

  • User1  logged in  and Login method is called in the Account controller.
  • User1 logs off and the LogOff method is called in the Account controller and the following code is executed:
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult LogOff()
    {
        AuthenticationManager.SignOut();
        return RedirectToAction("Login", "Account");
    }
    
  • User2 logged in  and Login method is called in the Account controller. After this the User1 is logged in instead of User2.

It seems this is a known issue where the explicit sign-in is preferred over a generic sign-out with no params. The solution is change the  call to SignOut and pass the AuthenticationType of the cookie, and this will override the explicit sign-in.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie);
    return RedirectToAction("Login", "Account");
}
Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter