【发布时间】:2018-10-29 18:08:28
【问题描述】:
目前我们的固定链接不正确并妨碍搜索,例如https://example.com/en/blogs/19 - 这应该在 URL 中包含 Google 可以在搜索中获取的字词,而不是用于从 Db 检索的 int id。
假设一篇关于“汽车行业最新消息”的文章,如果我们能够编辑包含关键字的链接,Google 将在算法中给予更多的权重。例如:https://example.com/en/blogs/news/The_Automotive_Industry_Latest - 此链接应指向https://example.com/en/blogs/19
我可以使用以下方法来实现这一点 - 但这是实现这一目标的方法吗?
[Route("en/blogs")]
public class BlogController : Controller
{
[HttpGet("{id}")]
[AllowAnonymous]
public IActionResult GetId([FromRoute] int id)
{
var blog = _context.Blogs.Where(b => b.Id == id);
return Json(blog);
}
[HttpGet("{text}")]
[AllowAnonymous]
public IActionResult GetText([FromRoute] string text)
{
var blog = _context.Blogs.Where(b => b.Title.Contains(text));
if(blog != null)
GetId(blog.Id)
return Ok();
}
}
我猜这仍然不会被谷歌索引为文本,所以必须通过 sitemap.xml 来完成?这一定是一个常见的要求,但我找不到任何文档。
我知道 IIS URL 重写,但如果可能,我想远离这种情况。
【问题讨论】:
-
一种常见的方法是重写你的url以包含id和标题,参见stackoverflow的url示例,例如:
www.example.org/123/the-best-example-ever,要做到这一点,我认为创建一个MapHttpRoute是最简单的...我无法突然为您提供一个示例,我已经有一段时间没有这样做了
标签: c# asp.net-core-2.0 asp.net-core-webapi asp.net-core-routing