【问题标题】:Sharing Asp.Net MVC url with facebook与 facebook 共享 Asp.Net MVC url
【发布时间】:2019-04-23 11:20:49
【问题描述】:

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

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

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

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

【问题讨论】:

  • 您究竟需要什么帮助?不太清楚实际的 question 应该在这里。请阅读How to Ask
  • 请点击以下链接: mydomain/review/reviewdetails/2 mydomain/review/reviewdetails/3 对于这两个链接,我需要使用 facebook 调试器来收集实际的标题、图像和描述,同时分享它们都来自一个页面。我说清楚了吗?
  • “虽然它们都来自一个页面” - 这是两个不同的 URL,因此它们被视为两个不同的 Open Graph 对象。 “单页”在这方面没有任何意义。如果大多数时候无法在第一次共享上获取数据,这通常意味着您的服务器响应速度太慢。解决方法是在您发布新 URL 时通过 API(研究它)自动触发抓取。

标签: c# asp.net facebook model-view-controller


【解决方案1】:

完整代码示例

将代码示例复制并粘贴到您的网站。调整值 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>

【讨论】:

  • 嗨菲利普斯,感谢您的回答,这是可观的。实际上我之前做过这一切,事实是当我的链接更改为 mydomain/postdetails/13 到 mydomain/postdetails/14 时,图像不会显示,直到我调试新的 url。
【解决方案2】:

请看下面的解决方案:

*** 测试控制器

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>

注意:

  • TEST 是您的控制器
  • TestId 是您在 TESTController 上的函数,其 id 参数为整数
  • IdNews 是您从数据库表名 News 中检索的 Id(参见 db.News 控制器)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多