2015-10-27 22 views
2

我的代碼如下:asp.net mvc4的VaryByParam不起作用

[HttpGet] 
[OutputCache(Duration = 90, VaryByParam = "cityCode")] 
public ActionResult About(string userName, string cityCode) 
{ 
    //do something... 
    return View(); 
} 
  1. 緩存工作正常,當我訪問的網址:

http://localhost:52121/LabOne/MvcCache/About?userName=admin&cityCode=010

  • 但是當我像下面那樣訪問這個路由URL時,緩存不起作用,爲什麼?
  • http://localhost:52121/LabOne/MvcCache/About/admin/010

    +0

    你能否把你的路線配置放在這裏 –

    回答

    0

    我複製你的代碼,我測試了我的機器上,我配置了RouteConfig爲以下

    public class RouteConfig 
    { 
        public static void RegisterRoutes(RouteCollection routes) 
        { 
         routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    
         routes.MapRoute(
          name: "Default", 
          url: "{controller}/{action}/{id}", 
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
         ); 
    
         routes.MapRoute(
          name: "aboutRoute", 
          url: "{controller}/{action}/{userName}/{cityCode}", 
          defaults: new { controller = "Home", action = "About", userName = UrlParameter.Optional, cityCode = UrlParameter.Optional } 
         ); 
        } 
    } 
    

    和我面臨同樣的問題,我將解釋這一點:

    OutputCache取決於URL,並且您提供的示例實際上是兩個不同的 URL,雖然它們是ar e會產生相同的結果。

    因此,請嘗試再次請求URL http://localhost:52121/LabOne/MvcCache/About/admin/010。並且您將看到OutputCache正在工作,並且MVC將從緩存中獲得結果,因爲OutputCache已在上一次緩存該URL。

    UPDATE

    根據這個問題Using outputcache in MVC和接受的答案,高速緩存與URL的工作,並沒有與MVC路由系統的相關性。