ASP.NET routing w/ changing article name
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?
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.
Related Questions
- → Function Undefined in Axios promise
- → React formatting dates with momentjs server side
- → Using ReactJs, How do I update my form fields when my parent object sent in from props changes?
- → Visual Studio 2012 Express: Browser returning 500 status trying to download jsx file
- → AngularJS directive: "templateUrl" doesn't work while "template" works
- → how to add cannonical tag for ASPX .NET page
- → Javascript Paypal slider string suffix
- → JavaScript in MVC 5 not being read?
- → Selecting an element by its attribute when it has a colon in its name
- → Function works for NON dynamically loaded images
- → URL routing requires /Home/Page?page=1 instead of /Home/Page/1
- → There are "gaps" between rows in my masonry cards
- → creating sef links and changing plus sign (+) with dash (-)