ASP.NET Core – MVC – Bootstrap – Responsive Web Programming – Lecture 3: More Controllers & Views



ASP.NET Core – MVC – Bootstrap – Responsive Web Programming – Lecture 3: More Controllers & Views

ASP.NET Core - MVC - Bootstrap - Responsive Web Programming - Lecture 3: More Controllers & Views

Join Discord For Help : https://bit.ly/CodeInNetDiscord. Lecture 3/17 of ASP.NET Core MVC with C# Programming Full Course. More Controllers & Views. If I have been of assistance to you and you would like to show your support for my work, please consider becoming a patron on 🥰 https://www.patreon.com/SECourses

Source code repository of this Course:
https://github.com/FurkanGozukara/Responsive-Web-Design-With-ASP.NET-5-and-MVC-Pattern-and-BootStrap-5-2021

Playlist of this course: ASP.NET Core MVC Full Course – C# And BootStrap Version – Responsive Web #PROGRAMMING With Tutorials 👨‍💻 (FREE) – 2023:
https://www.youtube.com/playlist?list=PLdCdTIJgqkdFQ8pEZLZLsQIbze1KNEqWA,

#ASPNETCORE #WebDevelopment

In Lecture 3:
* How to write a custom route path for default controller in an ASP.NET Core MVC application
* How to call multiple different Views from single controller
* What are actions of controllers and how to use actions
* Different action results
* How to return file or different HTTP status code from Views
* How to do a redirect from View action calls
* How pass data to the Views from controllers with Model or ViewData or ViewBag approaches
* How to do null check to parse Model or ViewData in Views
* How to use dynamic models in Views
* What is the default compiler path checking of ASP.NET Core MVC
* How to do form post with raw HTML and Razor syntax in Views

Advanced Routing, Controllers, and Views in ASP.NET Core MVC

Introduction:

In this article, we will discuss various advanced topics related to ASP.NET Core MVC applications, focusing on custom routing, controller actions, action results, passing data to views, and form handling. These advanced techniques enable developers to build efficient, maintainable, and flexible applications with ease.

Custom Route Path for Default Controller:
To create a custom route path for the default controller in an ASP.NET Core MVC application, you can modify the default route configuration in the Startup.cs file. Instead of using the default route template, you can define a custom route pattern that suits your application’s requirements.

Calling Multiple Different Views from a Single Controller:
A single controller can return multiple views by creating different action methods, each returning a view based on specific conditions or logic. You can use the “View()” method to return a view, passing the view name as an argument.

Understanding Controller Actions:
Actions in controllers are methods that handle incoming HTTP requests and produce responses. They are responsible for executing the necessary logic to process requests, such as data retrieval or manipulation, and returning appropriate action results.

Different Action Results:
ASP.NET Core MVC provides various action results to cater to different scenarios, such as ViewResult, PartialViewResult, JsonResult, FileResult, and StatusCodeResult, among others. Each action result represents a different type of response that can be returned from an action method.

Returning File or Different HTTP Status Code from Views:
To return a file or a different HTTP status code from a view, you can utilize the appropriate action result, such as FileResult for files or StatusCodeResult for status codes. This way, you can tailor the response based on the desired outcome.

Redirecting from View Action Calls:
To redirect from a view action call, you can use the “Redirect()” or “RedirectToAction()” methods in your controller action. These methods create a new HTTP response with the appropriate status code and location header, instructing the client to navigate to a different URL.

Passing Data to Views:
Data can be passed to views from controllers using three primary approaches: Model, ViewData, and ViewBag. The Model approach relies on strongly-typed views, while ViewData and ViewBag use a dictionary-based approach for passing data.

Null Checking for Parsing Model or ViewData in Views:
To perform a null check when parsing Model or ViewData in views, you can use conditional statements such as the null-conditional operator (?.) in C# or the null-coalescing operator (??). This helps ensure that your view gracefully handles missing or null data.

Using Dynamic Models in Views:
Dynamic models can be used in views by utilizing the “dynamic” keyword in C#. This allows you to create views that can handle models with varying properties and types at runtime, providing greater flexibility.

Default Compiler Path Checking in ASP.NET Core MVC:
ASP.NET Core MVC has a built-in mechanism for locating and compiling views. By default, it checks for view files in the “Views” folder, following the convention “{Controller}/{Action}.cshtml”. If a view is not found in this location, it checks the “Shared” folder for a view with the same name.

Form Post with Raw HTML and Razor Syntax in Views:
To create a form post using raw HTML and Razor syntax, you can use the HTML form element along with the various input elements.

Comments are closed.