Sharing Asp.Net MVC url with facebook



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

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

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

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

完整代码示例

将代码示例复制并粘贴到您的网站。调整值 数据 href 到您的网站网址。接下来使用 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>

请参阅以下解决方案:

测试控制器

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) + "&amp; 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。新闻控制器(

最新更新