asp.net-mvcfacebookfacebook-share

How to Create a Share to Facebook button on a localhost application


I have a local host application in which a user can enter a blog to website by logging in and anyone can read the contents of the blog. while creating the blog the application auto generates a url based on the name of the blog. The URL is a basically a string e.g. Name: "New Blog" URL: "New-Blog".

Now when a person views the blog i want to give him the ability to share the blog on his Facebook account. So i used a Facebook code generator to ge teh code for the share button.

    <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_GB/sdk.js#xfbml=1&version=v3.2';
         fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
    </script>


     <div class="fb-share-button" data-href="http://localhost/Blog/{{BlogList.Url}}" data-layout="button" data-size="large" data-mobile-iframe="false"><a target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2Fplugins%2F&amp;src=sdkpreparse" class="fb-xfbml-parse-ignore"></a></div>

This was the code that was generated. Now i realize that this is not going to work with the url i am passing: http://localhost/Blog/{{BlogList.Url}}

Now what url should i use to be able to share this on facebook while also get the url name that was auto generated.


Solution

  • The URL must be absolute, and it must be public:

    http://yourdomain/Blog/xxx
    

    You need to replace localhost with your domain, and you may want to use https instead of http. You should not use http anymore, but that is just a side note.