Views in Asp.net Core MVC

Views in Asp.net Core MVC
In ASP.NET Core MVC, views are responsible for rendering the user interface (UI) that your application presents to the user. They are typically HTML templates with embedded Razor syntax, which is a powerful templating engine that allows you to seamlessly blend HTML markup with C# code.
 
Notes
Dynamic Content: Views can generate dynamic content based on data passed to them from the controller (the model). This allows you to display information fetched from databases, user inputs, and other sources.
Razor Syntax: Views use Razor syntax, which starts with @ symbols, to embed C# code within the HTML. This code can perform tasks like looping through data collections, conditional rendering, and accessing model properties.
 
 
AddControllersWithViews() Method
This extension method is used to register the necessary services for MVC (models, views, controllers) with the dependency injection container. It’s a shortcut for adding multiple services at once, including:
Controller Discovery: Automatically discovers controller classes in your project.
View Engine: Configures Razor as the view engine.
Model Binding: Sets up model binding for handling form submissions.
Validation: Enables model validation.
 
 
ViewResult
The ViewResult class is an action result type in ASP.NET Core MVC that represents a view to be rendered. When a controller action returns a ViewResult, the MVC framework locates the corresponding view file, passes the model data (if any) to it, and renders the view’s content as HTML.
 
Default View Locations
ASP.NET Core MVC follows a convention for determining where to find view files:
By Convention: By default, the view engine looks for views in the Views/[ControllerName]/[ActionName].cshtml path. For example, the Index action method in the HomeController would look for a view file at Views/Home/Index.cshtml.
Overriding with ViewName: You can explicitly specify the view name using the ViewName property of the ViewResult or the View() helper method.
Shared Views: Shared views are stored in the Views/Shared folder and can be used by multiple controllers.
 
Code
// Program.cs
builder.Services.AddControllersWithViews(); // Enables MVC features
 
// …
 
 
// HomeController.cs
[Route(“home”)]
public IActionResult Index()
{
    return View(); // Renders Views/Home/Index.cshtml
}
 
// Views/Home/Index.cshtml
<!DOCTYPE html>
<html>
    <head>
        <title>Asp.Net Core App</title>
    </head>
    <body>
        Welcome
    </body>
</html>
 
Enabling MVC: The AddControllersWithViews() method configures the application for MVC, including view support.
Controller Action: The Index action method in HomeController returns a ViewResult without specifying a view name.
View Location: Since no view name is explicitly provided, the MVC framework follows the convention and looks for the view at Views/Home/Index.cshtml.
View Content: The Index.cshtml view contains simple HTML that will be rendered as the response.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply