• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Sunday, August 23, 2020

Controllers in ASP.NET Core MVC

 Admin     August 23, 2020     .Net, .Net Core, .Net Core MVC, Asp.Net, C#     No comments   

In this article, I am going to discuss the Controllers in ASP.NET Core MVC application with an example. Please read our previous article before proceeding to this article where we discussed ASP.NET Core Dependency Injection with an example. As part of this article, we are going to discuss the following pointers.
  1. What are Controllers in ASP.NET Core MVC Application?
  2. What are action Methods?
  3. How to return JSON Data from Controller Action Method?
  4. Returning ObjectResult from Controller Action Method in ASP.NET Core MVC Application?
  5. How to return a View from the Controller Action Method?
What are Controllers in ASP.NET Core MVC Application?
The Controllers in ASP.NET Core MVC application are the classes having a set of public methods. These public methods are called as actions (action methods). These action methods are the methods of a Controller which is actually going to handle the incoming HTTP Requests.

The Controllers in MVC application logically group similar types of actions together. This aggregation of actions or grouping similar types of action together allows us to define sets of rules such as caching, routing, and authorization which is going to be applied collectively.
Controllers in ASP.NET Core MVC Application

By convention, the controller classes in ASP.NET Core MVC application should reside in the project’s root-level Controllers folder and inherits from the Microsoft.AspNetCore.Mvc.Controller base class.

We can consider a class as a Controller when at least one of the following conditions is true.
  1. The class name is suffixed with the word “Controller” or if the class inherits from a class whose name is suffixed with the word “Controller”
  2. The class is decorated with the [Controller] attribute.
Let us have a look at the following Controller:
Controllers in ASP.NET Core MVC Application
With the above Controller place in our application, whenever the user type the following URL and hit the enter key

http://localhost:xxxx/home/GetStudentDetails/102

Then the above URL “/home/GetStudentDetails/102” is mapped to the “GetStudentDetails” method of the HomeController as shown in the below image.
Mapping URL TO Actions in ASP.NET MVC Core Application

How the above mapping is done that we will discuss in our upcoming articles. For now, just understand that the above mapping is done by the routing rules which are defined for your application.

In ASP.NET Core MVC the action methods of a controller can return different types of data such as JSON, View, String, Object, XML, etc.

Controller Action Method Returning JSON Data:
In the below example, the GetStudentDetails action method always going to returns the data in JSON format irrespective of the content negotiation. This is because of the return type of the GetStudentDetails() method which is set to JsonResult. In this case, it is going to ignore the Accept Header values.
Controller Action Method Returning JSON Data

Controller Action Method returning ObjectResult:
In the following example, it looks for the Accept Header value and if the value is set to application/xml, then it returns the data in XML format whereas if the value is set to application/json, then the data is going to return in JSON format.
Controller Action Method returning ObjectResult

Note: In order to return XML data from an action method, you need to register the XML Serializer Formatter by calling the AddXmlSerializerFormatters() method within the ConfigureServices() method of the Startup.cs class as shown in the below image.
Adding XML Serializer in MVC Core

Controller Action Method returning View:
In order to return a view from an action method in ASP.NET Core MVC, you need to use ViewResult as the return type of the action method. In the following example, the GetStudentDetails action method is going to return a View as the return of the method is set to ViewResult.
Controller Action Method returning View

At this point, if you run the application and navigate to “/home/GetStudentDetails/102” URL, then you will get the following error.
Controller Action Method returning View Error

The reason for the above error is we do not have the required View file in our application. In the next article, I am going to discuss the Views in the ASP.NET Core MVC application. In this article, I try to explain Controllers in the ASP.NET Core MVC application. I hope you enjoy this article.

Summary:
I hope this post will be helpful to understand the concept of Controllers in ASP.NET Core MVC
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape