Correct optional route parameters on browsing url

2020-05-07 02:16发布

问题:

I am working on Ecommerce project (Asp.Net Mvc3). My routes for product and categories are mentioned below

routes.MapLocalizedRoute(
  "Product",
  "p/{productId}/{SeName}",
   new { controller = "Catalog", action = "Product", SeName = UrlParameter.Optional },
);

routes.MapLocalizedRoute(
  "Product", 
  "c/{categoryId}/{SeName}",
  new { controller = "Catalog", action = "Category", SeName = UrlParameter.Optional },
);

When use browse url http://mysite.in/p/123/my-product it redirects to correct product and same thing happens for category as well.

Now when I provide wrong SeName or Just removes SeName from url it redirects to correct product/category, which is correct. But I want valid SeName to be appended i.e if there is wrong or no SeName in the url sename for repsective product/category should be added in url. Is there any effect on Site SEO of above functinality.

Stackoverflow does the same think i.e though SeName is optional it appends seName if not provided.

Please reply if anybody having worked on same issue.

回答1:

This is referred to as a 'slug' and is typically handled in an action filter. Phil Haack has a good article Manipulating Action Method Parameters that explains it in detail. The basic concept is to handle the filters OnActionExecuting method to extract the route value for the ID parameter, then look up the corresponding name from the database and add that value to the Name parameter.