0

Account folder in project and Register.cs modelProgram.cs codeThis is the user registration controller code on the site that returns the registration view, but when it runs and clicks on the registration, I get a 404 error. Even the registration page follows _Layout.

 public class AccountController : Controller
 {
    
     //Dependency Injection
     private readonly checkRegisterUser _checkRegisterUser;

     public AccountController(checkRegisterUser checkRegisterUser)
     {
         checkRegisterUser = _checkRegisterUser;
     }
     
   

     [HttpPost]
     public IActionResult Register(UA_ViewModel register)
     {
         if (!ModelState.IsValid)
         {
             return View("Register", register);
         }

         if(_checkRegisterUser.isExistUserEmail(register.userEmail.ToLower()))
         {
             ModelState.AddModelError(key:"userEmail", errorMessage:"این ایمیل قبلا ثبت شده است");
             return View("Register", register);
         }
          UsersAccounts user = new UsersAccounts()
         {
             userName = register.userName,
             userEmail = register.userEmail,
             userPassword = register.userPassword
         };
         _checkRegisterUser.addUser(user);
         return View("Register", register);
     }
}

I tried to solve the error by giving [Route()] attribute and writing Constructor but no change was made.

7
  • Hi @AliHANiF, 1. How do you configure the route template in Program.cs? 2. How is your razor view code and pls F12 in the browser to check your request url? Are you sure you send the post request with correct url? Besides, do you use Area for the controller and views? Commented Jul 3, 2024 at 7:12
  • have you tried putting [AllowAnonymous] on the action method ? Commented Jul 3, 2024 at 7:15
  • I used the AllowAnonymous feature for the registration extension and I get a 405 error Commented Jul 4, 2024 at 11:47
  • Hi Rena, In Program.cs Map Controller Route is located on the Home controller by default and this user registration code is located on the Account Controller. Commented Jul 4, 2024 at 11:58
  • Hi @AliHANiF, 1. can you share the screenshot for the location of the AccountController? 2. could you pls share your Program.cs? 3. also pls F12 in the browser and choose the Network panel to check the request you send. Pls share enough code, it would be more helpful to resolve your issue. If not mind, you can also create a minimal repo to share with us. Commented Jul 5, 2024 at 8:13

2 Answers 2

1

For the user registration controller, you must create a get type action and a post controller to send the form information. In the view, the action field in the form tag must point to the post controller . in cotroller :

 public class AccountController : Controller
 {
    
     //Dependency Injection
     private readonly checkRegisterUser _checkRegisterUser;

     public AccountController(checkRegisterUser checkRegisterUser)
     {
         checkRegisterUser = _checkRegisterUser;
     }

     [httpGet("[action]")]
     publick IActionResult Register()
     {
        View();
     }

     [HttpPost("[action]")]
     public IActionResult Register(UA_ViewModel register)
     {
         if (!ModelState.IsValid)
         {
             return View(register);
         }

         if(_checkRegisterUser.isExistUserEmail(register.userEmail.ToLower()))
         {
             ModelState.AddModelError(key:"userEmail", errorMessage:"این ایمیل قبلا ثبت شده است");
             return View(register);
         }
          UsersAccounts user = new UsersAccounts()
         {
             userName = register.userName,
             userEmail = register.userEmail,
             userPassword = register.userPassword
         };
         _checkRegisterUser.addUser(user);
         return View(register);
     }
}

and in view :

<form action="/Account/Register" type="post">
...
</form>
Sign up to request clarification or add additional context in comments.

Comments

0

This is the view interface, but you use the [HttpPost] attribute identifier, if the post data submission, please do not use return view(...) ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.