React + .
NET Core Notifications System
Scenario
- After a user logs in or registers, on the dashboard header, a notification icon fetches and shows
project-related notifications.
Backend ([Link] Core)
1. Create Notification Model
public class Notification
public int Id { get; set; }
public int UserId { get; set; }
public string Message { get; set; }
public bool IsRead { get; set; } = false;
public DateTime CreatedAt { get; set; } = [Link];
2. Create NotificationsController
[ApiController]
[Route("api/[controller]")]
public class NotificationsController : ControllerBase
private readonly YourDbContext _context;
public NotificationsController(YourDbContext context)
{
_context = context;
[HttpGet("user/{userId}")]
public async Task<IActionResult> GetUserNotifications(int userId)
var notifications = await _context.Notifications
.Where(n => [Link] == userId)
.OrderByDescending(n => [Link])
.ToListAsync();
return Ok(notifications);
3. Insert Notification on Registration/Login
_context.[Link](new Notification
UserId = [Link],
Message = "Welcome! Your registration was successful."
});
await _context.SaveChangesAsync();
Frontend (React)
1. Install Axios
npm install axios
2. Fetch Notifications in Header Component
import React, { useEffect, useState } from 'react';
import axios from 'axios';
function NotificationIcon({ userId }) {
const [notifications, setNotifications] = useState([]);
useEffect(() => {
[Link](`/api/notifications/user/${userId}`)
.then(response => setNotifications([Link]))
.catch(error => [Link](error));
}, [userId]);
return (
<div>
<span>Notification</span>
<span>{[Link]}</span>
<div className="dropdown">
{[Link]((n, index) => (
<div key={index}>
{[Link]}
<small>{new Date([Link]).toLocaleString()}</small>
</div>
))}
</div>
</div>
);
export default NotificationIcon;
3. Use in Dashboard Header
<NotificationIcon userId={loggedInUserId} />
Easy Tips
- Use SignalR for real-time notifications later.
- Limit number of fetched notifications for performance.
- Add an "isRead" toggle when panel opens.
Summary
Feature | Implementation
------------------|--------------------------
Notification model | Created in backend
API endpoint | /api/notifications/user/{id}
React header icon | Fetches via Axios
Notification logic | On register/login/server events