如何为动态生成的页面重写一个 URL 的概述?
Overview of how to rewrite a URL for dynamically generated page?
我正在编写自己的博客系统作为练习,我 运行 遇到了一个我很难找到答案的问题。
为动态生成的页面定义和实施自定义 URL 需要哪些步骤?
我真的只是在寻找必要步骤的高级概述。
我知道的:
我需要将这个 URL 存储在我的数据库中,所以我需要在我的模型中为它添加一个位置。
PostModel.cs(型号)
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string URL { get; set; }
public string IntroText { get; set; }
public string Body { get; set; }
public DateTime Created { get; set; }
public DateTime? Modified { get; set; }
public string Author { get; set; }
public List<Comment> Comments { get; set; }
}
所以,假设我已经成功地将我想要的 URL 存储在数据库中,它看起来像这样:
/this-is-a-test-entry/
目前我正在根据他们的 ID 提取我的条目,但我希望能够定义我自己的 URLs。我真的不知道下一步是什么。 (控制器,路由配置,其他?)
有人可以高层次概述我需要采取的后续步骤吗?
你需要做的是开始根据 URL 提取你的记录(确保你也为 属性 建立索引)然后你基本上可以使用默认路由来做:
public class BlogController
{
public ActionResult Post(string id)
{
//get the post via URL
}
}
然后您可以通过以下导航访问它:
http://www.yoursite.com/Blog/Post/this-is-a-test-entry
我正在编写自己的博客系统作为练习,我 运行 遇到了一个我很难找到答案的问题。
为动态生成的页面定义和实施自定义 URL 需要哪些步骤?
我真的只是在寻找必要步骤的高级概述。
我知道的:
我需要将这个 URL 存储在我的数据库中,所以我需要在我的模型中为它添加一个位置。
PostModel.cs(型号)
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string URL { get; set; }
public string IntroText { get; set; }
public string Body { get; set; }
public DateTime Created { get; set; }
public DateTime? Modified { get; set; }
public string Author { get; set; }
public List<Comment> Comments { get; set; }
}
所以,假设我已经成功地将我想要的 URL 存储在数据库中,它看起来像这样:
/this-is-a-test-entry/
目前我正在根据他们的 ID 提取我的条目,但我希望能够定义我自己的 URLs。我真的不知道下一步是什么。 (控制器,路由配置,其他?)
有人可以高层次概述我需要采取的后续步骤吗?
你需要做的是开始根据 URL 提取你的记录(确保你也为 属性 建立索引)然后你基本上可以使用默认路由来做:
public class BlogController
{
public ActionResult Post(string id)
{
//get the post via URL
}
}
然后您可以通过以下导航访问它:
http://www.yoursite.com/Blog/Post/this-is-a-test-entry