Disqus Integration In ASP.NET MVC

As you all know, nowadays comments are part of a blog. We stored data in the database or XML file in the past. It is a big headache to handle the data maintenance and storage space, which is costly. To overcome this, we have an option in Disqus. It is a most trusted comments plugin and easy to use even without sound knowledge in the programming.

Background

Disqus is a blog comment hosting service that offers a platform for social integration, social networking, user profiles, analytics, mobile commenting and so on. It was founded in 2007 by Daniel Ha and Jason Yan as a Y Combinator startup. It’s simple and one can quickly learn and even non-techies can learn it. It is directly integrated with the most popular services like WordPress, Tumblr, Squarespace and so on.

Setting up Disqus Environment

First, we have to register an account and to log into the forum. If we can setup up Disqus on a new site with entering the site name, then one needs to choose unique Disqus URL, a category, and then click Next button.
 
Once you have successfully created your own site on Disqus, you can choose your platform like “Universal Code”, as shown below in the screenshots:

  
 

If we select a universal code option, then you can learn the setup instructions for the universal code in the following steps:

Step 1

We can add the code, given below, where you’d like Disqus to load.

  1. <div id="disqus_thread"></div>  
  2. <script>  
  3. /** 
  4. * RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS. 
  5. * LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables 
  6. */  
  7. /* 
  8. var disqus_config = function () { 
  9. this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable 
  10. this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable 
  11. }; 
  12. */  
  13. (function() { // DON'T EDIT BELOW THIS LINE  
  14. var d = document, s = d.createElement('script');  
  15.   
  16. s.src = '//san2blog.disqus.com/embed.js';  
  17.   
  18. s.setAttribute('data-timestamp', +new Date());  
  19. (d.head || d.body).appendChild(s);  
  20. })();  
  21. </script>  
  22. <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript>  
Step 2

We are able to edit the recommended configuration variables section, using CMS or the platform’s dynamic values. You may want to learn why defining an identifier and a URL is important to prevent the duplicate threads.

How to display comment count

We can add the code, given below, before your site’s closing </body> tag

  1. <a href="http://example.com/bar.html#disqus_thread">Link</a>.  

How to integrate the Disqus with Sample MVC Application

We are going to discuss how to integrate the Disqus commenting plugin with the sample Application and show the demo in MVC. We will see the step by step guidelines for the sample Application creation here.

Step 1

Open Visual Studio 2015, go to file menu, point to new and then click new project. New ASP.NET project window will open, you can select an ASP.NET Web Application and type Project Name DisqusCommentDemo. Choose the project location path and then click on the OK button.

A new ASP.NET project window will open, you can select an empty template and select an MVC folder with no authentication and then click OK button.

 
Step 2

How to add Layout and ViewStart page,

Go to View folder, right click the View folder and point tto Add, followed by clicking the New Folder. Now, you can change new folder name as “Shared” folder.

Go to the shared folder and right click the shared folder and point Add, followed by clicking the MVC 5 Layout page. You can type the item name as _Layout and click OK button.

Go to View folder, right click the View folder, point Add, followed by clicking the Add View. You can type the view name as _ViewStart Template as leave it empty and click Add button.

Similarly, you can add HomeController.cs under the Controller folder and Index.cshtml under the View, inside a home folder.  

Step 3

When you want to load the Disqus comment plugin in the file, you can add the following line:

  1. <div id="disqus_thread"></div>  

Index.cshtml

  1. @{  
  2.     ViewBag.Title = "Home";  
  3.     Layout = "~/Views/Shared/_Layout.cshtml";  
  4. }  
  5.   
  6. <h2>Home</h2>  
  7.   
  8. <div id="disqus_thread"></div>  

We have added layout reference in an index page. Hence, we are able to add Disqus plugin scripts to _Layout.cshtml. We also have a unique identifier name for the Disqus plugin, registered in the forum with the help of the following lines:

  1. var d = document, s = d.createElement('script');  
  2. s.src = '//san2blog.disqus.com/embed.js';  

Finally, we can add one more script for comment count. The line that follows is used before closing </body> tag 

  1. <script id="dsq-count-scr" src="//san2blog.disqus.com/count.js" async></script>  

_Layout.cshtml  

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4. <head>  
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">  
  6.     <meta name="viewport" content="width=device-width" />  
  7.     <title>@ViewBag.Title</title>  
  8.     <link href="~/Style/bootstrap.min.css" rel="stylesheet">  
  9.     <script>  
  10. /** 
  11. * RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS. 
  12. * LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables 
  13. */  
  14. /* 
  15. var disqus_config = function () { 
  16. this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable 
  17. this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable 
  18. }; 
  19. */  
  20. (function() { // DON'T EDIT BELOW THIS LINE  
  21. var d = document, s = d.createElement('script');  
  22.   
  23. s.src = '//san2blog.disqus.com/embed.js';  
  24.   
  25. s.setAttribute('data-timestamp', +new Date());  
  26. (d.head || d.body).appendChild(s);  
  27. })();  
  28.     </script>  
  29.     <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript>  
  30. </head>  
  31. <body>  
  32.  <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">  
  33.         <div class="container">  
  34.             <div class="navbar-header">  
  35.                 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-example">  
  36.                     <span class="sr-only">Toggle navigation</span>  
  37.                     <span class="icon-bar"></span>  
  38.                     <span class="icon-bar"></span>  
  39.                     <span class="icon-bar"></span>  
  40.                 </button>  
  41.                 <a class="navbar-brand" href="#">Disqus Comment Apps</a>  
  42.             </div>  
  43.             <div class="collapse navbar-collapse" id="navbar-collapse-example">  
  44.                 <ul class="nav navbar-nav">  
  45.                     <li>  
  46.                         <a href="#">Home</a>  
  47.                     </li>  
  48.                     <li>  
  49.                         <a href="#">About</a>  
  50.                     </li>  
  51.                     <li>  
  52.                         <a href="#">Contact</a>  
  53.                     </li>  
  54.                 </ul>  
  55.             </div>  
  56.         </div>  
  57. </nav>  
  58.     <div class="container">  
  59.         <div class="row">  
  60.             <div class="col-lg-12">  
  61.                 @RenderBody()  
  62.   
  63.             </div>  
  64.         </div>  
  65.         <footer>  
  66.             <div class="row">  
  67.                 <div class="col-lg-12">  
  68.                     <p>Copyright © San2debug 2016</p>  
  69.                 </div>  
  70.             </div>  
  71.             <!-- /.row -->  
  72.         </footer>  
  73.         </div>  
  74.     <script src="~/scripts/jquery.js"></script>  
  75.     <script src="~/scripts/bootstrap.min.js"></script>  
  76.     <script id="dsq-count-scr" src="//san2blog.disqus.com/count.js" async></script>  
  77. </body>  
  78. </html>  

Step 4

Now, if you can run the sample Application, you can see how a comment looks, as shown below:

 
Note
 
You can also read this article in my blog here.

Reference

Conclusion

I hope, I have covered all the required things. If you find anything that I missed in this article, please let me know. Please share your valuable feedback or suggestions.