1
我試圖讓一個設置允許在文章的兩種url格式之間進行選擇,一種是日期段,一種是隻有蛞蝓。我希望用戶能夠爲他們的博客選擇這些設置中的任何一個,並且它不應該要求啓動代碼中的更改來回切換。實際上,我試圖支持多租戶博客,因此每個博客都可以擁有自己的url格式首選項。如何在ASP.NET Core中使用錨標籤幫助呈現日期部分段
我在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>
}
但是當我將其配置爲使用pubdate的url中它呈現這樣的鏈接:
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呈現這樣的網址:
http://localhost:60000/blog/2016/03/07/squirrel
任何人都可以看到我在做什麼錯或不正確嗎?或者我需要爲此實現自定義錨標籤幫助器嗎?
你有正確的想法使用'asp-route =「postwithdate」'這應該工作給你在這裏顯示的代碼。 –
這就是我想的,不知道它是否可能是RC1中的一個錯誤 –