Assignment 4 : Middlewares and routing assignment
1. What is routing in .NET Core MVC, and how does
it work?
2. What is the significance of route parameters in
routing?
3. How do you handle optional parameters in route
definitions?
4. How can you define constraints on route
parameters?
5. How do regular expressions work in routing
constraints?
6. What is the purpose of int, guid, alpha constraints
in routing?
7. How can you apply multiple constraints to a
single route parameter?
8. How does catch-all routing (* wildcard) work in
.NET Core?
9. What happens if a request does not match any
defined route constraints?
10. What is the difference between
app.UseRouting() and app.UseEndpoints()?
11. How does endpoint routing work in .NET
Core?
12. What is the request processing pipeline in
.NET Core?
13. What is middleware in .NET Core?
14. What is the difference between app.Use(),
app.Run(), and app.UseMiddleware()?
15. What is UseWhen() in middleware, and how
does it work?
16. How can you create custom middleware in
.NET Core?
17. How can you conditionally execute
middleware based on request headers or paths?
18. What is the difference between “Api” , “MVC”,
“Minimal API”
19. What is the difference between MapGet and
MapPost in minimal APIs?
20. What is wwwroot in an ASP.NET Core
application?
21. Why is wwwroot publicly accessible, and how
can you restrict access?
22. How do you enable serving static files in an
ASP.NET Core application?
23. How can you change the default static files
location?
24. How do you restrict access to specific static
files?
25. Where should you store a large number of
images in an ASP.NET Core application , in
wwwroot or cloud?
Middleware Tasks:
1. Log Request Processing Time:
Implement a middleware that implement
IMiddleware to calculates the time taken to
process a request.
Log the request method, path, and total
processing time.
Use app.Use to register this middleware.
2. Restrict Access Based on Time:
Implement a middleware that only allows access
to the application during specific hours (9 AM - 5
PM).
If a request is made outside of these hours, return
an HTTP 403 Forbidden with a message: "Access
is restricted to working hours."
Use app.Use to register this middleware.
Routing Tasks
1. Project Setup:
Create a new ASP.NET Core mvc empty
application (using .NET 9
Configure all endpoints in Program.cs using
MapGet, MapPost
2. Course Model:
Create a model class called Course with the following
properties:
int CourseId
string CourseName
decimal CourseFee
string CourseDuration
3. Fixed List of Courses:
Define an in-memory, fixed list (for example, a
List<Course>) containing several sample courses.
Sample Data:
Course 1:
o CourseId = 101
o CourseName = "C# for Beginners"
o CourseFee = 250
o CourseDuration = "8 weeks"
Course 2:
o CourseId = 102
o CourseName = "ASP.NET Core Fundamentals"
o CourseFee = 300
o CourseDuration = "10 weeks"
Course 3:
o CourseId = 103
o CourseName = "Entity Framework Core"
o CourseFee = 200
o CourseDuration = "6 weeks"
Endpoints:
1- URL: /
Method: GET
Response: Welcome to the Best Courses
2- GET "/courses" – List All Courses:
URL: /courses
Method: GET
Returns the complete list of courses in JSON
format.
3- GET "/course/{courseId}" – Get Specific Course
Details:
URL: /course/{courseId}
Method: GET
Route Constraint: {courseId} must be an integer.
Response: Returns the details of the course with
the specified CourseId.
Validation: If the course exists, return HTTP 200.
If not found, return HTTP 404 with a message:
"Course with id {courseId} not found".
4- POST "/course" – Add a New Course:
URL: /course
Method: POST
Accepts a new course object (in JSON format) and
adds it to the list.
Validation: Ensure CourseId is unique.
If valid, add the course and return HTTP 201.
If the course exists, return HTTP 400 with a message.
5- GET "/course-fee/{courseId}" – Apply Discount
Logic:
URL: /course-fee/{courseId}
Method: GET
Query Parameter: ?discount=10 (optional, must
be between 0-100).
Behavior: If provided, applies a discount to the
course fee.
HTTP Status Codes:
200 OK – Successful GET requests.
201 Created – Successful POST requests.
400 Bad Request – Invalid input.
404 Not Found – Course not found.