asp .net中的博客自定义urlheader

时间:2012-07-28 09:04:25

标签: asp.net-mvc-3 routes

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)

1 个答案:

答案 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 }
    );
}