Answer the question
In order to leave comments, you need to log in
How to properly set URL in ASP.Net MVC?
Hello! I understand the third week in asp. Everything seems to be clear, but I can’t understand the logic of setting up routing and displaying URLs. An interesting task has arisen before me.
There is a ProductListController controller. It has an Index action with category filtering by the get tool (that is, with the filter set, the address bar looks like this: localhost/ProductList/?category=2).
Hence the first question: how to make the address bar look like: localhost/ProductList/notebook (notebook corresponds to the second category in the list of categories that a product can belong to)?
In addition, in the Index view, each product in the list can be clicked to go to the product page (the Product action, in the case of the first product in the list, corresponds to the address: localhost/ProductList/Product/1)
Question #2: Is it possible to cast the URL on the product page to the form localhost/ProductList/notebook/1 and then to localhost/ProductList/notebook/[NC_laptop] (the CNC is in the database and is located by id)?
From the manuals, I guess that this is all configured in RouteConfig, but I really don’t quite understand the logic of this system yet (well, except for the very basics). So I ask, if the task is feasible, describe the solution in a little more detail.
Answer the question
In order to leave comments, you need to log in
one)
routes.MapRoute(
"Product",
"ProductList/Product/{productId}",
new { controller = "ProductList", action = "Product"}
);
routes.MapRoute(
"ProductList",
"ProductList/{categoryName}",
new { controller = "ProductList", action = "Category",}
);
routes.MapRoute(
"ProductFromId",
"ProductList/Product/{categoryName}/{productId}",
new { controller = "ProductList", action = "Product"},
new {productId = @"\d+" }
);
routes.MapRoute(
"ProductFromName",
"ProductList/Product/{categoryName}/{productName}",
new { controller = "ProductList", action = "ProductFromName"}
);
Yes, everything is decided in RouteConfig. localhost is an appendage to forget about. for the first task you need a root with a template just ProductList -
routes.MapRoute(
"ProductList", // Route name
"ProductList", // URL with parameters
new { controller = "ProductList", action = "Index", id = UrlParametr.Optional } // Parameter defaults
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question