SlideShare a Scribd company logo
1 of 18
Routing
Routing
 In ASP.NET Core MVC, routing is the process of mapping an incoming
HTTP request to a specific action method
 Before routing, URLs typically mapped to a physical file on disk
 For example, the URL ‘../products.aspx’ would map to a file named
‘products.aspx’ in the application folder
 This may be simpler than routing, but has disadvantages:
 If you change the file name, all links to the page within the application need to
be changed
 URLs exposed to users need to be changed
Benefits of routing
 Routing allows us to define URLs that are not tied to particular
files or structure of our application
 It decouples action methods from the URLs that are used to
execute them
 If you need to change URLs, you only need to modify the
routing system; action methods remain unchanged
Routing terms
 Route template
 Defines a URL pattern that is used to match against request URLs
 A template is a string that contains placeholders for parts that may vary
 For example, a route template {controller}/{action}, would map the
URL ‘www.localhost:12345.com/product/view’ to
ProductController.View() action method
Route template
 A single route template can map a number of different URLs
 For example, the same route template would map the URL
‘home/index’ to HomeController.Index() action method
 An application can have more than one routing template
Routing terms
 URL segment
 A small, contiguous section of a URL
 They are separated by a character, often by the ‘/’ character
 Routing involves matching the segments of a URL to a route template
Routing in MVC
 Two ways to define routes:
 Routing using conventions
 Routing using attributes
 In this chapter we will cover routing using conventions
Routing using conventions
 Defined in the Startup.cs file:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
 EndPointMiddleware is where you configure all the endpoints
 An endpoint is some handler that returns a response
 Route name: a name that can be used by other parts of the program
 Pattern: the route template
Defining multiple routes
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "category",
pattern: "{controller}/{action}/category{cat}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Defining multiple routes
 The order of routes is important
 It defines the order against which the routing infrastructure will attempt to
match an incoming request
 You should list the most specific routes first, in order of decreasing
specificity, until you get to the most broad/general route
Route template syntax
 It consists of segments, separated by “/’
 A segment can be
 A literal
 A route parameter
 A literal must be matched exactly by the request URL
 A parameter is a section of a URL that may vary, but still be a match for the template
 Route parameters are defined by giving them a name, and placing them in braces, such
as {controller} or {action}
How do routing templates work?
 For each defined route template, the router attempts to split the request’s URL into segments
corresponding to the template
 For example, suppose we have this template:
{controller}/{action}
 and the URL is: /products/view
 The EndPointMiddleware will split the URL into segments corresponding to the template
 In this case, the URL matches the pattern of the template
 It has 2 segments
 When there is a match, values associated with the parameters are stored in a collection associated
with the request
 In our example:
 value associated with {controller} parameter is ‘products’
 value associated with {action} parameter is ‘view’
 Then using these values, EndPointMiddleware attempts to find a controller named
‘productsController’ that contains an action named ‘view’
How do routing templates work? (cont’d)
 Supposed we have a different request URL, for example:
/products/books/view
 This URL with three segments does not match the route template
{controller}/{action}
 The EndPointMiddleware will compare it against other route templates, if
there are more
 If no match is found, a 404 response will be generated
How do routing templates work? (cont’d)
 Literal segments
 Consider the route template below, that contains a literal segment
sales/{controller}/{action}
 The URL below will be a match; it has three segments and the first is ‘sales’:
sales/products/view
 However, this other URL, will not be a match, because the first segment is not exactly ‘sales’ as
specified in the template
rentals/products/view
Default values
 Consider the template below:
{controller}/{action=Index}
 The {action} parameter has a default value specified for it, Index
 This means that if the URL doesn’t contain a segment corresponding to the action
parameter, then the router will use the value ‘Index’ instead
 For example, both these two URLs will match this template
/products/view (executes View action in ProductsController)
/products (executes Index action in ProductsController)
Optional parameters
 Consider the template below:
{controller}/{action=index} /{id?}
 Third segment, id, is an optional parameter
 If present, the router will capture the value, otherwise it will not create a route value for
id
 Both URLs below will be a match for this template
products/view/100
products/view
Constraints
 Routing only matches URL segments to route parameters, it doesn’t know anything about
the data that you’re expecting those route parameters to contain
 For example, if the template is
{controller=Home}/{action=Index}/{id?}
 Both these URLs would match
/Home/List/test
/1/2/3
 But we don’t have a controller named 1Controller!
 We can add additional constraints to a route template that must be satisfied for a URL to be
considered a match
 Constraints can be defined in a route template for a given route parameter using : (a colon)
 For example, {qty:int}, means the parameter must be integer
Handling multiple matching actions for
a route
 Often you want to match a single URL to two different action methods
 One of the most common situations is when you’re submitting a form
 In order to select one action over the other for a given request, you can add an
attribute
 For example, the [HttpPost] attribute indicates that the method is a match for
POST requests only, and not any other HTTP methods.
 Example:
[HttpPost]
public IActionResult Index(OrderModel model)
{
ViewBag.AmtDue = model.CalculateAmountDue();
return View();
}

More Related Content

Similar to Chapter5.pptx

AngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPIAngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPIEric Wise
 
Custom routing in asp.net mvc
Custom routing in asp.net mvcCustom routing in asp.net mvc
Custom routing in asp.net mvcCapgemini India
 
quickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcquickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcjorgesimao71
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desaijinaldesailive
 
Murach: How to transfer data from controllers
Murach: How to transfer data from controllersMurach: How to transfer data from controllers
Murach: How to transfer data from controllersMahmoudOHassouna
 
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...murtazahaveliwala
 
ASP.Net MVC 4 [Part - 2]
ASP.Net MVC 4 [Part - 2]ASP.Net MVC 4 [Part - 2]
ASP.Net MVC 4 [Part - 2]Mohamed Abdeen
 
Angular Presentation
Angular PresentationAngular Presentation
Angular PresentationAdam Moore
 

Similar to Chapter5.pptx (20)

AspNetMVC.ppt
AspNetMVC.pptAspNetMVC.ppt
AspNetMVC.ppt
 
MVC 4
MVC 4MVC 4
MVC 4
 
Routing
RoutingRouting
Routing
 
AngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPIAngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPI
 
Asp.net,mvc
Asp.net,mvcAsp.net,mvc
Asp.net,mvc
 
Custom routing in asp.net mvc
Custom routing in asp.net mvcCustom routing in asp.net mvc
Custom routing in asp.net mvc
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
quickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcquickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvc
 
Mvc
MvcMvc
Mvc
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
 
Murach: How to transfer data from controllers
Murach: How to transfer data from controllersMurach: How to transfer data from controllers
Murach: How to transfer data from controllers
 
CodeIgniter 101 Tutorial
CodeIgniter 101 TutorialCodeIgniter 101 Tutorial
CodeIgniter 101 Tutorial
 
AngularJs-training
AngularJs-trainingAngularJs-training
AngularJs-training
 
Rails review
Rails reviewRails review
Rails review
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
 
ASP.Net MVC 4 [Part - 2]
ASP.Net MVC 4 [Part - 2]ASP.Net MVC 4 [Part - 2]
ASP.Net MVC 4 [Part - 2]
 
17612235.ppt
17612235.ppt17612235.ppt
17612235.ppt
 
Angular Presentation
Angular PresentationAngular Presentation
Angular Presentation
 

Recently uploaded

Cytokinin, mechanism and its application.pptx
Cytokinin, mechanism and its application.pptxCytokinin, mechanism and its application.pptx
Cytokinin, mechanism and its application.pptxVarshiniMK
 
‏‏VIRUS - 123455555555555555555555555555555555555555
‏‏VIRUS -  123455555555555555555555555555555555555555‏‏VIRUS -  123455555555555555555555555555555555555555
‏‏VIRUS - 123455555555555555555555555555555555555555kikilily0909
 
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...lizamodels9
 
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxMicrophone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxpriyankatabhane
 
Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024AyushiRastogi48
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSarthak Sekhar Mondal
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |aasikanpl
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Temporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of MasticationTemporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of Masticationvidulajaib
 
Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫qfactory1
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PPRINCE C P
 
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxRESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxFarihaAbdulRasheed
 
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
TOTAL CHOLESTEROL (lipid profile test).pptx
TOTAL CHOLESTEROL (lipid profile test).pptxTOTAL CHOLESTEROL (lipid profile test).pptx
TOTAL CHOLESTEROL (lipid profile test).pptxdharshini369nike
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxSwapnil Therkar
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentationtahreemzahra82
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxNandakishor Bhaurao Deshmukh
 
Forest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantForest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantadityabhardwaj282
 

Recently uploaded (20)

Cytokinin, mechanism and its application.pptx
Cytokinin, mechanism and its application.pptxCytokinin, mechanism and its application.pptx
Cytokinin, mechanism and its application.pptx
 
‏‏VIRUS - 123455555555555555555555555555555555555555
‏‏VIRUS -  123455555555555555555555555555555555555555‏‏VIRUS -  123455555555555555555555555555555555555555
‏‏VIRUS - 123455555555555555555555555555555555555555
 
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
 
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxMicrophone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
 
Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Temporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of MasticationTemporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of Mastication
 
Volatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -IVolatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -I
 
Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C P
 
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxRESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
 
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
TOTAL CHOLESTEROL (lipid profile test).pptx
TOTAL CHOLESTEROL (lipid profile test).pptxTOTAL CHOLESTEROL (lipid profile test).pptx
TOTAL CHOLESTEROL (lipid profile test).pptx
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentation
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
 
Forest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantForest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are important
 

Chapter5.pptx

  • 2. Routing  In ASP.NET Core MVC, routing is the process of mapping an incoming HTTP request to a specific action method  Before routing, URLs typically mapped to a physical file on disk  For example, the URL ‘../products.aspx’ would map to a file named ‘products.aspx’ in the application folder  This may be simpler than routing, but has disadvantages:  If you change the file name, all links to the page within the application need to be changed  URLs exposed to users need to be changed
  • 3. Benefits of routing  Routing allows us to define URLs that are not tied to particular files or structure of our application  It decouples action methods from the URLs that are used to execute them  If you need to change URLs, you only need to modify the routing system; action methods remain unchanged
  • 4. Routing terms  Route template  Defines a URL pattern that is used to match against request URLs  A template is a string that contains placeholders for parts that may vary  For example, a route template {controller}/{action}, would map the URL ‘www.localhost:12345.com/product/view’ to ProductController.View() action method
  • 5. Route template  A single route template can map a number of different URLs  For example, the same route template would map the URL ‘home/index’ to HomeController.Index() action method  An application can have more than one routing template
  • 6. Routing terms  URL segment  A small, contiguous section of a URL  They are separated by a character, often by the ‘/’ character  Routing involves matching the segments of a URL to a route template
  • 7. Routing in MVC  Two ways to define routes:  Routing using conventions  Routing using attributes  In this chapter we will cover routing using conventions
  • 8. Routing using conventions  Defined in the Startup.cs file: app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });  EndPointMiddleware is where you configure all the endpoints  An endpoint is some handler that returns a response  Route name: a name that can be used by other parts of the program  Pattern: the route template
  • 9. Defining multiple routes app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "category", pattern: "{controller}/{action}/category{cat}"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
  • 10. Defining multiple routes  The order of routes is important  It defines the order against which the routing infrastructure will attempt to match an incoming request  You should list the most specific routes first, in order of decreasing specificity, until you get to the most broad/general route
  • 11. Route template syntax  It consists of segments, separated by “/’  A segment can be  A literal  A route parameter  A literal must be matched exactly by the request URL  A parameter is a section of a URL that may vary, but still be a match for the template  Route parameters are defined by giving them a name, and placing them in braces, such as {controller} or {action}
  • 12. How do routing templates work?  For each defined route template, the router attempts to split the request’s URL into segments corresponding to the template  For example, suppose we have this template: {controller}/{action}  and the URL is: /products/view  The EndPointMiddleware will split the URL into segments corresponding to the template  In this case, the URL matches the pattern of the template  It has 2 segments  When there is a match, values associated with the parameters are stored in a collection associated with the request  In our example:  value associated with {controller} parameter is ‘products’  value associated with {action} parameter is ‘view’  Then using these values, EndPointMiddleware attempts to find a controller named ‘productsController’ that contains an action named ‘view’
  • 13. How do routing templates work? (cont’d)  Supposed we have a different request URL, for example: /products/books/view  This URL with three segments does not match the route template {controller}/{action}  The EndPointMiddleware will compare it against other route templates, if there are more  If no match is found, a 404 response will be generated
  • 14. How do routing templates work? (cont’d)  Literal segments  Consider the route template below, that contains a literal segment sales/{controller}/{action}  The URL below will be a match; it has three segments and the first is ‘sales’: sales/products/view  However, this other URL, will not be a match, because the first segment is not exactly ‘sales’ as specified in the template rentals/products/view
  • 15. Default values  Consider the template below: {controller}/{action=Index}  The {action} parameter has a default value specified for it, Index  This means that if the URL doesn’t contain a segment corresponding to the action parameter, then the router will use the value ‘Index’ instead  For example, both these two URLs will match this template /products/view (executes View action in ProductsController) /products (executes Index action in ProductsController)
  • 16. Optional parameters  Consider the template below: {controller}/{action=index} /{id?}  Third segment, id, is an optional parameter  If present, the router will capture the value, otherwise it will not create a route value for id  Both URLs below will be a match for this template products/view/100 products/view
  • 17. Constraints  Routing only matches URL segments to route parameters, it doesn’t know anything about the data that you’re expecting those route parameters to contain  For example, if the template is {controller=Home}/{action=Index}/{id?}  Both these URLs would match /Home/List/test /1/2/3  But we don’t have a controller named 1Controller!  We can add additional constraints to a route template that must be satisfied for a URL to be considered a match  Constraints can be defined in a route template for a given route parameter using : (a colon)  For example, {qty:int}, means the parameter must be integer
  • 18. Handling multiple matching actions for a route  Often you want to match a single URL to two different action methods  One of the most common situations is when you’re submitting a form  In order to select one action over the other for a given request, you can add an attribute  For example, the [HttpPost] attribute indicates that the method is a match for POST requests only, and not any other HTTP methods.  Example: [HttpPost] public IActionResult Index(OrderModel model) { ViewBag.AmtDue = model.CalculateAmountDue(); return View(); }