如何允许用户在ASP.NET MVC中创建永久链接?

Jos*_*e3d 5 asp.net-mvc asp.net-mvc-3

我的路线定义为:

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

{theme}/{subtheme}/{urltitle}如果本文有永久链接,我如何处理要重写为永久链接的请求?

Geo*_*ker 7

要实现这一目标,您需要做三件事:

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

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

数据库表结构

Article
-------
Id       <--------- 
Title              \
Slug               |
Theme              |
SubTheme           |
                   |
                   |
Permalink Table    |
---------------    |
PermalinkId        |
Name               |
Slug               /
ArticleId ---------
Run Code Online (Sandbox Code Playgroud)

路线

//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"}
    );
Run Code Online (Sandbox Code Playgroud)

调节器

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

}
Run Code Online (Sandbox Code Playgroud)

模型

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

允许用户创建永久链接

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

对于每种方法,请确保将301重定向(RedirectToAction发出此问题)发布到正确的"当前"URL.如果你不这样做,你将受到搜索神的惩罚.

更新固定链接操作以将您重定向到Show操作:

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

现在创建永久链接.这涉及将CR(of CRUD)添加到Permalink存储库,就像我们之前使用文章一样.

以下是一些警告:

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