Click here to Skip to main content
16,020,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi.
I have a project in asp.net mvc5 and I have a "Products" Controller.
I want to have these Urls:
for product list : "www.domain.com/Products"
for PDP page or one product page : "www.domain.com/Products/myproductName"
and also another PDP Url: "www.domain.com/Products/myproductID"

and for website SEO, I want to redirect permanent(302) "productID" Url to "productname" Url.

I have a "Index: action in my "Products" Controller and two "cshtml" view pages "Index" and "Product".
"Index.cshtml" is product list view and "Product.cshtml" is PDP view.
I added this route in my "RouteConfig.cs" file :
C#
routes.MapRoute(
    name: "Products",
    url: "Products/{id}",
    defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);


This is my Action Code:
C#
public ActionResult Index(string id)
{
    if (string.IsNullOrEmpty(id))
    {
        var ProductList = repP.GetProductList();
        return View(ProductList);
    }
    else
    {
        try
        {
            int pid = Convert.ToInt32(id);
            var p = repP.GetProductByID(pid);
            if (p != null)
                return RedirectToActionPermanent("", "Products", new { id = p.prdxtraUrlTitle });
            else
                return RedirectToAction("");
        }
        catch (Exception)
        {
            var p = repP.GetProductByUrl(id);
            if (p != null)
                return View("Product", p);
            else
                return RedirectToAction("");
        }
    }
}

Now, what's my problem? Google Indexed for example this url of my website: www.domain.com/Products/Index/1
but I need to redirect this url to for example www.domain.com/Products/My-Software

But when I type www.domain.com/Products/1 or www.domain.com/Products/Index/1 after redirect, my browser address is : www.domain.com/Products/Index/My-Software instead of www.domain.com/Products/My-Software

1- what is the best way to solve this problem? Did I do it correctly?
2- If my code is correct, how can I remove "Index" from My Urls? productID and productName url. because google detect the content of these Urls are duplicate and this decreases website SEO score.
Thanks a lot.

What I have tried:

I told it in description part.
Posted

Change your route to this:
C#
routes.MapRoute(
    name: "Products",
    url: "Products/{id}",
    defaults: new { controller = "Products", id = UrlParameter.Optional }
);
Once you have done this, make sure you have a method in your ProductsController that matches the signature of the id parameter.
C#
public class ProductsController : Controller
{
  public ActionResult Products(string id)
  {
    // Your logic here.
  }
}
 
Share this answer
 
Thanks for your answer. I tested this, but it doesn't work. When I call "www.domain.com/Products" url, browser shows "404 Not Found Page".
 
Share this answer
 
Comments
Pete O'Hanlon 22-Jul-24 8:50am    
You need to add the id for this to match.
Richard Deeming 22-Jul-24 11:01am    
You have posted this comment as another "solution" to your question.

To reply to Pete, click the "Have a Question or Comment?" button under his solution and post a comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900