【问题标题】:How do I allow users to create a permalink in ASP.NET MVC?如何允许用户在 ASP.NET MVC 中创建永久链接?
【发布时间】:2011-10-25 17:51:38
【问题描述】:

我的路线定义为:

{theme}/{subtheme}/{urltitle} 用于列出文章详细信息,我想让其他人(不是开发人员)为特定文章创建永久链接,例如 http://www.whateverdomain/article-about-cars/:

问题

如果这篇文章有固定链接,我该如何处理对{theme}/{subtheme}/{urltitle} 的请求被重写为固定链接?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    要实现这一点,您必须做三件事:

    1. 定义一个足够模糊的路由,供内部数据库调用使用,其中包含一些仅与该文章相关的特定信息。
    2. 在您的控制器操作中调用数据库,不仅允许正常路由,还允许此永久链接(我将在下面向您展示两个不同的控制器)
    3. 创建“CRUD”(或者实际上是用户创建自己的永久链接并将其存储在数据库字段中所必需的“CR”(我还将在下面介绍)。

    首先,让我们从我们的文章需要哪些信息开始:​​

    数据库表结构

    Article
    -------
    Id       <--------- 
    Title              \
    Slug               |
    Theme              |
    SubTheme           |
                       |
                       |
    Permalink Table    |
    ---------------    |
    PermalinkId        |
    Name               |
    Slug               /
    ArticleId ---------
    

    路线

    //normal route for article
    routes.MapRoute("article",
        "{theme}/{subtheme}/{slug}",
        new {controller = "article", action = "show" }
        );
    
    //Permalink route for article
    //You may want to create a custom route constraint for this, or place at bottom of routes
    routes.MapRoute("permalinkArticleRoute",
        "{PermaLinkName}",
        new {controller = "article", action = "showbypermalink"}
        );
    

    控制器

    public class ArticleController : Controller
    {
        public ArticleRepository ArticleRepository {get; set;} //DI'd or constructor injected
        public ActionResult Show(Article article)
        {
            var article = ArticleRepository.GetBy(article.theme, article.subtheme, article.slug);
            ArticleViewModel avm = new ArticleViewModel(article);
            return View(avm);
        }
    
        public ActionResult ShowByPermalink(string PermalinkName)
        {
            var article = ArticleRepository.GetBy(PermalinkName);
            ArticleViewModel avm = new ArticleViewModel(article);
            return View(avm);
        }
    
    }
    

    型号

    public class ArticleRepository
    {
        //Uses Linq-to-SQL. Can be adapted for any other ORM. The retrieval logic is the same
        //It's the actual code that differs
    
        public Article GetBy(string theme, string subtheme, string slug)
        {
            return (from a in db.Articles where
                (a.Theme == theme && a.Subtheme == subtheme && a.Slug == slug)
                select a).FirstOrDefault();
        }
    
        public Article GetBy(string permalinkName)
        {
    
            return (from a in db.Articles 
                join p in Permalink on permaLink.ArticleId equals a.Id
                where p.permalinkName == permalinkName
                select a;
        }
    }
    

    允许用户创建永久链接

    最后一部分是供用户创建永久链接的创建/读取功能。请注意,从 SEO 的角度来看,这是“不好的”(当多个链接解析到同一页面时发生的稀释),但您可能想要这样做(无论出于何种原因)。

    使用每种方法,请确保向正确的“当前”URL 发出 301 重定向(RedirectToAction 发出此问题)。否则,你将受到搜索之神的惩罚。

    更新您的永久链接操作以将您重定向到Show 操作:

        public ActionResult ShowByPermalink(string PermalinkName)
        {
            var article = ArticleRepository.GetBy(PermalinkName);
            return RedirectToAction("Show", article);
        }
    

    现在创建永久链接。这涉及将CR(属于CRUD)添加到永久链接存储库,就像我们之前在文章中所做的那样。

    这里有一些注意事项:

    • 如果不使用Url 路由{permalinkId}/{permalinkName},则必须具有确保所有永久链接唯一的逻辑。
    • 您需要在操作中包含逻辑以处理 404 或格式错误的 URL。
    • 创建文章时,您需要逻辑来正确地 slug 文章并将其与文章一起存储
    • custom route constraint(使用IRouteConstraint)将是一种在请求时检查用户是否获得有效 slug 权限的方法。但是,这需要更多的工作并导致更多的数据库命中(除非您有良好的缓存机制,但这会导致其他潜在问题)
    • 如果有其他路由定义了{something} 行为,您的永久链接路由应该位于路由的底部,例如:http://example.com/something。否则,当您不希望发生这种情况时,其他路线将击中您的永久链接路线。如果您有良好的路线限制,请​​不要担心这一点。

    【讨论】:

    • 嗨乔治,非常好的答案,但问题是,如果创建了永久链接,我们应该始终显示永久链接(我认为是通过 Url 重写)而不是文章的默认 url。在这种情况下,我认为我需要实现一个 HttpHandler 来检查是否为文章创建了永久链接,如果是,则将 url 重写为 /permalinkcreated/ 对吗?问题是对于 SEO,我们不希望相同内容的不同 url。谢谢
    • @Jose3d 如果您发出RedirectToAction,它将重写用户浏览器中的Url(我在上面详细说明了这一点)。
    • 嗨乔治,我认为 RedirectToAction 是 Response.Redirect 的替代品(据我所知),它不会完全重写,它会向您的浏览器返回一个 Http Code 302(对象已移动)。出于这个原因,我想知道我是否应该在 HttpModule 中使用 Context.RewritePath 来进行重写而不是重定向用户。
    • 乔治你的回答真的很好,我可能会实现这样的东西。但是我仍然想问您对此的看法:除了产品、类别等。还有添加自己的“信息”页面的功能,只是静态内容。从性能的角度来看,创建静态 .(cs)html 文件会更好。我将如何通过单个操作打开不同的 .cshtml 文件?比如 'domain.com/info/contact' 和 'domain.com/info/aboutus' 这些html文件中的内容是静态的。
    • 有时你写 PermalLink,有时你写 PermaLink
    猜你喜欢
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 2012-06-30
    相关资源
    最近更新 更多