5

Đây là mã tôi sử dụng khi ai đó truy cập vào trang sản phẩm trên trang web thương mại điện tử của tôi.Mã phương thức hành động C# này có thực sự kích hoạt chuyển hướng 301 không?

public ActionResult Details(int id, string slug) 
{ 
    using (var productRepository = new EfProductRepository()) 
    { 
     var product = productRepository.FindById(id); 
     if (product == null) return RedirectToAction("Index", "Home"); 
     if (product.SeoTextSlug != slug) 
      return RedirectToAction("Details", new {id = product.ProductId, slug = product.SeoTextSlug}); 

     var model = new ProductDetailModel(); 

     //Load the product information. 
     model.Product.ProductId = product.ProductId; 
     model.Product.CoverImagePath = product.CoverImagePath; 
     model.Product.Name = product.Name; 
     model.Product.Tagline = product.Tagline; 
     model.Product.Price = product.Price; 
     model.Product.Stock = product.Stock; 
     model.Product.PieceCount = (int)product.PieceCount; 
     model.Product.SKU = product.SKU; 

     //Load the reviews for that product. 
     if (product.Reviews.Any()) 
     { 
      foreach (var review in product.Reviews) 
      { 
       model.Reviews.Add(new ReviewModel() 
       { 
        ReviewId = review.ReviewId, 
        AccountId = (int)review.AccountId, 
        Content = review.Content, 
        Location = review.Location, 
        ProductId = (int)review.ProductId, 
        PublishDate = review.PublishDate, 
        ReviewRatingId = (int)review.ReviewRatingId 
       }); 
      } 
     } 

     return View(model); 
    } 
} 

Trong bit này:

if (product.SeoTextSlug != slug) 
    return RedirectToAction("Details", new {id = product.ProductId, slug = product.SeoTextSlug}); 

Tôi thực sự bắn một chuyển hướng 301 một cách chính xác?

Chắc chắn nó hoạt động như tôi muốn, nhưng tôi muốn đảm bảo rằng tôi trả lại kết quả HTTP chuẩn chính xác để công cụ tìm kiếm phản hồi đúng cách.

+0

Bạn có thể xác nhận nó bằng cách sử dụng các công cụ cho nhà phát triển của trình duyệt. Chuyển đến Mạng và xem Yêu cầu HTTP. – epignosisx

Trả lời

12

Tôi tin rằng RedirectToAction trả 302 trong khi RedirectToActionPermanent lợi nhuận 301.

+0

Vì vậy, tất cả những gì tôi phải làm trên đầu của tôi là sử dụng 'RedirectToActionPermanent' thay vào đó, đúng không? –

+0

@Sergio: vâng. Điều kiện duy nhất là bạn phải chắc chắn rằng bạn muốn 301 thay vì 302. –

Các vấn đề liên quan