我在我當前的項目中面臨一個問題,我想爲我的頁面顯示自定義URl。我嘗試了很多技術,但都沒有滿足我的要求。 我想URL是這樣的:WordPress的博客類型MVC(自定義URL路由)中的固定鏈接
http://www.anyDomain.com/What-Is-Your-Name
目前,我能夠設置URL是這樣的:
http://www.anyDomain.com/What-Is-Your-Name?Id=1
我想從URL忽略查詢字符串。這樣Controller可以識別請求&做出相應的響應。
這裏,Id
用於從數據庫獲取詳細信息。如何將參數值從View
傳遞到Controller
,以便它可以在不添加URL的情況下識別請求?
我的控制器
[Route("~/{CategoryName}")]
public ActionResult PropertyDetails(int Id)
{
}
RouteConfig
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults:
new
{
controller = "Home",
action = "Index",
}
);
我查看
<a href="@Url.Action("PropertyDetails", "Home", new {@Id=item.ID,@CategoryName = Item.Title })">
我剛剛注意到,我要像URL StackOverflow的使用
http://stackoverflow.com/questions/43774917/wordpress-blog-type-permalink-in-mvccustom-url-routing
我認爲這肯定URL解決您的問題,請試試這個標題:http://www.jerriepelser.com/blog/generate-seo-friendly-urls-aspnet-mvc/ – stylishCoder
請參閱[多層次的MVC自定義路由](https://stackoverflow.com/a/31958586/181087)。 – NightOwl888