我的路线定义为:
{theme}/{subtheme}/{urltitle}
用于列出文章详细信息,我想为其他人(而不是开发人员)提供为特定文章创建永久链接的可能性,如示例http://www.whateverdomain/article-about-cars/
:
问题
如果本文有永久链接,如何处理将 {theme}/{subtheme}/{urltitle}
重写为永久链接的请求?
最佳答案
要实现这一目标,您必须做三件事:
- 定义一个足够模糊的路由,供内部数据库调用使用,其中包含一些仅与该文章相关的特定信息。
- 在 Controller 操作中进行数据库调用,不仅允许正常路由,还允许此永久链接(我将在下面向您展示两个不同的 Controller )
- 创建“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"}
);
Controller
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
发出此命令)。如果你不这样做,你将受到搜索之神的惩罚。
更新您的固定链接操作以将您重定向到显示
操作:
public ActionResult ShowByPermalink(string PermalinkName)
{
var article = ArticleRepository.GetBy(PermalinkName);
return RedirectToAction("Show", article);
}
现在创建永久链接。这涉及将 CR
(CRUD
)添加到永久链接存储库,就像我们之前在本文中所做的那样。
以下是一些注意事项:
- 如果您不使用网址路由
{permalinkId}/{permalinkName}
,则必须采用逻辑来确保所有永久链接都是唯一的。 - 您需要在操作中包含处理 404 或格式错误网址的逻辑。
- 创建文章时,您需要逻辑来正确地标记文章并将其与文章一起存储
- 一个custom route constraint (使用
IRouteConstraint
)将是一种在请求时检查用户是否正确获得有效slug的方法。然而,这是更多的工作并导致更多的数据库命中(除非您有良好的缓存机制,但这会导致其他潜在问题) - 如果还有其他定义
{something}
行为的路由,则您的永久链接路由应位于路由的底部,例如:http://example.com/something
。否则,当您不希望发生这种情况时,其他路由将命中您的永久链接路由。如果您有良好的路线限制,则不必担心这一点。
关于asp.net-mvc - 如何允许用户在 ASP.NET MVC 中创建永久链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7039351/