省略URL的中间部分

时间:2017-01-23 01:47:12

标签: c# asp.net-core-mvc asp.net-routing asp.net-core-routing

我正在开发一个博客网络应用程序,我希望个人博客帖子链接看起来像这样:http://example.com/Post/2017/1/22/post-title,但同时,如果一天有超过两个帖子,则第二个链接一个应该看起来像http://example.com/Post/2017/1/22/2/another-post-title。目前,我已按照以下方式实施:

[AllowAnonymous]
[Route("Post/{year:int}/{month:int}/{day:int}/{*title}")]
[HttpGet]
public Task<IActionResult> GetPostByDateTitle(int year, int month, int day, string title)
{
    return this.GetPostByDate(year, month, day, 1);
}

[AllowAnonymous]
[Route("Post/{year:int}/{month:int}/{day:int}/{dayId:int}/{*title}")]
[HttpGet]
public Task<IActionResult> GetPostByDateTitle(int year, int month, int day, int dayId, string title)
{
    return this.GetPostByDate(year, month, day, dayId);
}

剃刀代码:

@if (Model.DayId == 1)
{
    <a asp-controller="Post" asp-action="GetPostByDateTitle" asp-route-year="@Model.Year" asp-route-month="@Model.Month" asp-route-day="@Model.Day" asp-route-title="@LinkTitle(Model.Title)">link</a>
}
else
{
    <a asp-controller="Post" asp-action="GetPostByDateTitle" asp-route-year="@Model.Year" asp-route-month="@Model.Month" asp-route-day="@Model.Day" asp-route-dayid="@Model.DayId" asp-route-title="@LinkTitle(Model.Title)">link</a>
}

始终忽略title并且仅存在以使URL更易于人类阅读,因此它会被解析,但会被忽略。我真正想要的是让dayId部分可选而不采用两种方法并检查Razor代码,找出要调用哪一个。我该怎么做?

0 个答案:

没有答案