Open Closed

Null Reference Exception When Overriding RegisterModel #2274


User avatar
0
riley.trevillion.spatialhub4d created

Check the docs before asking a question: https://docs.abp.io/en/commercial/latest/ Check the samples, to see the basic tasks: https://docs.abp.io/en/commercial/latest/samples/index The exact solution to your question may have been answered before, please use the search on the homepage.

If you're creating a bug/problem report, please include followings:

  • ABP Framework version: v4.3.3
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no

Hello

I'm attempting to override the Register page of the account module in my project. I want the ability to add a First Name and Last Name to the registration form. There is a requirement to be able to add additional fields in the future, but the focus right now is just to add those two fields.

I've followed the documentation on how to completely override the page as detailed here - https://docs.abp.io/en/abp/4.3/UI/AspNetCore/Customization-User-Interface#completely-overriding-a-razor-page

When attempting to register as a new user, the Register page correctly renders with the new fields and is correctly picking up my new model class. When attempting to submit I get a Null Reference Exception from the base class. I'm not sure why.

My aim here is to run through the standard logic that ABP takes care of as part of user registration and then update the new user with the additional information, save it to the database and continue onwards.

I've attached screenshots of the relevant parts to assist with diagnosing this

Register.cshtml

Register.cshtml.cs

Register.cshtml.cs Error

File Structure


6 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share your project with me(include steps)? liming.ma@volosoft.com

  • User Avatar
    0
    riley.trevillion.spatialhub4d created

    Hi @maliming

    I've emailed you a link to a downloadable copy of a project that reproduces the issue I am seeing. Please let me know if you haven't received it or have trouble downloading it.

    Thankyou

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can check this. I copied the code from base.

    protected async override Task<IdentityUser> RegisterLocalUserAsync()
    {
        var userDto = await RegisterLocalUser2Async();
        var user = await UserManager.GetByIdAsync(userDto.Id);
    
        user.Name = Input.FirstName;
        user.Surname = Input.LastName;
    
        (await UserManager.UpdateAsync(user)).CheckErrors();
    
        return user;
    }
    
    protected virtual async Task<IdentityUser> RegisterLocalUser2Async()
    {
        ValidateModel();
    
        var captchaResponse = string.Empty;
        if (UseCaptcha)
        {
            captchaResponse = HttpContext.Request.Form[RecaptchaValidatorBase.RecaptchaResponseKey];
        }
        var userDto = await AccountAppService.RegisterAsync(
            new RegisterDto
            {
                AppName = "MVC",
                EmailAddress = Input.EmailAddress,
                Password = Input.Password,
                UserName = Input.FirstName + Input.LastName,
                ReturnUrl = ReturnUrl,
                ReturnUrlHash = ReturnUrlHash,
                CaptchaResponse = captchaResponse
            }
        );
    
        return await UserManager.GetByIdAsync(userDto.Id);
    }
    
  • User Avatar
    0
    riley.trevillion.spatialhub4d created

    Hi @maliming

    The example you've provided fixes the exception itself but this solution would mean that I'd need to copy / duplicate the code from the RegisterModel (or any other ABP model I want to override) in order to override that method. That is not ideal if I need to override lots of different models and methods. I will definitely need to do this in the future across a range of different razor pages related to the Login and Registration workflows.

    The documentation for completely overriding a razor page says that I should be able to call base class methods to perform the default ABP logic (which I want it to do) and then I can perform my own logic before or after this call as shown here - https://docs.abp.io/en/abp/4.4/UI/AspNetCore/Customization-User-Interface#completely-overriding-a-razor-page

    I'm following this process in the sample application I have emailed to you, but calling the base class method gives me that exception.

    Is this a bug?

    I would like to be able to call base class methods directly from my child class instead of having to duplicate the code in my child class.

    Is this possible?

    Thankyou

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You did not provide the parameters required by the base class.

  • User Avatar
    0
    riley.trevillion.spatialhub4d created

    Hi @maliming

    Thanks for pointing that out. This was indeed the problem. There is a requirement to not require a user to provide a username (only email) when first registering. This is why that field would be null and causing these issues. I have worked around this by setting the the username to the email address upon signup and removed the username field from the ModelState so it doesn't trip up any 'required' validation errors.

    Thanks again, the ticket can remain closed.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11