【问题标题】:how to render date part segments with anchor taghelper in ASP.NET Core如何在 ASP.NET Core 中使用锚标记助手呈现日期部分段
【发布时间】:2016-06-30 23:29:06
【问题描述】:

我正在尝试创建一个博客,其设置允许在 2 种 url 格式的帖子之间进行选择,一种以日期为段,另一种只有 slug。我希望用户能够为他们的博客选择这些设置中的任何一个,并且它不应该需要更改启动代码来来回切换。事实上,我正在尝试支持多租户博客,这样每个博客都可以有自己的 url 格式偏好。

我在 Startup.cs 中定义了以下路由

routes.MapRoute(
name: "blogcategory",
template: "blog/category/{category=''}/{pagenumber=1}"
, defaults: new { controller = "Blog", action = "Category" }
);

routes.MapRoute(
"blogarchive",
"blog/{year}/{month}/{day}",
new { controller = "Blog", action = "Archive", month = "00", day = "00" },
new { year = @"\d{4}", month = @"\d{2}", day = @"\d{2}" }
);

routes.MapRoute(
"postwithdate",
"blog/{year}/{month}/{day}/{slug}",
new { controller = "Blog", action = "PostWithDate" },
new { year = @"\d{4}", month = @"\d{2}", day = @"\d{2}" }
);

routes.MapRoute(
name: "blogpost",
template: "blog/{slug}"
, defaults: new { controller = "Blog", action = "Post" }
);

routes.MapRoute(
name: "blogindex",
template: "blog/"
, defaults: new { controller = "Blog", action = "Index" }
);

routes.MapRoute(
name: "pageindex",
template: "{slug=none}"
, defaults: new { controller = "Page", action = "Index" }
);

routes.MapRoute(
name: "def",
template: "{controller}/{action}" 
);
routes.MapRoute(
"postwithdate",
"blog/{year}/{month}/{day}/{slug}",
new { controller = "Blog", action = "PostWithDate" },
new { year = @"\d{4}", month = @"\d{2}", day = @"\d{2}" }
);

我的博客控制器有这些与发布路线相关的方法

[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> Post(string slug, string mode = "")
{
    return await Post(0, 0, 0, slug, mode);
}

[HttpGet]
[AllowAnonymous]
[ActionName("PostWithDate")]
public async Task<IActionResult> Post(int year , int month, int day, string slug, string mode = "")
{
    ...
}

如果我手动导航到

http://localhost:60000/blog/2016/03/07/squirrel

页面按预期工作

在我看来,我正在使用锚标记助手呈现指向帖子的链接

@if (Model.ProjectSettings.IncludePubDateInPostUrls)
{
    <a asp-controller="Blog" asp-action="Post" 
       asp-route-year="@Model.TmpPost.PubDate.Year"
       asp-route-month="@Model.TmpPost.PubDate.Month"
       asp-route-day="@Model.TmpPost.PubDate.Day"
       asp-route-slug="@Model.TmpPost.Slug" 
       itemprop="url">@Model.TmpPost.Title</a>
}
else
{
    <a asp-controller="Blog" asp-action="Post" asp-route-slug="@Model.TmpPost.Slug" itemprop="url">@Model.TmpPost.Title</a>
}

但是当我将它配置为在 url 中使用 pubDate 时,它​​会呈现如下链接:

http://localhost:60000/blog/squirrel?year=2016&month=3&day=7

那个网址也可以,但我怎样才能让它像这样呈现:?

http://localhost:60000/blog/2016/03/07/squirrel

我还尝试使用带有 taghelper 的命名路由而不是控制器和操作,如下所示:

<a asp-route="postwithdate" 
       asp-route-year="@Model.TmpPost.PubDate.Year"
       asp-route-month="@Model.TmpPost.PubDate.Month"
       asp-route-day="@Model.TmpPost.PubDate.Day"
       asp-route-slug="@Model.TmpPost.Slug" 
       itemprop="url">@Model.TmpPost.Title</a>

但即使没有像这样的蛞蝓也会呈现完全错误:

http://localhost:60000/blog

我想让 tagHelper 像这样渲染 url:

http://localhost:60000/blog/2016/03/07/squirrel

谁能看到我做错了什么或做错了什么?或者我需要为此实现一个自定义锚标记助手吗?

【问题讨论】:

  • 您使用asp-route="postwithdate" 的想法是正确的,考虑到您在此处显示的代码,这应该可以工作。
  • 我就是这么想的,不知道会不会是 RC1 的错误

标签: asp.net-core asp.net-core-mvc


【解决方案1】:

好的,我找到了一个使用命名路由的解决方案,只需要确保月份和日期被格式化为 2 位数字,然后它现在可以按照我想要的日期段呈现

<a asp-route="postwithdate"
    asp-route-year="@Model.TmpPost.PubDate.Year"
    asp-route-month="@Model.TmpPost.PubDate.Month.ToString("00")"
    asp-route-day="@Model.TmpPost.PubDate.Day.ToString("00")"
    asp-route-slug="@Model.TmpPost.Slug" 
    itemprop="url">@Model.TmpPost.Title</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多