以下是有人访问我的电子商务网站上的产品页面时使用的代码.
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",slug = product.SEOTextSlug});
当然它正如我所希望的那样工作,但我想确保我返回正确的标准HTTP结果,以便搜索引擎正确响应它.
解决方法
我相信RedirectToAction返回302而RedirectToActionPermanent返回301.