It might seem like magic, but there is a method in .NET to how you are able to view web pages in the web browser. In this blog post, I will go over MVC routing, how it works, and why it is important. Now, let’s dive right in!
What Is MVC Routing
MVC stands for Model-View-Controller and is a programming design pattern implemented in .NET MVC. If you’re unfamiliar with this pattern, then go ahead and read an explanation about it in my earlier post here.
MVC routing is the method to how we navigate to pages in .NET MVC web applications. When the user submits a URL into the address bar of the web browser, the URL is mapped to the controller action, which returns the correct web page into the browser.
The default MVC routing pattern is Controller/Action/Id and is included as part of the source URL. The pattern maps the first segment to the controller, the second segment into the controller action, and then the third segment to the id parameter, which identifies the specific data or model.
How Does MVC Routing Work
Suppose you entered the following URL into the address bar of the web browser:
MVC routing would then map this URL into the pattern, https://localhost:1234/{controller}/{action}/{id} where the parameters are:
Controller = Home. The Home controller would handle the request.
Action = Details. The Details action or method of the the Home controller
Id = 3. This parameter is optional and is used to query the data for the specific model.
For example, in my blog application, the Blogs Details page displays the information for a blog. In order to display the blog information onto the web page, you will enter the request URL into the address bar of the web browser:
The routing of Blogs/Details/5 would work by having the Details action of the Blogs controller query the database for the blog with Id of 5. If found, the blog is returned as a Model to the View where the information is displayed in the browser as a web page.
Common Questions - MVC Routing
What happens if we don’t enter one or some of the parameters?
If you don’t enter a controller name, it defaults to the value of Home. If you don’t enter an action, it defaults to the value of Index. If you don’t enter an id, it defaults to an empty string.
How do we make changes to the MVC routing in .NET?
To make changes, you must edit the endpoint routing in the Program.cs file that comes with every MVC project file. In the following example you see the code of the default MVC routing for a sample .NET MVC project:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Conclusion - MVC Routing
So, that’s MVC routing explained in a nutshell. Basically, to request a web page in .NET, you must use parameters along with the source URL to map to the correct view.
I hope you enjoyed learning about MVC routing. If you found this article to be helpful, don’t forget to leave a comment below and subscribe to receive future updates on any new articles.