如何在ASP.NET Core中使用anchor taghelper呈现日期部分片段

时间:2016-03-15 13:30:05

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

我试图制作一个博客,其设置允许在帖子的2个网址格式之间进行选择,其中一个以日期作为细分,一个仅包含细分。我希望用户能够为他们的博客选择这些设置中的任何一个,并且不应要求更改启动代码来来回切换。事实上,我试图支持多租户博客,因此每个博客都可以拥有自己的网址格式偏好。

我在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>

但是如果没有像这样的slu ::那那就完全错了:

http://localhost:60000/blog

我想让tagHelper像这样渲染网址:

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

任何人都可以看到我做错了什么或做得对吗?或者我是否需要为此实现自定义锚标记?

1 个答案:

答案 0 :(得分: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>