0

I'm trying to build a signup and confirm page using AWS Cognito and I'm getting thrown by this exception

System.NullReferenceException: 'Object reference not set to an instance of an object.'

AspNetCore.Views_Accounts_Signup.ViewData.get returned null.

I'm in a learning process so I'm not quite sure how the things are work together to fix it

Sigunup page

@page
@{
    ViewData["Title"] = "Signup Page";
}


@model CognitoWebAUTH.Models.Accounts.SignupModel;

<div class="row">
    <div class="cold-md-4">
        <form method="post">
            <h4>Create new account</h4>
            <div asp-validation-summary="All" class="text-danger">

            </div>
            <div class="form-group">
                <label asp-for="Email"></label>
                <input asp-for="Email" class="form-control"></input>
                <span asp-for="Email" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Password"></label>
                <input asp-for="Password" class="form-control"></input>
                <span asp-for="Password" class="text-danger"></span>

            </div>
            <div class="form-group">
                <label asp-for="ConfirmPassword"></label>
                <input asp-for="ConfirmPassword" class="form-control"></input>
                <span asp-for="ConfirmPassword" class="text-danger"></span>

            </div>
            <button type="submit" class="btn btn-default"></button>
        </form>
    </div>
</div>

Signup model

public class SignupModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [StringLength(8,ErrorMessage ="Password must be at least six characters long!")]
    [Display(Name ="Password")]
    public string Password { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Compare("Password",ErrorMessage ="Password and its confirmation do not match")]
    [Display(Name = "ConfirmPassword")]
    public string ConfirmPassword { get; set; }
}

startup

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCognitoIdentity();
        services.AddControllersWithViews();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }
        app.UseStaticFiles();

        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }

1 Answer 1

4

Remove @page in your Sigunup page,@page is used in razor page.When you use @page in a view,it will be a razor page.You can refer to the official doc.

When you remove it,you can fix the error.

Sign up to request clarification or add additional context in comments.

1 Comment

Your suggestion worked for me. I am working with an ASP.NET Core MVC project that includes some razor pages. I didn't even add the @page, it must have auto generated. Thank you

Your Answer

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