Understand basic routing

How MVC routing works

MVC invokes controller classes, and the action methods within them, depending on the incoming URL. The default URL routing logic used by MVC, uses a format like this to determine what code to invoke:

/[Controller]/[ActionName]/[Parameters]

The routing format is set in the Program.cs file.

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

How did our previous experiment with URLs work?

When you browse to the app and don't supply any URL segments, it defaults to the "Home" controller and the "Index" method specified in the template line highlighted above.

In the preceding URL segments we tried on the browser:

  • The first URL segment determines the controller class to run. So, localhost:5001/HelloWorld maps to the HelloWorld Controller class.

  • The second part of the URL segment determines the action method on the class. So localhost:5001/HelloWorld/Index causes the Index method of the HelloWorldController class to run.

  • Notice that you only had to browse to localhost:5001/HelloWorld and the Index method was called by default. Index is the default method that will be called on a controller if a method name isn't explicitly specified.

  • The third part of the URL segment ( id) is for route data. Route data is explained later.

When you browse to the app and don't supply any URL segments, it defaults to the "Home" controller

Index is the default method that will be called on a controller if a method name isn't explicitly specified.

Let's invoke welcome method

Browse to: https://localhost:{PORT}/HelloWorld/Welcome. Replace {PORT} with your port number.

The Welcome method runs and returns the string This is the Welcome action method.... For this URL, the controller is HelloWorld and Welcome is the action method.

We have NOT used Parameters as defined in our URL template!

Let's provision for passing parameters

Modify the code to pass some parameter information from the URL to the controller. For example, /HelloWorld/Welcome?name=Rick&numtimes=4.

Change the Welcome method to include two parameters as shown in the following code.

// GET: /HelloWorld/Welcome/ 
// Requires using System.Text.Encodings.Web;
public string Welcome(string name, int numTimes = 1)
{
    return HtmlEncoder.Default.Encode($"Hello {name}, NumTimes is: {numTimes}");
}

The preceding code:

  • Uses the C# optional-parameter feature to indicate that the numTimes parameter defaults to 1 if no value is passed for that parameter.

  • Uses HtmlEncoder.Default.Encode to protect the app from malicious input, such as through JavaScript.

  • Uses Interpolated Strings in $"Hello {name}, NumTimes is: {numTimes}".

Run the app and browse to: https://localhost:{PORT}/HelloWorld/Welcome?name=Rick&numtimes=4. Replace {PORT} with your port number.

Try different values for name and numtimes in the URL. The MVC model binding system automatically maps the named parameters from the query string to parameters in the method. We will discuss this later.

In the previous image:

  • The name and numTimes parameters are passed in the query string.

  • The ? (question mark) in the above URL is a separator, and the query string follows.

  • The & character separates field-value pairs.

We have still NOT used Parameters as defined in our URL template!

Let's use parameters finally

Replace the Welcome method with the following code:

public string Welcome(string name, int ID = 1)
{
    return HtmlEncoder.Default.Encode($"Hello {name}, ID: {ID}");
}

Run the app and enter the following URL: https://localhost:{PORT}/HelloWorld/Welcome/3?name=Rick

In the preceding URL:

  • The third URL segment matched the route parameter id.

  • The Welcome method contains a parameter id that matched the URL template in the MapControllerRoute method.

  • The trailing ? starts the query string.

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

In the preceding example:

  • The third URL segment matched the route parameter id.

  • The Welcome method contains a parameter id that matched the URL template in the MapControllerRoute method.

  • The trailing ? (in id?) indicates the id parameter is optional.

Last updated