{"id":961,"date":"2025-06-11T10:07:39","date_gmt":"2025-06-11T07:07:39","guid":{"rendered":"https:\/\/www.certbolt.com\/certification\/?p=961"},"modified":"2026-01-01T14:39:47","modified_gmt":"2026-01-01T11:39:47","slug":"asp-net-core-explained-a-guide-to-routing-in-asp-net","status":"publish","type":"post","link":"https:\/\/www.certbolt.com\/certification\/asp-net-core-explained-a-guide-to-routing-in-asp-net\/","title":{"rendered":"ASP.NET Core Explained: A Guide to Routing in ASP.NET"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">ASP.NET Core is a modern, open-source, and cloud-optimized web framework developed by Microsoft for building web applications. Unlike the traditional ASP.NET framework that was tied exclusively to the Windows platform, ASP.NET Core is cross-platform, meaning it can run on Windows, macOS, and Linux. This framework was introduced as a redesign of the original ASP.NET to provide better flexibility, higher performance, and more modularity to support various application types across different platforms.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">ASP.NET Core replaced the older .NET Framework-based ASP.NET by focusing on a lightweight, modular architecture. It offers improved performance and the ability to run in different environments, making it ideal for cloud-based applications and microservices architectures.<\/span><\/p>\n<p><b>Why ASP.NET Core?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The original ASP.NET framework served developers well for many years, but as technology evolved and the number of platforms increased, the need for a more advanced framework became clear. ASP.NET Core addresses the limitations of the earlier versions by providing a more streamlined, faster, and cross-platform solution. It enables developers to build modern web applications that are scalable, maintainable, and easily deployable to the cloud.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Though ASP.NET Core shares some foundational ideas with the original ASP.NET, it is fundamentally a new framework with different architecture, improved middleware pipeline, and a modular design. This separation allows developers to include only the necessary components, reducing the application&#8217;s footprint and improving startup time.<\/span><\/p>\n<p><b>Cross-Platform Capabilities<\/b><\/p>\n<p><span style=\"font-weight: 400;\">One of the most significant features of ASP.NET Core is its cross-platform nature. This means you can build and run ASP.NET Core applications not only on Windows but also on macOS and Linux. This is a crucial advantage for teams using diverse operating systems or deploying applications in cloud environments that use Linux servers.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The framework\u2019s compatibility with the .NET Core runtime allows applications to benefit from the latest improvements in performance, security, and deployment. Developers are no longer limited by platform-specific constraints, which opens up new possibilities for deployment and development workflows.<\/span><\/p>\n<p><b>Open Source and Community Driven<\/b><\/p>\n<p><span style=\"font-weight: 400;\">ASP.NET Core is fully open source, which encourages contributions from developers worldwide. The open-source nature has led to a faster evolution of the framework and better transparency. Developers can view the source code, suggest improvements, or even contribute to the codebase. This collaborative approach ensures ASP.NET Core stays up-to-date with modern development practices and incorporates community feedback effectively.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Microsoft hosts the source code on public repositories, making it easy for developers to follow changes, report issues, and participate in discussions.<\/span><\/p>\n<p><b>Project Structure and File System in ASP.NET Core<\/b><\/p>\n<p><span style=\"font-weight: 400;\">When creating a new ASP.NET Core project, you will notice a predefined structure of files and folders that help organize your application logically. Understanding this project layout is crucial for working efficiently with the framework.<\/span><\/p>\n<p><b>Solution Explorer and Important Files<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Within the Solution Explorer in development environments such as Visual Studio, you will find essential files and folders created by default. One of the key files is <\/span><b>global.json<\/b><span style=\"font-weight: 400;\">, which specifies the location of the application\u2019s source code. This file informs the .NET Core SDK where to look for project files during build and runtime.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If your source code resides outside the specified folders (commonly <\/span><span style=\"font-weight: 400;\">src<\/span><span style=\"font-weight: 400;\"> and <\/span><span style=\"font-weight: 400;\">test<\/span><span style=\"font-weight: 400;\">), the project will not build correctly unless you update the <\/span><span style=\"font-weight: 400;\">global.json<\/span><span style=\"font-weight: 400;\"> file accordingly. This system ensures clarity in large projects that may contain multiple components or services.<\/span><\/p>\n<p><b>Project File: project.json vs csprojj<\/b><\/p>\n<p><span style=\"font-weight: 400;\">In earlier versions of ASP.NET Core, the project used a file named <\/span><span style=\"font-weight: 400;\">project.json<\/span><span style=\"font-weight: 400;\"> to manage dependencies and configuration. This JSON file lists packages, tools, and framework targets required for the project. However, with later updates, ASP.NET Core transitioned back to using the traditional <\/span><span style=\"font-weight: 400;\">.csproj<\/span><span style=\"font-weight: 400;\"> XML-based project files.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The <\/span><span style=\"font-weight: 400;\">.csproj<\/span><span style=\"font-weight: 400;\"> files now serve as manifests for all source code, references, and build options in the project. Understanding this change is essential when maintaining legacy projects or upgrading them to newer versions of ASP.NET Core.<\/span><\/p>\n<p><b>Startup Class and Application Configuration<\/b><\/p>\n<p><span style=\"font-weight: 400;\">At the core of every ASP.NET Core application is the <\/span><span style=\"font-weight: 400;\">Startup<\/span><span style=\"font-weight: 400;\"> class. This class plays a vital role in configuring services and the HTTP request pipeline for the application. The <\/span><span style=\"font-weight: 400;\">Startup<\/span><span style=\"font-weight: 400;\"> class typically contains two key methods:<\/span><\/p>\n<p><b>ConfigureServices Method<\/b><\/p>\n<p><span style=\"font-weight: 400;\">This method is responsible for registering services that the application will use through dependency injection. Services might include database contexts, authentication providers, MVC services, or custom components.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">By adding services to the service container during startup, ASP.NET Core ensures they are available throughout the lifetime of the application, making it easier to manage dependencies and promote modularity.<\/span><\/p>\n<p><b>Configure Method<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The <\/span><span style=\"font-weight: 400;\">Configure<\/span><span style=\"font-weight: 400;\"> method sets up the middleware pipeline that handles HTTP requests and responses. Middleware components are executed in the order they are added here, allowing you to control how requests are processed.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Typical middleware components include error handling, static file serving, routing, authentication, and custom logic. Configuring this pipeline carefully is critical to building performant and secure web applications.<\/span><\/p>\n<p><b>Example of a Basic Startup Class<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Here is a simple example demonstrating the <\/span><span style=\"font-weight: 400;\">Startup<\/span><span style=\"font-weight: 400;\"> class configuration:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public class Startup<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public void ConfigureServices(IServiceCollection services)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Register application services here<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public void Configure(IApplicationBuilder app, IWebHostEnvironment env)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (env.IsDevelopment())<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0app.UseDeveloperExceptionPage();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0app.Run(async context =&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0await context.Response.WriteAsync(&#171;Hello World!&#187;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0});<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This example shows a minimal setup where the application responds with &#171;Hello World!&#187; to every request and displays detailed error pages in development mode.<\/span><\/p>\n<p><b>Middleware in ASP.NET Core<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Middleware is are components that form the request processing pipeline in ASP.NET Core applications. Each middleware component can inspect, modify, or short-circuit the HTTP requests and responses as they pass through.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Middleware plays a key role in handling tasks such as:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Authentication and authorization<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Error handling and diagnostics<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Serving static files<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Routing requests to controllers or endpoints<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Middleware is designed as a chain of delegates, each calling the next in line, allowing granular control over the request lifecycle.<\/span><\/p>\n<p><b>Handling Errors and Exceptions<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Error handling is a fundamental part of any web application. ASP.NET Core uses middleware components to catch exceptions and provide appropriate responses.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, in a development environment, the <\/span><span style=\"font-weight: 400;\">DeveloperExceptionPage<\/span><span style=\"font-weight: 400;\"> middleware displays detailed exception information to help developers debug errors quickly. In production, custom error-handling middleware can be configured to show user-friendly error pages without exposing sensitive details.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Using middleware for error handling ensures that exceptions are processed consistently and that the application can recover gracefully from unexpected errors.<\/span><\/p>\n<p><b>Deep Dive into ASP.NET Core Project Structure<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Understanding the project structure in ASP.NET Core is essential for effective development and maintenance. When you create a new ASP.NET Core application, the default template sets up a clean and organized folder hierarchy and configuration files that form the backbone of your application.<\/span><\/p>\n<p><b>Key Files and Folders in ASP.NET Core Projects<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>wwwroot Folder<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\"> This is the default location for all static files such as HTML, CSS, JavaScript, and images. By convention, ASP.NET Core serves files only from this folder directly to the client. Any file placed outside this folder requires explicit handling to be accessible.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Program.cs<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\"> In recent ASP.NET Core versions, this file contains the entry point of the application and is responsible for setting up the web host and running the application. It configures server settings, logging, and other essential infrastructure.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Startup.cs<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\"> As described earlier, this file defines the <\/span><span style=\"font-weight: 400;\">Startup<\/span><span style=\"font-weight: 400;\"> class, responsible for configuring services and the HTTP request pipeline via its two core methods, <\/span><span style=\"font-weight: 400;\">ConfigureServices<\/span><span style=\"font-weight: 400;\"> and <\/span><span style=\"font-weight: 400;\">Configure<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>appsettings.json<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\"> This file contains configuration settings for the application in JSON format, such as database connection strings, API keys, and custom options. ASP.NET Core supports hierarchical configuration, enabling overrides from environment-specific files such as <\/span><span style=\"font-weight: 400;\">appsettings.Development.json<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Properties Folder<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\"> This contains project-related metadata files, including the launch settings file that defines how the application should be run locally or in debug mode.<\/span><\/li>\n<\/ul>\n<p><b>Modularity and Dependency Management<\/b><\/p>\n<p><span style=\"font-weight: 400;\">One of the advantages of ASP.NET Core\u2019s architecture is its modular design. Instead of being forced to load large monolithic libraries, developers can selectively include only the packages needed for their application. This reduces overhead, improves performance, and simplifies application updates.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Dependency management in ASP.NET Core is handled through NuGet packages. Developers list required packages in the project file (<\/span><span style=\"font-weight: 400;\">.csproj<\/span><span style=\"font-weight: 400;\">), and the build system restores and integrates these packages into the project during compilation.<\/span><\/p>\n<p><b>Global.json and SDK Version Control<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The <\/span><span style=\"font-weight: 400;\">global.json<\/span><span style=\"font-weight: 400;\"> file helps lock the .NET SDK version used by your project, ensuring consistency across development and deployment environments. This is especially important in teams or CI\/CD pipelines where multiple SDK versions may be installed.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">A typical <\/span><span style=\"font-weight: 400;\">global.json<\/span><span style=\"font-weight: 400;\"> looks like this:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">json<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0&#171;sdk&#187;: {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&#171;version&#187;: &#171;7.0.100&#187;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This ensures that the specified SDK version is used when building and running the project, preventing unexpected behavior caused by SDK mismatches.<\/span><\/p>\n<p><b>Detailed Explanation of Middleware Components<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Middleware is a critical concept in ASP.NET Core that shapes the way HTTP requests and responses are handled. Middleware components are chained together in the order they are registered in the <\/span><span style=\"font-weight: 400;\">Configure<\/span><span style=\"font-weight: 400;\"> method of the <\/span><span style=\"font-weight: 400;\">Startup<\/span><span style=\"font-weight: 400;\"> class.<\/span><\/p>\n<p><b>Commonly Used Middleware Components<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Static File Middleware<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\"> Serves static content such as CSS, JavaScript, and image files from the <\/span><span style=\"font-weight: 400;\">wwwroot<\/span><span style=\"font-weight: 400;\"> folder without involving MVC or Razor processing.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Routing Middleware<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\"> Determines how incoming requests are matched to endpoints in the application, such as controllers or Razor Pages.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Authentication Middleware<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\"> Verifies user credentials and establishes a user identity within the application.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Authorization Middleware<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\"> Checks if the authenticated user has permission to access the requested resources.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Exception Handling Middleware<\/b><b><br \/>\n<\/b><span style=\"font-weight: 400;\"> Intercepts exceptions thrown during request processing and provides customized error responses.<\/span><\/li>\n<\/ul>\n<p><b>Creating Custom Middleware<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Developers can create custom middleware to handle specific requirements. A middleware component is a class that accepts an <\/span><span style=\"font-weight: 400;\">HttpContext<\/span><span style=\"font-weight: 400;\"> and either processes the request or passes it to the next middleware.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example of simple custom middleware:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public class CustomMiddleware<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private readonly RequestDelegate _next;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public CustomMiddleware(RequestDelegate next)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_next = next;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public async Task InvokeAsync(HttpContext context)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Logic before passing the request<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0await context.Response.WriteAsync(&#171;Custom Middleware Processing\\n&#187;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Call the next middleware in the pipeline<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0await _next(context);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Logic after the next middleware has processed<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To register this middleware, add it in the <\/span><span style=\"font-weight: 400;\">Configure<\/span><span style=\"font-weight: 400;\"> method:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">app.UseMiddleware&lt;CustomMiddleware&gt;();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This modular design allows developers to cleanly insert custom processing logic anywhere in the request pipeline.<\/span><\/p>\n<p><b>Advanced Error Handling Techniques in ASP.NET Core<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Robust error handling is essential for building resilient web applications. ASP.NET Core leverages middleware and configuration options to provide detailed diagnostics during development and user-friendly error pages in production.<\/span><\/p>\n<p><b>Developer Exception Page<\/b><\/p>\n<p><span style=\"font-weight: 400;\">In development environments, enabling the <\/span><span style=\"font-weight: 400;\">DeveloperExceptionPage<\/span><span style=\"font-weight: 400;\"> middleware presents detailed exception stack traces, source code lines, and debugging information. This feature helps developers quickly identify and fix issues.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">if (env. IsDevelopment())<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0app.UseDeveloperExceptionPage();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><b>Custom Error Pages and Status Code Handling<\/b><\/p>\n<p><span style=\"font-weight: 400;\">In production, exposing detailed error information is a security risk. Instead, developers can configure custom error-handling middleware to show user-friendly messages.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, to handle HTTP status codes such as 404 (Not Found), use:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">app.UseStatusCodePagesWithReExecute(&#171;\/Error\/{0}&#187;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This directs requests that result in an error status to a dedicated error-handling endpoint or page.<\/span><\/p>\n<p><b>Exception Handling Middleware<\/b><\/p>\n<p><span style=\"font-weight: 400;\">ASP.NET Core includes <\/span><span style=\"font-weight: 400;\">UseExceptionHandler<\/span><span style=\"font-weight: 400;\">, which catches exceptions globally and allows redirection to error-handling logic.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">app.UseExceptionHandler(&#171;\/Home\/Error&#187;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Developers can create an error controller or Razor Page at <\/span><span style=\"font-weight: 400;\">\/Home\/Error<\/span><span style=\"font-weight: 400;\"> to display a custom error message.<\/span><\/p>\n<p><b>Logging and Diagnostics<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Integrating logging frameworks such as Microsoft.Extensions.Logging allows capturing error information in files, databases, or remote monitoring services.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example of injecting a logger into the <\/span><span style=\"font-weight: 400;\">Startup<\/span><span style=\"font-weight: 400;\"> class:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public void Configure(IApplicationBuilder app, ILogger&lt;Startup&gt; logger)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0app.Use(async (context, next) =&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0try<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0await next.Invoke();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Catch (Exception ex)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Logger.LogError(ex, &#171;An error occurred processing the request.&#187;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throw;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0});<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Logging combined with middleware ensures thorough error monitoring and troubleshooting capabilities.<\/span><\/p>\n<p><b>Understanding Static Files in ASP.NET Core<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Static files such as CSS, JavaScript, and images play a vital role in web applications by providing styles, scripts, and media content to clients.<\/span><\/p>\n<p><b>Serving Static Files<\/b><\/p>\n<p><span style=\"font-weight: 400;\">ASP.NET Core serves static files through the Static Files Middleware. By default, this middleware looks for files within the <\/span><span style=\"font-weight: 400;\">wwwroot<\/span><span style=\"font-weight: 400;\"> folder and serves them directly.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To enable static file serving, add the following to your <\/span><span style=\"font-weight: 400;\">Configure<\/span><span style=\"font-weight: 400;\"> method:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">app.UseStaticFiles();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Any file placed inside <\/span><span style=\"font-weight: 400;\">wwwroot<\/span><span style=\"font-weight: 400;\"> becomes accessible via a URL path matching its relative location.<\/span><\/p>\n<p><b>Customizing Static File Settings<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Developers can customize static file serving behavior, such as changing the default folder or setting cache control headers.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example to serve static files from a custom folder:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">app.UseStaticFiles(new StaticFileOptions<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0FileProvider = new PhysicalFileProvider(<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Path.Combine(Directory.GetCurrentDirectory(), &#171;MyStaticFiles&#187;)),<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0RequestPath = &#171;\/StaticFiles&#187;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">});<\/span><\/p>\n<p><span style=\"font-weight: 400;\">With this setup, files in the <\/span><span style=\"font-weight: 400;\">MyStaticFiles<\/span><span style=\"font-weight: 400;\"> folder are accessible under the <\/span><span style=\"font-weight: 400;\">\/StaticFiles<\/span><span style=\"font-weight: 400;\"> URL segment.<\/span><\/p>\n<p><b>Security Considerations<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Only trusted content should be placed in static file folders. Sensitive files should be excluded from static serving or protected via authorization middleware to prevent unintended exposure.<\/span><\/p>\n<p><b>Setting Up MVC in ASP.NET Core<\/b><\/p>\n<p><span style=\"font-weight: 400;\">ASP.NET Core supports the Model-View-Controller (MVC) design pattern, which separates concerns to create maintainable and testable web applications.<\/span><\/p>\n<p><b>Installing MVC Packages<\/b><\/p>\n<p><span style=\"font-weight: 400;\">To enable MVC functionality in a new project, install the necessary package via NuGet. For example, using the command line:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">dotnet add package Microsoft.AspNetCore.Mvc<\/span><\/p>\n<p><b>Registering MVC Services<\/b><\/p>\n<p><span style=\"font-weight: 400;\">After installation, register MVC services inside the <\/span><span style=\"font-weight: 400;\">ConfigureServices<\/span><span style=\"font-weight: 400;\"> method:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public void ConfigureServices(IServiceCollection services)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0services.AddControllersWithViews();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This makes controller and view services available throughout the application.<\/span><\/p>\n<p><b>Adding MVC Middleware<\/b><\/p>\n<p><span style=\"font-weight: 400;\">In the <\/span><span style=\"font-weight: 400;\">Configure<\/span><span style=\"font-weight: 400;\"> method, add MVC routing middleware:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">app.UseRouting();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">app.UseEndpoints(endpoints =&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Endpoints.MapControllerRoute(<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0name: &#171;default&#187;,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0pattern: &#171;{controller=Home}\/{action=Index}\/{id?}&#187;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">});<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This sets up the routing system and defines the default route pattern.<\/span><\/p>\n<p><b>MVC Design Pattern Explained<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The MVC design pattern divides the application into three interconnected components:<\/span><\/p>\n<p><b>Model<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The model represents the data and business logic of the application. It consists of classes that define data structures, validation rules, and interaction with the database.<\/span><\/p>\n<p><b>View<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The view defines the user interface and presentation layer. It is responsible for displaying data to users and capturing user input. Views in ASP.NET Core MVC are typically implemented using Razor syntax, which allows mixing HTML with C# code.<\/span><\/p>\n<p><b>Controller<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Controllers handle user input and application logic. They receive HTTP requests, interact with models, and select views to render responses.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">By separating concerns, MVC enhances maintainability and scalability.<\/span><\/p>\n<p><b>Routing in ASP.NET Core<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Routing is the mechanism that maps incoming HTTP requests to the appropriate controller actions or endpoints.<\/span><\/p>\n<p><b>Convention-Based Routing<\/b><\/p>\n<p><span style=\"font-weight: 400;\">In convention-based routing, routes are defined based on URL patterns that follow established conventions. The routing system matches URLs to controllers and actions based on predefined patterns.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">endpoints.MapControllerRoute(<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0name: &#171;default&#187;,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0pattern: &#171;{controller=Home}\/{action=Index}\/{id?}&#187;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This means the URL <\/span><span style=\"font-weight: 400;\">\/Products\/Details\/5<\/span><span style=\"font-weight: 400;\"> would invoke the <\/span><span style=\"font-weight: 400;\">Details<\/span><span style=\"font-weight: 400;\"> action method of the <\/span><span style=\"font-weight: 400;\">ProductsController<\/span><span style=\"font-weight: 400;\"> with <\/span><span style=\"font-weight: 400;\">id<\/span><span style=\"font-weight: 400;\"> equal to 5.<\/span><\/p>\n<p><b>Attribute Routing<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Attribute routing allows developers to define routes directly on controllers and action methods using attributes.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">[Route(&#171;products&#187;)]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public class ProductsController: Controller<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0[Route(&#171;details\/{id}&#187;)]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public IActionResult Details(int id)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Action logic here<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Attribute routing provides more flexibility and fine-grained control over URL patterns.<\/span><\/p>\n<p><b>Route Evaluation Order<\/b><\/p>\n<p><span style=\"font-weight: 400;\">When both convention-based and attribute routing are used, attribute routes are evaluated first. Understanding the order is crucial to avoid conflicts and unexpected behavior.<\/span><\/p>\n<p><b>Controllers in ASP.NET Core MVC<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Controllers are the heart of the MVC pattern and serve as intermediaries between the view (UI) and the model (data\/business logic). They process incoming HTTP requests, handle user inputs, retrieve or manipulate data, and return appropriate responses.<\/span><\/p>\n<p><b>Anatomy of a Controller<\/b><\/p>\n<p><span style=\"font-weight: 400;\">A typical controller class in ASP.NET Core MVC:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">using Microsoft.AspNetCore.Mvc;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public class HomeController: Controller<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public IActionResult Index()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return View();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public IActionResult About()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ViewData[&#171;Message&#187;] = &#171;Your application description page.&#187;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return View();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Controllers inherit from the <\/span><span style=\"font-weight: 400;\">Controller<\/span><span style=\"font-weight: 400;\"> base class, which provides many helper methods and properties to facilitate HTTP responses.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Each public method in a controller is called an action method.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Action methods return an <\/span><span style=\"font-weight: 400;\">IActionResult<\/span><span style=\"font-weight: 400;\"> (or a type derived from it), which represents the HTTP response.<\/span><\/li>\n<\/ul>\n<p><b>Action Methods and Their Execution<\/b><\/p>\n<p><span style=\"font-weight: 400;\">When a request matches a controller and action method route, ASP.NET Core MVC invokes the action method. During invocation, model binding occurs, where parameters are automatically filled from request data such as query strings, form data, or route values.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example of action with parameters:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public IActionResult Details(int id)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0var product = _productService.GetById(id);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0if (product == null)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return NotFound();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return View(product);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here, the <\/span><span style=\"font-weight: 400;\">id<\/span><span style=\"font-weight: 400;\"> is bound from the URL or request and used to fetch data.<\/span><\/p>\n<p><b>Controller Base Class Features<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The <\/span><span style=\"font-weight: 400;\">Controller<\/span><span style=\"font-weight: 400;\"> class exposes useful members such as:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">View()<\/span><span style=\"font-weight: 400;\">: Returns a <\/span><span style=\"font-weight: 400;\">ViewResult<\/span><span style=\"font-weight: 400;\"> that renders a view.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Redirect()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">RedirectToAction()<\/span><span style=\"font-weight: 400;\">: Issues HTTP redirects.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">JSON ()<\/span><span style=\"font-weight: 400;\">: Returns JSON-formatted data.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">BadRequest()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">NotFound()<\/span><span style=\"font-weight: 400;\">: Returns common HTTP status code responses.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">ModelState<\/span><span style=\"font-weight: 400;\">: Holds validation state for incoming data.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Using these, developers can easily generate varied responses depending on the request and data state.<\/span><\/p>\n<p><b>Action Results in ASP.NET Core MVC<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Action results encapsulate the response returned from a controller action. ASP.NET Core MVC provides multiple built-in action result types, allowing diverse HTTP responses beyond just HTML views.<\/span><\/p>\n<p><b>Common Action Result Types<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>ViewResult<\/b><span style=\"font-weight: 400;\">: Renders an HTML view.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>JsonResult<\/b><span style=\"font-weight: 400;\">: Returns JSON data, useful for APIs or AJAX.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>ContentResult<\/b><span style=\"font-weight: 400;\">: Returns raw string content.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>RedirectResult<\/b><span style=\"font-weight: 400;\">: Redirects to a URL.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>FileResult<\/b><span style=\"font-weight: 400;\"> and derived types: Returns files (streams, paths, or byte arrays).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>StatusCodeResult<\/b><span style=\"font-weight: 400;\">: Returns specific HTTP status codes.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>EmptyResult<\/b><span style=\"font-weight: 400;\">: Returns no content.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Example usage:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public IActionResult GetJsonData()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0var data = new { Name = &#171;Sample&#187;, Value = 123 };<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return Json(data);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This action returns a JSON object serialized from the anonymous type.<\/span><\/p>\n<p><b>Custom Action Results<\/b><\/p>\n<p><span style=\"font-weight: 400;\">You can create custom action result classes by implementing <\/span><span style=\"font-weight: 400;\">IActionResult<\/span><span style=\"font-weight: 400;\"> or inheriting from existing base classes to handle specific response logic.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example of a simple custom action result:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public class PlainTextResult: IActionResult<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private readonly string _content;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public PlainTextResult(string content)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_content = content;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public async Task ExecuteResultAsync(ActionContext context)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var response = context.HttpContext.Response;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0response.ContentType = &#171;text\/plain&#187;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0await response.WriteAsync(_content);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Usage inside a controller:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public IActionResult GetPlainText()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return new PlainTextResult(&#171;This is plain text.&#187;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><b>Views in ASP.NET Core MVC<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Views are responsible for generating the HTML returned to clients. In ASP.NET Core MVC, views are typically Razor files with a <\/span><span style=\"font-weight: 400;\">.cshtml<\/span><span style=\"font-weight: 400;\"> extension, combining HTML markup with C# code.<\/span><\/p>\n<p><b>Razor View Engine<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Razor is a lightweight markup syntax that lets you embed C# code in HTML using <\/span><span style=\"font-weight: 400;\">@<\/span><span style=\"font-weight: 400;\"> directives. It provides a clean and expressive way to build dynamic web pages.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example Razor syntax:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">html<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;h1&gt;Welcome, @Model.UserName!&lt;\/h1&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">@if(Model.IsLoggedIn)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;p&gt;You have @Model.NotificationsCount new notifications.&lt;\/p&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">else<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;p&gt;Please log in to see your notifications.&lt;\/p&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The Razor engine processes these files at runtime or compile time and generates HTML output.<\/span><\/p>\n<p><b>Layouts and Partial Views<\/b><\/p>\n<p><span style=\"font-weight: 400;\">To maintain a consistent UI across pages, Razor supports layouts and partial views.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Layouts<\/b><span style=\"font-weight: 400;\"> define common page structure, such as headers, footers, and navigation.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Partial Views<\/b><span style=\"font-weight: 400;\"> are reusable chunks of UI, like menus or widgets.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Example layout usage:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">html<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;!DOCTYPE html&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;html&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;head&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;title&gt;@ViewData[&#171;Title&#187;] &#8212; MyApp&lt;\/title&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;\/head&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;body&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;header&gt;My Application&lt;\/header&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;main&gt;@RenderBody()&lt;\/main&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;footer&gt;\u00a9 2025&lt;\/footer&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;\/body&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;\/html&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Views specify which layout to use:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">@{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Layout = &#171;_Layout&#187;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This allows content pages to inject their content into the layout.<\/span><\/p>\n<p><b>Passing Data to Views<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Data is passed from controllers to views using:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Model<\/b><span style=\"font-weight: 400;\">: The primary data object, declared with the <\/span><span style=\"font-weight: 400;\">@model<\/span><span style=\"font-weight: 400;\"> directive.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>ViewData<\/b><span style=\"font-weight: 400;\">: A dictionary for loosely typed key-value pairs.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>ViewBag<\/b><span style=\"font-weight: 400;\">: Dynamic wrapper over <\/span><span style=\"font-weight: 400;\">ViewData<\/span><span style=\"font-weight: 400;\"> providing simpler syntax.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public IActionResult Index()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0var model = new UserModel { UserName = &#171;Alice&#187;, IsLoggedIn = true };<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0ViewData[&#171;Message&#187;] = &#171;Welcome!&#187;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return View(model);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In the view:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">@model UserModel<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;h2&gt;@ViewData[&#171;Message&#187;]&lt;\/h2&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;p&gt;Hello, @Model.UserName&lt;\/p&gt;<\/span><\/p>\n<p><b>Working with DBContext and Entity Framework Core<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Data access is a fundamental part of web applications. ASP.NET Core uses Entity Framework Core (EF Core) as an object-relational mapper (ORM) to interact with databases using .NET objects.<\/span><\/p>\n<p><b>DBContext Class<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The <\/span><span style=\"font-weight: 400;\">DbContext<\/span><span style=\"font-weight: 400;\"> is the primary class coordinating EF Core functionality, including querying, saving, and configuring entities.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Define a custom context by inheriting from <\/span><span style=\"font-weight: 400;\">DbContext<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public class ApplicationDbContext: DbContext<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0: base(options)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public DbSet&lt;Product&gt; Products { get; set; }<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here, <\/span><span style=\"font-weight: 400;\">Products<\/span><span style=\"font-weight: 400;\"> represents a database table mapped to the <\/span><span style=\"font-weight: 400;\">Product<\/span><span style=\"font-weight: 400;\"> entity class.<\/span><\/p>\n<p><b>Configuring DbContext in Startup<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Register the context with the dependency injection container in <\/span><span style=\"font-weight: 400;\">ConfigureServices<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0options.UseSqlServer(Configuration.GetConnectionString(&#171;DefaultConnection&#187;)));<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This enables the context to be injected into controllers or services.<\/span><\/p>\n<p><b>Using DbContext in Controllers<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Example of injecting <\/span><span style=\"font-weight: 400;\">ApplicationDbContext<\/span><span style=\"font-weight: 400;\"> in a controller:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public class ProductsController: Controller<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private readonly ApplicationDbContext _context;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public ProductsController(ApplicationDbContext context)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_context = context;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public IActionResult Index()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var products = _context.Products.ToList();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return View(products);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This fetches all products from the database and passes them to the view.<\/span><\/p>\n<p><b>Entity Configuration and Migrations<\/b><\/p>\n<p><span style=\"font-weight: 400;\">EF Core supports configuring entities using data annotations or Fluent API for advanced mappings.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">modelBuilder.Entity&lt;Product&gt;()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0.Property(p =&gt; p.Name)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0.IsRequired()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0.HasMaxLength(100);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Database schema changes are managed via migrations:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Add migration: <\/span><span style=\"font-weight: 400;\">dotnet ef migrations add InitialCreate<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Update database: <\/span><span style=\"font-weight: 400;\">dotnet ef database update<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Migrations keep your database schema in sync with your model changes.<\/span><\/p>\n<p><b>Razor Syntax and Features in Detail<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The Razor syntax simplifies combining server-side logic with HTML markup.<\/span><\/p>\n<p><b>Razor Directives<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Some common Razor directives:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">@model<\/span><span style=\"font-weight: 400;\">: Specifies the type of the model passed to the view.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">@using<\/span><span style=\"font-weight: 400;\">: Imports namespaces.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">@inject<\/span><span style=\"font-weight: 400;\">: Injects services into the view.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">@section<\/span><span style=\"font-weight: 400;\">: Defines content sections for layouts.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">@functions<\/span><span style=\"font-weight: 400;\">: Adds C# methods or properties within the view.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">@model Product<\/span><\/p>\n<p><span style=\"font-weight: 400;\">@functions {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0string FormatPrice(decimal price) =&gt; price.ToString(&#171;C&#187;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;h2&gt;@Model.Name&lt;\/h2&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;p&gt;Price: @FormatPrice(Model.Price)&lt;\/p&gt;<\/span><\/p>\n<p><b>Conditional Statements and Loops<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Razor supports typical C# control flow:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">@if (Model.Stock &gt; 0)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;p&gt;In Stock&lt;\/p&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">else<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;p&gt;Out of Stock&lt;\/p&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;ul&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">@foreach(var item in Model.Reviews)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;li&gt;@item.Comment&lt;\/li&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;\/ul&gt;<\/span><\/p>\n<p><b>HTML Helpers and Tag Helpers<\/b><\/p>\n<p><span style=\"font-weight: 400;\">HTML helpers are C# methods that generate HTML markup, for example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">@Html.TextBoxFor(model =&gt; model.Name)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">@Html.ValidationMessageFor(model =&gt; model.Name)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Tag Helpers provide a more HTML-like syntax and are recommended in newer projects:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">html<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;input asp-for=&#187;Name&#187; class=&#187;form-control&#187; \/&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;span asp-validation-for=&#187;Name&#187; class=&#187;text-danger&#187;&gt;&lt;\/span&gt;<\/span><\/p>\n<p><b>Advanced Routing Concepts in ASP.NET Core<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Routing flexibility is critical to building RESTful and user-friendly web applications.<\/span><\/p>\n<p><b>Route Constraints<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Route constraints restrict route parameters to specific data types or patterns.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">endpoints.MapControllerRoute(<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0name: &#171;product&#187;,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0pattern: &#171;products\/{id:int}&#187;,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0defaults: new { controller = &#171;Products&#187;, action = &#171;Details&#187; });<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This route matches only if the <\/span><span style=\"font-weight: 400;\">id<\/span><span style=\"font-weight: 400;\"> parameter is an integer.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Custom constraints can also be implemented by extending <\/span><span style=\"font-weight: 400;\">IRouteConstraint<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><b>Route Prefixes and Areas<\/b><\/p>\n<p><span style=\"font-weight: 400;\">To organize large applications, you can use route prefixes or areas.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Route Prefixes<\/b><span style=\"font-weight: 400;\"> apply a common prefix to all routes in a controller.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">[Route(&#171;admin\/[controller]\/[action]&#187;)]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public class UsersController: Controller<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public IActionResult Index() { &#8230; }<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Areas<\/b><span style=\"font-weight: 400;\"> provide logical grouping with separate route tables.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Define an area:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">csharp<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CopyEdit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">[Area(&#171;Admin&#187;)]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public class DashboardController: Controller<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public IActionResult Index() { &#8230; }<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Configure area routing:<\/span><\/p>\n<p><b>Final Thoughts<\/b><\/p>\n<p><span style=\"font-weight: 400;\">ASP.NET Core is a powerful, flexible, and modern framework designed for building web applications that run cross-platform with high performance. It represents a significant evolution from the traditional ASP.NET framework by embracing open-source principles, modular design, and cloud readiness.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The framework\u2019s core features, such as dependency injection, middleware pipeline, configuration, and logging, provide developers with fine-grained control and extensibility. The MVC pattern integrated within ASP.NET Core facilitates clear separation of concerns, making code more maintainable and testable. Controllers process requests and return appropriate responses through action results, while views, powered by the Razor engine, enable dynamic and clean UI rendering.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Routing plays a crucial role in directing HTTP requests to the correct endpoints, supporting both convention-based and attribute-based approaches. Advanced routing capabilities such as constraints, areas, and custom route handlers help organize complex applications and create clean URL structures.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Entity Framework Core seamlessly integrates data access with the web layer, allowing developers to work with databases using strongly typed C# objects and LINQ queries. With migrations and fluent configuration, managing the database schema becomes straightforward and aligned with application evolution.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Overall, ASP.NET Core offers a comprehensive ecosystem for modern web development that balances performance, scalability, and developer productivity. Mastering its key concepts \u2014 middleware, MVC, routing, data access, and Razor views \u2014 equips developers to build robust, maintainable, and responsive web applications suited to today\u2019s diverse platforms and cloud environments.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET Core is a modern, open-source, and cloud-optimized web framework developed by Microsoft for building web applications. Unlike the traditional ASP.NET framework that was tied exclusively to the Windows platform, ASP.NET Core is cross-platform, meaning it can run on Windows, macOS, and Linux. This framework was introduced as a redesign of the original ASP.NET to provide better flexibility, higher performance, and more modularity to support various application types across different platforms. ASP.NET Core replaced the older .NET Framework-based ASP.NET by focusing on a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1049,1053],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/posts\/961"}],"collection":[{"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/comments?post=961"}],"version-history":[{"count":3,"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/posts\/961\/revisions"}],"predecessor-version":[{"id":9909,"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/posts\/961\/revisions\/9909"}],"wp:attachment":[{"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/media?parent=961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/categories?post=961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.certbolt.com\/certification\/wp-json\/wp\/v2\/tags?post=961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}