net mvc 3并尝试创建一个博客应用程序。我想要一个包含urlheader的url,例如:
博客/详情/第三交
但是,我得到一个这样的网址:
Blog/Details/3?urlHeader=Third-post
我做错了什么?
我的博客控制器如下:
public ViewResult Details(int id, string urlHeader)
{
var blogs = blogrepository.GetBlog(id);
var recentblogs = blogrepository.FindRecentBlogs(5);
var archivelist = blogrepository.ArchiveList();
BlogDetailsViewModels viewModel = new BlogDetailsViewModels { Blog = blogs, RecentBlogs = recentblogs, ArchiveList = archivelist };
return View(viewModel);
}
,路线方法如下:
routes.MapRoute(
"BlogDetail", // Route name
"Blog/Details/{urlHeader}", // URL with parameters
new { controller = "Blog", action = "Details" }, // Parameter defaults
new { urlHeader = "" } // Parameter constraints
);
,链接如下:
@Html.ActionLink(Model.Title , "Details", "Blog", new { id = Model.BlogID, urlHeader = Model.UrlHeader }, null)
答案 0 :(得分:0)
您可以拥有以下路线:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Blog",
"Blog/Details/{id}/{urlHeader}",
new { controller = "Blog", action = "Details", urlHeader = UrlParameter.Optional },
new { id = @"\d+" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}