-1

我的网站中有一个页面显示帖子详细信息。网址看起来像:mydomain/review/reviewdetails/id

注意:这里 id 是 mvc 参数,它将为每个帖子动态更改。

现在的问题是,当我创建一个新帖子时,正在生成一个新的 id,并且由于 id 已更改,新 id 的 url 与以前的 url 是分开的。因此,我需要使用 facebook 调试器调试 url,以便在共享时为帖子获取正确的图像和描述。

我被困住了。提前感谢您的帮助。

4

2 回答 2

0

请看下面的解决方案:

*** 测试控制器

public ActionResult TestId(int? id)
{
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        News news = db.News.Find(id);
        if (news == null)
        {
            return HttpNotFound();
        }
        return View(news);
    }

*** 看法

@{
     string @url = "http://YOUR_URL/TEST/TestID/" + Html.DisplayFor(model => model.IdNews);
    string @fb_shared_page = "https://www.facebook.com/sharer/sharer.php?u=http%3A%2f%2fYOUR_URL%2fTEST%2fTestId2f" + Html.DisplayFor(model => model.IdNews) + "& src = sdkpreparse";
}

<div class="fb-share-button" data-href=" @url" data-layout="button" data-size="small">
   <a target="_blank" href="@fb_shared_page" class="fb-xfbml-parse-ignore">
      Shared
   </a>
 </div>

笔记:

  • 测试是你的控制器
  • TestId 是您在 TESTController 上的函数,其 id 参数为整数
  • IdNews 是您从数据库表名 News 中检索的 Id(参见 db.News 控制器)
于 2019-05-06T15:21:49.623 回答
0

完整代码示例

将代码示例复制并粘贴到您的网站。将值 data-href 调整为您的网站 URL。接下来使用 og:*** 元标记来调整您的链接预览。og:url 和 data-href 应该使用相同的 URL。

<html>
<head>
  <title>Your Website Title</title>
    <!-- You can use Open Graph tags to customize link previews.
    Learn more: https://developers.facebook.com/docs/sharing/webmasters -->
  <meta property="og:url"           content="https://www.your-domain.com/your-page.html" />
  <meta property="og:type"          content="website" />
  <meta property="og:title"         content="Your Website Title" />
  <meta property="og:description"   content="Your description" />
  <meta property="og:image"         content="https://www.your-domain.com/path/image.jpg" />
</head>
<body>

  <!-- Load Facebook SDK for JavaScript -->
  <div id="fb-root"></div>
  <script>(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.0";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));</script>

  <!-- Your share button code -->
  <div class="fb-share-button" 
    data-href="https://www.your-domain.com/your-page.html" 
    data-layout="button_count">
  </div>

</body>
</html>
于 2019-04-24T20:16:28.237 回答