Ad

ASP.NET Routing W/ Changing Article Name

- 1 answer

Is it possible to ignore the second last part of an url with ASP.NET routing?

e.g. ../article-name-x/123456/

No matter how the product name changes, we would like to point to the same article always by adding the article id at the end and use that to point to the correct article.

We sometimes have a product that hasn't got it's final name yet on announcement, so we need to update when the final name is known. But we used the initial url already in our communication and we don't want the link to be broken later.

Can someone help me out?

Ad

Answer

If I understood your question right you want the route to have both article name and id.

You can use Attribute routing in .net MVC. Pass both name and id as path parameters defined in your route. Then in the action you can use any one of them (or both) to search the article.

First allow attribute routing in RegisterRoutes method

routes.MapMvcAttributeRoutes();

Then define a Route attribute for your action

[RoutePrefix("articles")]
[Route("{action=index}")]
public class ArticlesController : Controller
{
    // e.g /articles
    public ActionResult Index() { … } 

    // e.g. /articles/my-article-name/my-article-id
    [Route("{name}/{id}")]
    public ActionResult Search(string name, string id) { … }
}

This action can be called as

articles/my-article-name/my-article-id

Please refer this article for details.

HTH.

Ad
source: stackoverflow.com
Ad