这是我在有人访问我的电子商务网站上的产品页面时使用的代码。
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);
}
}
在这一点:
if (product.SeoTextSlug != slug)
return RedirectToAction("Details", new {id = product.ProductId, slug = product.SeoTextSlug});
我实际上是否正确触发了 301 重定向?
当然它可以按我的意愿工作,但我想确保我返回正确的标准 HTTP 结果,以便搜索引擎正确响应它。