asp.net core seo url code example
Example: seo friendly url asp.net core
[HttpGet("product/{id}/{title}", Name = "GetProduct")]
public IActionResult GetProduct(int id, string title)
{
// Get the product as indicated by the ID from a database or some repository.
var product = this.productRepository.Find(id);
// If a product with the specified ID was not found, return a 404 Not Found response.
if (product == null)
{
return this.NotFound();
}
// Get the actual friendly version of the title.
string friendlyTitle = FriendlyUrlHelper.GetFriendlyTitle(product.Title);
// Compare the title with the friendly title.
if (!string.Equals(friendlyTitle, title, StringComparison.Ordinal))
{
// If the title is null, empty or does not match the friendly title, return a 301 Permanent
// Redirect to the correct friendly URL.
return this.RedirectToRoutePermanent("GetProduct", new { id = id, title = friendlyTitle });
}
// The URL the client has browsed to is correct, show them the view containing the product.
return this.View(product);
}