gpt4 book ai didi

.net - 带有更改文章名称的 ASP.NET 路由

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:43:42 24 4
gpt4 key购买 nike

是否可以通过 ASP.NET 路由忽略 url 的倒数第二部分?

例如../文章名称-x/123456/

无论产品名称如何变化,我们都希望始终通过在末尾添加文章 ID 来指向同一篇文章,并使用它来指向正确的文章。

有时我们的产品在发布时还没有最终名称,因此我们需要在知道最终名称时进行更新。但是我们已经在我们的通信中使用了初始 url,我们不希望链接在以后被破坏。

有人可以帮帮我吗?

最佳答案

如果我对您的问题的理解正确,您希望路由同时包含文章 nameid

您可以在 .net MVC 中使用属性路由。传递 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) { … }
}

这个 Action 可以称为

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

请引用this article了解详情。

HTH.

关于.net - 带有更改文章名称的 ASP.NET 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36055448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com