Asp.net core 如何在ASP.NET Core中使用anchor taghelper呈现日期零件段

Asp.net core 如何在ASP.NET Core中使用anchor taghelper呈现日期零件段,asp.net-core,asp.net-core-mvc,Asp.net Core,Asp.net Core Mvc,我正在尝试创建一个博客,设置允许在两种url格式之间进行选择,一种是以日期为段,另一种是仅以slug为段。我希望用户能够为他们的博客选择这两种设置中的任何一种,并且不需要在启动代码中进行更改来来回切换。事实上,我正在尝试支持多租户博客,这样每个博客都可以有自己的url格式首选项 我在Startup.cs中定义了以下路由 routes.MapRoute( name: "blogcategory", template: "blog/category/{category=''}/{pagenumber

我正在尝试创建一个博客,设置允许在两种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 = "")
{
    ...
}
页面按预期工作

在我看来,我正在使用锚定标记帮助器呈现到文章的链接,如下所示

@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也可以工作,但如何使其呈现为这样:

http://localhost:60000/blog/2016/03/07/squirrel
我还尝试将命名路由与taghelper一起使用,而不是控制器和操作,如下所示:

http://localhost:60000/blog/squirrel?year=2016&month=3&day=7
<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
http://localhost:60000/blog/2016/03/07/squirrel
我想让tagHelper呈现url,如下所示:

http://localhost:60000/blog/squirrel?year=2016&month=3&day=7
<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
http://localhost:60000/blog/2016/03/07/squirrel

有人能看出我做错了什么或做得不对吗?或者我需要为此实现一个自定义锚标记帮助器吗?

好的,我找到了一个使用命名路由的解决方案,只需要确保月和日被格式化为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>
@Model.TmpPost.Title

您使用
asp route=“postwithdate”
的想法是正确的,考虑到您在这里展示的代码,这应该可以实现。这就是我的想法,不知道它是否可能是RC1中的错误