In this complete guide, we will build a fully functional Blog System using ASP.NET Core and Entity Framework Core (EF Core). This tutorial is aimed at developers who want to learn practical implementation of CRUD operations, authentication, and basic content management using modern .NET technologies.
Open Visual Studio and create a new ASP.NET Core Web Application. Choose either Web Application (Model-View-Controller) or Razor Pages as the project template. In this guide, weβll use Razor Pages for simplicity.
dotnet new webapp -n BlogSystem
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public int CategoryId { get; set; }
public Category Category { get; set; }
public string UserId { get; set; }
public ApplicationUser User { get; set; }
}
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection Posts { get; set; }
}
public class ApplicationUser : IdentityUser
{
public ICollection Posts { get; set; }
}
public class BlogDbContext : IdentityDbContext<ApplicationUser>
{
public BlogDbContext(DbContextOptions<BlogDbContext> options) : base(options) { }
public DbSet<Post> Posts { get; set; }
public DbSet<Category> Categories { get; set; }
}
builder.Services.AddDbContext<BlogDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<BlogDbContext>();
builder.Services.AddRazorPages();
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=BlogSystemDb;Trusted_Connection=True;"
}
dotnet ef migrations add InitialCreate
dotnet ef database update
public class IndexModel : PageModel
{
private readonly BlogDbContext _context;
public IndexModel(BlogDbContext context)
{
_context = context;
}
public IList<Post> Posts { get; set; }
public async Task OnGetAsync()
{
Posts = await _context.Posts.Include(p => p.Category).ToListAsync();
}
}
@page
@model IndexModel
<h2>Blog Posts</h2>
<table>
@foreach (var post in Model.Posts)
{
<tr>
<td>@post.Title</td>
<td>@post.Category.Name</td>
<td>@post.CreatedAt.ToShortDateString()</td>
</tr>
}
</table>
Repeat similar steps to create Razor Pages for:
[Authorize]
public class CreateModel : PageModel
{
//...
}
If using Individual User Accounts template, these are scaffolded by default. Ensure app.UseAuthentication() and app.UseAuthorization() are in your middleware pipeline.
Update _Layout.cshtml to add navigation to blog posts and account actions:
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" asp-page="/Posts/Index">Posts</a></li>
<li class="nav-item"><a class="nav-link" asp-page="/Categories/Index">Categories</a></li>
@if (User.Identity.IsAuthenticated)
{
<li class="nav-item"><a class="nav-link" asp-page="/Account/Logout">Logout</a></li>
}
else
{
<li class="nav-item"><a class="nav-link" asp-page="/Account/Login">Login</a></li>
}
</ul>
Create CRUD Razor Pages for Category model similar to Post model.
dotnet publish -c Release -o ./publish
Host the application using Azure App Service, IIS, or any hosting that supports ASP.NET Core.
This tutorial gave you a complete walkthrough of how to build a scalable and secure Blog System using ASP.NET Core and Entity Framework Core. You now have the foundation to add advanced features like rich text editing, pagination, search, user roles, and SEO optimization.
Copyrights © 2024 letsupdateskills All rights reserved