For routing a URL should have controller/action/data, with data passed to the action. In the lesson it has controller/data for the URL with action missing. Yes, the application works as the lesson says it should, but I don't understand why. If I use /Employee/Mark
as the URL, why does it not look for an action named Mark instead of using Mark as data?
I have pasted the route from the lesson below.
[code]
routes.MapRoute("Employee","Employee/{name}",new{
controller ="Employee", action ="Search", name =UrlParameter.Optional});
If you look at the definition of the route, you'll see that the pattern is:
Employee/{name}
This basically states that any routes that come in meeting this pattern (e.g. Employee/dmcdivitt, Employee/Rion, etc.) will be routed to the related action that uses the Employee controller and Search action (defined by the 3rd parameter to MapRoute). Since
the name parameter is mentioned in the pattern, it will be bound as a parameter and passed to the action as an argument.
So these would all translate per this mapping definition as follows:
If you put a breakpoint in your related action, you should see the name parameter get populated as expected:
public class EmployeeController: Controller
{
public ActionResult Search(string name)
{
// You should see name populated with the value passed in to your route
}
}
Thanks @Rion. The idea I had that /controller/action/data always applies must have come from something I read. In fact this comes from the pattern as the second argument of the routes.MapRoute method.
"The idea I had that /controller/action/data always applies must have come from something I read."
There are defaults, and there are routes you define yourself. You don't have to follow the default assumptions when you write your own routes. As you see in your example, the action is defined in the route; it doesn't need to be in the URL. In fact you
don't need the controller in the URL either.
In fact, this:
routes.MapRoute(
"PackageDetails",
"vacations/{title}-{packageId}",
new { controller = "Packages", action = "Details"},
new { packageId = @"\d+$" });
Takes a URL of vacations/Tahiti-12345 and passes it to the Packages controller, performs the Details action, and sends that action data for the packageId that is parsed as the trailing numbers in the URL. The "Tahiti" in the URL is visual information for
the site visitor, not needed for the Action.
Member
36 Points
153 Posts
why does data come through the route instead of an action
Jan 24, 2020 06:11 PM|dmcdivitt|LINK
I'm learning MVC and very much like the tutorial at https://www.tutorialspoint.com/asp.net_mvc/asp.net_mvc_controllers.htm . The page given in the link is the one I have a question about.
For routing a URL should have controller/action/data, with data passed to the action. In the lesson it has controller/data for the URL with action missing. Yes, the application works as the lesson says it should, but I don't understand why. If I use /Employee/Mark as the URL, why does it not look for an action named Mark instead of using Mark as data?
I have pasted the route from the lesson below.
[code]
[/code]
All-Star
114593 Points
18503 Posts
MVP
Re: why does data come through the route instead of an action
Jan 24, 2020 06:36 PM|Rion Williams|LINK
If you look at the definition of the route, you'll see that the pattern is:
This basically states that any routes that come in meeting this pattern (e.g. Employee/dmcdivitt, Employee/Rion, etc.) will be routed to the related action that uses the Employee controller and Search action (defined by the 3rd parameter to MapRoute). Since the name parameter is mentioned in the pattern, it will be bound as a parameter and passed to the action as an argument.
So these would all translate per this mapping definition as follows:
If you put a breakpoint in your related action, you should see the name parameter get populated as expected:
Member
36 Points
153 Posts
Re: why does data come through the route instead of an action
Jan 24, 2020 07:43 PM|dmcdivitt|LINK
Thanks @Rion. The idea I had that /controller/action/data always applies must have come from something I read. In fact this comes from the pattern as the second argument of the routes.MapRoute method.
Contributor
5961 Points
2466 Posts
Re: why does data come through the route instead of an action
Jan 24, 2020 08:14 PM|KathyW|LINK
"The idea I had that /controller/action/data always applies must have come from something I read."
There are defaults, and there are routes you define yourself. You don't have to follow the default assumptions when you write your own routes. As you see in your example, the action is defined in the route; it doesn't need to be in the URL. In fact you don't need the controller in the URL either.
In fact, this:
Takes a URL of vacations/Tahiti-12345 and passes it to the Packages controller, performs the Details action, and sends that action data for the packageId that is parsed as the trailing numbers in the URL. The "Tahiti" in the URL is visual information for the site visitor, not needed for the Action.