Controller in MVC

Controllers area unit primarily the central unit of your ASP.NET MVC application. it’s the first recipient, that interacts with incoming communications protocol Request. So, the controller decides that model are elite, so it takes the information from the model and passes a similar to the individual read, at that time read is rendered. Actually, controllers area unit dominant the general flow of the appliance taking the input and rendering the right output.

Controllers area unit C# categories inheritable from System.Web.Mvc.Controller, that is that the builtin controller base category. every public methodology in {an exceedingly|in a very} controller is understood as an action methodology, which means you’ll invoke it from the online via some URL to perform associate action.

The MVC convention is to place controllers within the Controllers folder that Visual Studio created once the project was got wind of.

Let’s take a glance at an easy example of Controller by making a replacement ASP.Net MVC project.

Step 1− Open the Visual Studio and click on on File → New → Project menu possibility.

A new Project dialog opens.

Step 2 –  From the left pane, choose Templates → Visual C# → internet.

Step 3 − In the middle pane, select ASP.NET Web Application.

Step 4 − Enter the project name ‘MVCControllerApplication’ in the Name field and click ok to continue. You will see the following dialog, which asks you to set the initial content for the ASP.NET project.

Step 5 − To keep things simple, select the Empty option and check the MVC checkbox in the ‘Add folders and core references for’ section and click Ok.

It will create a basic MVC project with minimal predefined content.

Once the project is created by Visual Studio you will see a number of files and folders displayed in the Solution Explorer window.

Since we have created ASP.Net MVC project from an empty project template, so at the moment, the application does not contain anything to run.

Step 6 − Add CustomerController by right-clicking on Controllers folder in the solution explorer. Select Add → Controller.

It will display the Add Scaffold dialog.

Step 7 − Select the MVC 5 Controller – Empty option and click ‘Add’ button.

The Add Controller dialog will appear.

Step 8 − Set the name to CustomerController and click ‘Add’ button.

You will see a replacement C# file CustomerController.cs within the Controllers folder, that is open for written material in Visual Studio in addition.

Now, during this application we are going to add a custom route for worker controller with the default Route.

Step one − move to “RouteConfig.cs” file beneath “App_Start” folder and add the subsequent route.

routes.MapRoute(

"Customer", "Customer/", new UrlParameter.Optional ">);

Following is that the complete implementation of RouteConfig.cs file.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing;

namespace MVCControllerApplication {

public category RouteConfig .axd/");

routes.MapRoute(

"Customer", "Customer/", new);

routes.MapRoute(

name: "Default", url: "//", defaults: new);

}

}

}

Consider a state of affairs whereby associatey user comes and searches for an worker, specifying the URL “Customer/Pawan”. during this case, Pawan are treated as a parameter name not like Action methodology. thus during this reasonably state of affairs our default route won’t work considerably.

To fetch the incoming worth from the browser once the parameter is obtaining passed, MVC framework provides an easy thanks to address this downside. it’s by exploitation the parameter within the Action methodology.

Step two − modification the CustomerController category exploitation the subsequent code.

using System;

using System.Collections.Generic;

using System.Linq;
using System.Web;

using System.Web.Mvc;

namespace MVCControllerApplication.Controllers {

public category CustomerController : Controller {

// GET: Customer

public ActionResult Search(string name)power unit input = Server.HtmlEncode(name);

come Content(input);

}

}

}

If you add a parameter to associate action methodology, then the MVC framework can seek for the worth that matches the parameter name. it’ll apply all the potential combination to search out out the parameter worth. it’ll search within the Route information, question string, etc.

Hence, if you request for /Customer/Pawan”, then the MVC framework can decide that i would like a parameter with “UserInput”, so Pawan can get picked from the URL which can get mechanically passed.

Server.HtmlEncode can merely convert any reasonably malicious script in plain text. once the higher than code is compiled and dead and requests the subsequent URL http://localhost:61465/Customer/Pawan, you’ll get the subsequent output.

 

Leave a Reply

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