代码之家  ›  专栏  ›  技术社区  ›  Ian Oxley

每个请求执行4次ASP.NET MVC控制器操作

  •  2
  • Ian Oxley  · 技术社区  · 16 年前

    以前有人遇到过这样的事情吗?基本上,我在一个控制器上有一个操作,它只通过存储库模式查询数据库,向view data添加一些数据,然后返回视图。但出于某种原因,这项行动被要求 每次请求4次 .

    整个动作本身只有10行长:

    public ActionResult Details(int id, string slug) {
        Product p = productRepository.GetProduct(id);
    
        IEnumerable<Image> imgs = productRepository.GetImages(p.ProductId);
        if (imgs.Count() > 0) {
            ViewData["MainImage"] = imgs.First();
            ViewData["Images"] = imgs;
        }
    
        Brand brand = productRepository.GetBrand(p.ProductId);
        ViewData["Brand"] = brand;
    
        var categories = productRepository.GetCategories(p.ProductId, true);
        ViewData["ProductCategories"] = categories;
    
        return View("Details", p);
    }
    

    此外,my global.asax中定义的路由如下:

    routes.MapRoute(
        "SlugsAfterId",
        "{controller}.mvc/{action}/{id}/{slug}",
        new { controller = "Products", action = "Browse", id = "" }
    );
    
    // The default route that comes with ASP.NET MVC
    routes.MapRoute(
        "Default",                                              // Route name
        "{controller}.mvc/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    );
    

    有人能解释一下这个吗?我完全被难住了。

    1 回复  |  直到 16 年前
        1
  •  7
  •   Chad Moran    16 年前

    看起来这些请求可能是客户端请求,比如图像、CSS或JS文件。