Controllers in ASP.NET Core MVC

Introduction

In this article, we’ll discuss Controllers in MVC .NET framework. If you choose the MVC framework then everything is a result of an action and actions are categorizing in controllers. Let’s see it in our application. Please, read our previous article before proceeding to this article where we learn Dependency Injection in ASP.NET Core.

So, we open the solution explorer we have the controllers for it. we have one default controller here which is called HomeController.

controller in asp.net core

The first thing, we can see the default Home controller is the class in the controller class that means and action inside a controller. And as you can see this default Test controller And the index is the default action.

Add Controllers

Let’s right-click on controllers for their Then select Add controller.

add Controller in asp.net core

Now, we have this dialog box to define which kind of controllers we would like to create.

ap.net mvc controller template

Let’s create an empty one. Now we are asked to give it a name. In .NET MVC and we see all controllers must-have controller at the end of the name. we chose this controller.

Name To Controller in asp.net mvc

This is an index action that is created by default when we created the controller. then, we delete this one to create another action.

we create a new public method here.

The name of our action is a string out the output is a simple string and as so it returns a string value.

return String controller in asp.net mvc

So, then save and test it in the browser to test our new action.

asp.net core mvc output

So when there are a new request and the source code is changed it automatically gets compiled back to our ID.

Let’s remember something we have in our out in Startup.cs. we have an option on that segment which is called ID.

maproute in asp.net core mvc

So, let’s use it in our actions.

We can add a parameter called ID which is not case sensitive.

params in asp.net core

That type can be anything for here we chose in and we also changed the return value to include ID.

return values from controller

Let’s see, we were tested in the browser.

So, now in browse there, if we add a number and then pressing enter, We will have these result

pass queryparams in asp.net mvc

The controller returns JSON data

The following example returns JSON data. Notice, the return type of the UserInfo() method is set to JsonResult as we are explicitly returning JSON data. In this case, UserInfo() method always returns JSON data. It does not respect content negotiation and ignores the Accept Header.

public class TestController : Controller
{
    private readonly IUserRepository _UserRepository;

    public TestController(IUserRepository UserRepository)
    {
        _UserRepository = UserRepository;
    }

    public JsonResult UserInfo()
    {
        User model = _UserRepository.GetUser(1);
        return Json(model);
    }
}

Controller returns ObjectResult

The following example respects content negotiation. It shows the Request Accept Header and if it is set to application/xml, then XML data is returned. If the Accept header is set to application/JSON, then JSON daThe following example respects content negotiation. It shows at the Request Accept Header and if it is set to application/XML, then XML data is returned. If the Accept header is set to application/JSON, then JSON data is returned.

public class TestController : Controller
{
    private IUserRepository _UserRepository;

    public TestController(IUserRepository UserRepository)
    {
        _UserRepository = UserRepository;
    }

    public ObjectResult UserInfo()
    {
        User model = _UserRepository.GetUser(1);
        return new ObjectResult(model);
    }
}

Configure XML format Service

So, it returns the data in XML format and we have to add Xml Serializer Formatter by calling AddXmlSerializerFormatters() method in ConfigureServices() method in Startup.cs file.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().AddXmlSerializerFormatters();
}

Controller returns View

The below example returns a View. Notice we have set ViewResult as the return type for the UserInfo method as we are returning a view.

public class TestController : Controller
{
    private IUserRepository _UserRepository;

    public TestController(IUserRepository UserRepository)
    {
        _UserRepository = UserRepository;
    }

    public ViewResult UserInfo()
    {
        User model = _UserRepository.GetUser(1);
        return View(model);
    }
}

At this point, if we run the application and navigate to http://localhost:49119/Test/UserInfo, we get the following error. This is because we do not have the required View file created yet. We will discuss Views in MVC in our next article.

InvalidOperationException: The view ‘UserInfo’ was not found. The following locations were searched: /Views/Test/UserInfo.cshtml /Views/Shared/UserInfo.cshtml /Pages/Shared/UserInfo.cshtml data is returned.

Thank you for reading this article, I hope you will understand the Controllers in ASP.NET Core MVC. We’ll get into more details about this when we’ll learn Views in ASP.NET Core MVC in the next article.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *