Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views3 pages

Registration Pagecode

The document outlines the implementation of a registration page using the MVC pattern in ASP.NET Core. It includes a User model with validation attributes, a registration view for user input, an AccountController to handle registration logic, and a Program.cs file to configure the application. The registration process includes input fields for username, email, password, and confirmation, with success messaging upon valid submission.

Uploaded by

kingkkboss442
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Registration Pagecode

The document outlines the implementation of a registration page using the MVC pattern in ASP.NET Core. It includes a User model with validation attributes, a registration view for user input, an AccountController to handle registration logic, and a Program.cs file to configure the application. The registration process includes input fields for username, email, password, and confirmation, with success messaging upon valid submission.

Uploaded by

kingkkboss442
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Registration page-MVC

1. Model- User
2. namespace registrationpage.Models
3. {
4. using System.ComponentModel.DataAnnotations;
5.
6. public class User
7. {
8. [Required]
9. [Display(Name = "Username")]
10. public string? Username { get; set; }
11.
12. [Required]
13. [EmailAddress]
14. public string? Email { get; set; }
15.
16. [Required]
17. [DataType(DataType.Password)]
18. public string? Password { get; set; }
19.
20. [Required]
21. [DataType(DataType.Password)]
22. [Compare("Password", ErrorMessage = "Passwords do not match.")]
23. public string? ConfirmPassword { get; set; }
24. }
25. }

2.Views-Account-Register.cshtml
@model registrationpage.Models.User
@{
ViewBag.Title = "Register";
}
@if (ViewBag.Message != null)
{
<div class="alert alert-success">
@ViewBag.Message
</div>
}

<h2>Register</h2>

@using (Html.BeginForm("Register", "Account", FormMethod.Post))


{
@Html.AntiForgeryToken()

<div>
@Html.LabelFor(m => m.Username)
@Html.TextBoxFor(m => m.Username)
@Html.ValidationMessageFor(m => m.Username)
</div>

<div>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
@Html.ValidationMessageFor(m => m.Email)

</div>
<div>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>

<div>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
@Html.ValidationMessageFor(m => m.ConfirmPassword)
</div>

<button type="submit">Register</button>
}

3.Controller-AccountController
using Microsoft.AspNetCore.Mvc;
using registrationpage.Models;

namespace registrationpage.Controllers
{
public class AccountController : Controller
{
// GET: /Account/Register
public ActionResult Register()
{
return View();
}

// POST: /Account/Register
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(User model)
{
if (ModelState.IsValid)
{
// TODO: Save to database
ViewBag.Message = "Registration successful!";
//return RedirectToAction("Register");
}

return View(model);
}

// Optional Login page placeholder


// public ActionResult Login()
// {
// return View();
//}

}
}

4.Program.cs
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.


builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.


if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios,
see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Account}/{action=Register}/{id?}");

app.Run();

You might also like