Use ASP.NET 4 URL Routing in nopCommerce 2

The 9 steps in the previous post should have gotten the re-writing working, but there are also more that needs to be refined. The biggest change in the previous post was the use of SEName to retrieve category as opposed to CategoryId. This creates an additional manual step as when you insert a new category you need to be sure to also input “Search engine friendly page name” in the SEO tab, otherwise that rewriting won’t work.

The original rewrite is already using a good method to generate nice SEO friendly names, why don’t we leverage that and always also insert the SEName when a new category is added? Only one thing to do:

In NopCommerceStore.Administration.Modules.CategoryAdd.ascx.cs, which contains the 4 tabs used in creating a new category, before save to database happens we get the Name field in the Category Info tab, call the helper function to generate the SEO friendly name, and fill in the SEName textbox in the SEO tab. I have to say, FindControl is so ASP.NET WebForm, haha…

protected Category Save()
{
    SimpleTextBox nameTextBox = ((SimpleTextBox)ctrlCategoryInfo.FindControl("txtName"));
    if (nameTextBox != null)
    {
        string catName = nameTextBox.Text;
        TextBox seNameTextBox = ((TextBox)ctrlCategorySEO.FindControl("txtSEName"));
        if (seNameTextBox != null)
        {
            string seName = seNameTextBox.Text;
            if (string.IsNullOrWhiteSpace(seName))
            {
                seNameTextBox.Text = NopSolutions.NopCommerce.BusinessLogic.SEO.SEOHelper.GetSEName(catName);
            }
        }
    }

Further to this topic I guess what we could do is to ensure that the SEName column is always unique. That’s a bigger change to the Database and the Entity context.

By Bryan Xu

Posted in eCommerce

Leave a Reply