Database Reference
In-Depth Information
Solution
Suppose your model looks like the one in Figure 4-16 . Here we've modeled our products, represented by the Product
entity, together with their categories. In a typical eCommerce website, we would show products by category. We
want to avoid exposing query strings like /Products/Index?Category=Tents in our URLs. While these cryptic URLs
simplify programming a little, they don't help much when it comes to search engine optimization. We would rather
have URLs that look more like /Products/Tents .
Figure 4-16. A model for products and their categories
We can get this more Search Engine Optimization -friendly URL structure by using routing. Routes are typically
created in the Application_Start() event handler in Global.asax . The code in Listing 4-7 illustrates adding a route
for the Product controller.
Listing 4-7. Adding the Route in Global.asax
protected void Application_Start()
{
RouteTable.Routes.MapRoute("Product", "{controller}/{name}", new { controller = "Product",
action = "Index" });
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
In the Index view as shown in the Listing 4-8, we use the category name bound to the name parameter in the Index
method of Product controller, as illustrated in Listing 4-7. We use the controller code in Listing 4-9 to fetch the value of
the category name parameter and produce the results through View. Figure 4-17 and Figure 4-18 show the rendered
pages for categories Tents and Cooking Equipment.
Listing 4-8. The Index View Code That Displays the Products Filtered by Category
@model IEnumerable<EntityFrameworkRecipe3.ViewModels.ProductVM>
@{
Layout = null;
}
<!DOCTYPE html>
 
Search WWH ::




Custom Search