通过路由的 ASP.NET MVC 博客 URL

发布于 2024-12-14 03:20:43 字数 337 浏览 4 评论 0原文

假设我有一个新的 MVC 站点,我想通过以下方式实现 URL。从路由的角度来看,执行此操作的最佳方法有哪些想法?我会将帖子存储在基本数据库表中。从控制器/动作的角度来看,我希望使其尽可能简单。

/2011/(2011 年以来的所有帖子)

/2011/11/(2011 年 11 月以来的所有帖子)

/2011/11/07(2011 年 11 月 7 日以来的所有帖子)

/2011/11/07/exact-post-title

/exact-帖子标题

/about

/archive

/tag/whatever-tag

Let's say I had a fresh MVC site and I wanted to implement URLs in the following manner. What are some thoughts on the best way to do this from a routing perspective? I would be storing posts in a basic database table. I would want to keep it as simple as possible from a controller/action perspective.

/2011/ (all posts from 2011)

/2011/11/ (all posts from November 2011)

/2011/11/07 (all posts from November 7th 2011)

/2011/11/07/exact-post-title

/exact-post-title

/about

/archive

/tag/whatever-tag

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶葆Ⅱㄣ 2024-12-21 03:20:43
// matches /2011/11/07/exact-post-title
routes.MapRoute(
    "ArticleDetails",
    "{yyyy}/{mm}/{dd}/{title}",
    new { controller = "Articles", action = "Details",
    new { yyyy = @"(19|20)\d\d.", mm = @"\d\d", dd = @"\d\d" }
);

// matches /2011/11/07
routes.MapRoute(
    "ArticlesByDay",
    "{yyyy}/{mm}/{dd}",
    new { controller = "Articles", action = "ByDay",
    new { yyyy = @"(19|20)\d\d.", mm = @"\d\d", dd = @"\d\d" }
);

// matches /2011/11
routes.MapRoute(
    "ArticlesByMonth",
    "{yyyy}/{mm}",
    new { controller = "Articles", action = "ByMonth",
    new { yyyy = @"(19|20)\d\d.", mm = @"\d\d" }
);

// matches /2011
routes.MapRoute(
    "ArticlesByYear",
    "{yyyy}",
    new { controller = "Articles", action = "ByYear",
    new { yyyy = @"(19|20)\d\d." }
);

/exact-post-title 路线存在某种问题。这几乎与您发送的任何内容相匹配。为了解决这个问题,您必须将所有其他可能的路线放在该路线的前面。您也可以在所有这些路由前面加上 /blog 或 /articles 来修复它:

routes.MapRoute(
    "ExactPostTitle",
    "articles/{title}", 
    new { controller = "Articles", action = "Details" }
);

现在它不会与以下内容冲突:

routes.MapRoute(
    "About",
    "about",
    new { controller = "Home", action = "About" }
);

存档将是类似的:

routes.MapRoute(
    "Archive",
    "archive",
    new { controller = "Home", action = "Archive" }
);

最后是标签路由:

routes.MapRoute(
    "Tag",
    "tag/{tagtext}",
    new { controller = "Tag", action = "Index" }
);

您可能需要调整以下顺序:路线,但一般来说,您总是首先想要最具体的路线。

如果您有以下路由:

routes.MapRoute(
    "ExactTitle",
    "{title}",
    new { controller = "Articles", action = "Details" }
);

routes.MapRoute(
    "About",
    "about",
    new { controller = "Home", action = "About" }
);

第一个路由与 /about 匹配,因此如果它们按此顺序,您将遇到问题。

// matches /2011/11/07/exact-post-title
routes.MapRoute(
    "ArticleDetails",
    "{yyyy}/{mm}/{dd}/{title}",
    new { controller = "Articles", action = "Details",
    new { yyyy = @"(19|20)\d\d.", mm = @"\d\d", dd = @"\d\d" }
);

// matches /2011/11/07
routes.MapRoute(
    "ArticlesByDay",
    "{yyyy}/{mm}/{dd}",
    new { controller = "Articles", action = "ByDay",
    new { yyyy = @"(19|20)\d\d.", mm = @"\d\d", dd = @"\d\d" }
);

// matches /2011/11
routes.MapRoute(
    "ArticlesByMonth",
    "{yyyy}/{mm}",
    new { controller = "Articles", action = "ByMonth",
    new { yyyy = @"(19|20)\d\d.", mm = @"\d\d" }
);

// matches /2011
routes.MapRoute(
    "ArticlesByYear",
    "{yyyy}",
    new { controller = "Articles", action = "ByYear",
    new { yyyy = @"(19|20)\d\d." }
);

There is sort of a problem with the /exact-post-title route. That is going to match pretty much anything you send it. You would have to put every other possible route in front of that one in order to fix that. You could also just prefix all of these routes with /blog or /articles to fix it:

routes.MapRoute(
    "ExactPostTitle",
    "articles/{title}", 
    new { controller = "Articles", action = "Details" }
);

Now it won't conflict with the following:

routes.MapRoute(
    "About",
    "about",
    new { controller = "Home", action = "About" }
);

Archive would be similar:

routes.MapRoute(
    "Archive",
    "archive",
    new { controller = "Home", action = "Archive" }
);

And finally the Tag route:

routes.MapRoute(
    "Tag",
    "tag/{tagtext}",
    new { controller = "Tag", action = "Index" }
);

You might have to play around with the order of the routes, but in general you always want the most specific routes first.

If you had the following routes:

routes.MapRoute(
    "ExactTitle",
    "{title}",
    new { controller = "Articles", action = "Details" }
);

routes.MapRoute(
    "About",
    "about",
    new { controller = "Home", action = "About" }
);

The first route matches /about so you are going to run into problems if they are in that order.

薄荷港 2024-12-21 03:20:43

我的解决方案是这样的:

首先在 RouteConfig.cs 中包含

          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        **routes.MapMvcAttributeRoutes();** this is very important 
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

您需要的路由,然后在控制器操作中包含:

    [Route("Posts/{year}/{month}/{day}/{title}")]
    public ActionResult Post(int year, int month, int day, string title)
    {
        return View();
    }

    [Route("Posts/{year}/{month}/{day}")]
    public ActionResult Post(int year, int month, int day)
    {
        return View();
    }

    [Route("Posts/{year}/{month}")]
    public ActionResult Post(int year, int month)
    {
        return View();
    }

    [Route("Posts/{yearOrTitle}")]
    public ActionResult Post(string yearOrTitle)
    {
//logical to search year or Title
        return View();
    }

My solution is this :

first include in RouteConfig.cs

          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        **routes.MapMvcAttributeRoutes();** this is very important 
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

after include in your controller action the route that you need:

    [Route("Posts/{year}/{month}/{day}/{title}")]
    public ActionResult Post(int year, int month, int day, string title)
    {
        return View();
    }

    [Route("Posts/{year}/{month}/{day}")]
    public ActionResult Post(int year, int month, int day)
    {
        return View();
    }

    [Route("Posts/{year}/{month}")]
    public ActionResult Post(int year, int month)
    {
        return View();
    }

    [Route("Posts/{yearOrTitle}")]
    public ActionResult Post(string yearOrTitle)
    {
//logical to search year or Title
        return View();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文