ASP.NET MVC routing: with querystring to one action, without to another

前端 未结 1 840
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-25 00:47

I need the following routes:

example.com/products
goes to a product categories page (e.g. cars, trucks, buses, bikes)
controller=Products

相关标签:
1条回答
  • 2021-01-25 01:34

    Introduce Parameters. ASP.NET MVC allows you to create 'pretty' URLs, and that's exactly what you should do here:

    First, the route mappings:

    routes.MapRoute(
        "SpecificProducts",
        "products/{a}/{b}",
        new { controller = "products", action = "Categories" }
        );
    
    routes.MapRoute(
        "ProductsIndex",
        "products".
        new { controller = "products", action = "Index" }
        );
    

    Then, the controller actions

    public ActionResult Index()
    {
    }
    
    public ActionResult Categories(string a, string b) //parameters must match route values
    {
    }
    

    This will allow you to use a search-friendly URL and you don't have to worry about query string parameters.

    0 讨论(0)
提交回复
热议问题