1

I have the following code-lines:

return Json(new { redirectTo = UrlHelper.Action("Index", "Home") });

and

ModelState.AddModelError("Useraccount.Email", emailAlreadyExistsException.Message);

For both the UrlHelper.Action method and the ModelState.AddModelError Method I'd like to avoid the hardcoded strings. Is there a better possibility?

mosquito87
  • 4,270
  • 11
  • 46
  • 77

1 Answers1

1

You could create a constants file and use constants instead:

public static class Constants 
{
    public const string HomeController = "Home";
    public const string IndexAction = "Index";
    public const string UserAccountEmail = "Useraccount.Email";
}

Your code then becomes:

return Json(new { redirectTo = UrlHelper.Action(Constants.IndexAction, Constants.HomeController) });

and

ModelState.AddModelError(Constants.UserAccountEmail, emailAlreadyExistsException.Message);

As an alternative for validation, you could use a library like FluentValidation

levelnis
  • 7,665
  • 6
  • 37
  • 61