ASP.NET 更改文章名称的路由
ASP.NET routing w/ changing article name
是否可以通过 ASP.NET 路由忽略 url 的倒数第二部分?
例如../article-name-x/123456/
无论产品名称如何变化,我们都希望始终通过在末尾添加文章 ID 来指向同一篇文章,并使用它来指向正确的文章。
有时我们的产品在发布时还没有最终名称,因此我们需要在知道最终名称时进行更新。但是我们已经在我们的通信中使用了初始 url,我们不希望 link 以后被破坏。
有人能帮帮我吗?
如果我理解你的问题是对的,你希望路由同时包含文章 name 和 id。
您可以在 .net MVC
中使用 Attribute routing
。传递 name 和 id 作为路径中定义的路径参数。然后在操作中您可以使用其中任何一个(或两者)来搜索文章。
首先在RegisterRoutes
方法中允许属性路由
routes.MapMvcAttributeRoutes();
然后为您的操作定义一个 Route
属性
[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) { … }
}
这个动作可以称为
articles/my-article-name/my-article-id
详情请参考this article
HTH.
是否可以通过 ASP.NET 路由忽略 url 的倒数第二部分?
例如../article-name-x/123456/
无论产品名称如何变化,我们都希望始终通过在末尾添加文章 ID 来指向同一篇文章,并使用它来指向正确的文章。
有时我们的产品在发布时还没有最终名称,因此我们需要在知道最终名称时进行更新。但是我们已经在我们的通信中使用了初始 url,我们不希望 link 以后被破坏。
有人能帮帮我吗?
如果我理解你的问题是对的,你希望路由同时包含文章 name 和 id。
您可以在 .net MVC
中使用 Attribute routing
。传递 name 和 id 作为路径中定义的路径参数。然后在操作中您可以使用其中任何一个(或两者)来搜索文章。
首先在RegisterRoutes
方法中允许属性路由
routes.MapMvcAttributeRoutes();
然后为您的操作定义一个 Route
属性
[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) { … }
}
这个动作可以称为
articles/my-article-name/my-article-id
详情请参考this article
HTH.