Here is the code I use when someone visits a product page on my ecommerce website.
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);
}
}
In this bit:
if (product.SeoTextSlug != slug)
return RedirectToAction("Details", new {id = product.ProductId, slug = product.SeoTextSlug});
Am I actually firing a 301 redirect correctly?
Sure it's working as I want it to, but I want to make sure I'm returning a correct standard HTTP result so search engines properly respond to it.