D
D
Denis2015-12-18 21:13:10
ASP.NET
Denis, 2015-12-18 21:13:10

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

2 answer(s)
W
wkololo_4ever, 2015-12-19
Kutovsky @deniskutovskiy

one)

routes.MapRoute(
            "Product",                                         
            "ProductList/Product/{productId}",                           
            new { controller = "ProductList", action = "Product"}
        );

In the "ProductList" controller, create a "Product" method that takes a "productId" parameter. MVC will map the passed product id from the url localhost/ProductList/Product/{productId} into it.
routes.MapRoute(
            "ProductList",                                         
            "ProductList/{categoryName}",                           
            new { controller = "ProductList", action = "Category",}  
        );

In the "ProductList" controller, create a "Category" method that takes a "categoryName" parameter. MVC will map the passed category name from the url localhost/ProductList/{categoryName} into it.
routes.MapRoute(
            "ProductFromId",                                         
            "ProductList/Product/{categoryName}/{productId}",                           
            new { controller = "ProductList", action = "Product"},
            new {productId = @"\d+" }
        );

Here "new {productId = @"\d+" }" acts as a constraint that id can only be a number.
routes.MapRoute(
            "ProductFromName",                                         
            "ProductList/Product/{categoryName}/{productName}",                           
            new { controller = "ProductList", action = "ProductFromName"}
        );

You can combine routes, and determine on the controller side what has come, the product name or id.
PS. Routes should be defined starting with the most restrictive. Otherwise, requests will be mapped to the wrong routes.

D
Dmitry Kovalsky, 2015-12-18
@dmitryKovalskiy

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
        );

For the second task, the route will be
trickier routes.MapRoute(
"ProductListSpecial", // Route name
"ProductList/{Product}/{ProductId}", // URL with parameters
new { controller = "ProductList", action = "Index" } / /Parameter defaults
);
More or less like this. Learn more in general.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question