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.
The expression *this is commonly used to return the current object from a member function: C++ Copy. return *this; The this pointer is also used to guard against self-reference: C++ Copy.
It is not a reserved identifier, so you are free to use it.
this means pointer to the object, so *this is an object. So you are returning an object ie, *this returns a reference to the object.
The bitwise OR assignment operator ( |= ) uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable.
I believe that RedirectToAction
returns 302 while RedirectToActionPermanent
returns 301.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With