Now we will talk about making new route in ASP.NET MVC. In the other article we have talked about modify/change the default route in ASP.NET MVC.
To create new route, open file RouteConfig.cs in App_Start folder

M. Faisol Riza - ASP.net MVC custom route

We can see the default route in RouteConfig.cs. Now we will add new route. Before that, we must remember that the ordering we write the code for route will affect how application work.

Example for new route :

routes.MapRoute(
     name: "Country", //Route name
     url: "Country/{name}", //Route url
     defaults: new { controller="Country", action="Search"}
);

Then in controller folder, create new controller called “CountryController”. Add action called “Search” with string parameter called “name”

public ActionResult Search(string name)
{
     return Content("This is Country controller "+name);
}

Run the application (F5). On the url, add “/Country/Indonesia”. Then we will seeM Faisol Riza love Indonesia

Thank You. Have a nice day ^_^

Tinggalkan komentar

I’m Riza

Welcome to my website. I’m happy to share my knowledge and experience here, especially related with my expertise in digital solution for enterprise. I’ll talk many topics around technology, AI, and others. Happy to know you!

You can find me in network platform below

Let’s connect