2015-02-11 35 views
-2

我需要你的幫助,因爲我搜索,但我的問題沒有解決。非常感謝。如何使用mvc4中的RouteConfig.cs文件重寫url?

我有網址:http://example.com/products/?sec=2(2 sectionname的ID) 而現在,我想告訴http://example.com/products/sectionname`

請讓我知道我可以做RouteConfig.cs文件? (我是新的mvc4和C#,所以我不明白我在谷歌搜索中看到的)。 下面是我的控制器:

public ActionResult Index(int cat=-1, int sec=-1, fea=-1) 
{ 
    var products = db.Products.Where(p => p.Publish).OrderByDescending(p=>p.ProductDate).ToList(); 
    if (sec!=-1) 
    { 
    products = products.Where(p => p.Category.SectionId ==sec).ToList();  
    } 
    if (cat!=-1) 
    { 
    products = products.Where(p => p.CategoryId == cat).ToList(); 
    } 
    if (fea!=-1) 
    { 
    products = products.Where(p => p.Feature == true).ToList(); 
    } 
    return View(products); 
} 

我的路線:

routes.MapRoute(
       "section name", // Route name 
       "Products/section/{sec}", // URL with parameters 
       new 
       { 
        controller = "Products", 
        action = "Index", sec =UrlParameter.Optional, 
       } 
      ); 

,當我跑我的項目,但它仍然http://example.com/products/?sec=2

+0

目前還不清楚你想從你的問題中獲得什麼。我建議你閱讀關於MVC路由,並告訴我們你的路由設置和你想要處理的URL。一個鏈接https://msdn.microsoft.com/en-us/library/cc668201.aspx。 – SBirthare 2015-02-11 05:17:25

回答

-1

,在我看來,我從<a href="/Products/[email protected]">@item.SectionName</a>改爲<a href="/Products/Sect/@item.Id">@item.SectionName</a>

它的工作。