validation - How manage error handling in Controller/Repository and show errors in View in Asp.Net Core MVC -


i have created asp.net core mvc application.

i create example form updating user , catch specific exceptions , show them in form.

i want catch 2 types of exceptions/errors:

1) form validation

2) repository exceptions

i have following code:

userrepository

public async task<userdto> updateuserasync(userdto user)         {             var useridentity = await _usermanager.findbyidasync(user.id.tostring());              if (useridentity == null) throw new userfriendlyexception("user not exist");              useridentity.mapto(user);              await _usermanager.updateasync(useridentity);              var userdto = useridentity.tomodel();              return userdto;         } 

user controller

[httppost]         public async task<iactionresult> user(userdto user)         {             //model not valid             if (!modelstate.isvalid)             {                 return view(user);             }              try             {                 await _identityrepository.updateuserasync(user);             }             catch (userfriendlyexception e)             {                 //some error repository                 modelstate.addmodelerror(e.message, e.message);                  return view(user);             }              //ok redirect list of users             return redirecttoaction(nameof(users));         } 

userfriendlyexception

public class userfriendlyexception : exception     {          public userfriendlyexception()         {          }          public userfriendlyexception(string message) : base(message)         {          }     } 

in view

<div asp-validation-summary="all" class="text-danger"></div> 

userdto

public class userdto     {         [required]         public int id { get; set; }          [required]         public string username { get; set; }          [required]         public string email { get; set; }          public bool emailconfirmed { get; set; }          public string phonenumber { get; set; }          public bool phonenumberconfirmed { get; set; }          public bool lockoutenabled { get; set; }          public bool twofactorenabled { get; set; }          public string accessfailedcount { get; set; }          public string lockoutend { get; set; }     } 

is way handle error/exception above? best practise things - better pattern it?


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -