how to render date part segments with anchor taghelper in ASP.NET Core

I'm trying to make a blog with a setting that allows a choice between 2 url formats for posts, one with the date as segments and one with only the slug. I want users to be able to choose either of these settings for their blog and it should not require changes in startup code to switch back and forth. In fact I'm trying to support multi-tenant blogs so each blog can have its own url format preference.

I have the following routes defined in 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}" }
);

My Blog controller has these methods related to the post routes

[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 I manually navigate to

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

the page works as expected

in my view I'm rendering the link to the post like this with the anchor tag helper

@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>
}

but when I configure it to use the pubDate in the url it renders the link like this:

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

That url also works but how can I make it render like this:?

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

I tried also using a named route with the taghelper instead of controller and action, like this:

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

but that one renders completely wrong without even the slug like this:

http://localhost:60000/blog

I want to make the tagHelper render the url like this:

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

Can anyone see what I'm doing wrong or not doing right? Or would I need to implement a custom anchor taghelper for this?

Answers


ok, I found a solution using the named route, just need to make sure the month and day get formatted as 2 digits, after doing that it now renders as I wanted with the date segments

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

Need Your Help

How to convert NSArray ( in string format) to real Array format?

ios objective-c json nsarray

When i parse one JSON i got following result inside my NSArray

Receiveing Output of Command from Paramiko - Python

python ssh paramiko

I'm creating a python script that SSH's into a linux box that runs a few commands. I'm using paramiko for the ssh functionality.