Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement Disqus in ASP.NET MVC?

I'm in the middle of specifying the build/buy trade-offs for a public website and have hit a rather interesting avenue.

Background

Part of the design of the website is to incorporate comments against a set of different 'items' which obviously have their own IDss. (i.e. /recipes/23 or equipment/16 etc, etc).

Initially, i had specified a comments system with tags. However, the project sponsor has come back and asked if it would be easy to incorporate Disqus into the mix. I've used this before with Joomla (never in .NET) and think that it'll be a great idea as the comments are automatically distrubuted via the usual social network mediums by default.

Question

Is it fairly painless to set up an implementation of Disqus on ASP.NET MVC that works seamlessly? Are there tutorials or examples of a working Disqus solution in ASP.NET MVC? I've seen this example and have read the documentation so far.

like image 632
jim tollan Avatar asked Jan 19 '11 12:01

jim tollan


3 Answers

Aparrently there is a wonderful nuget package for Disqus.

Install-Package Disqus.Helper

And then it's as easy as sticking this in your view, section, or partial view somewhere...

@Disqus {
     Disqus.Initialize("YourForumShortName")
}
@Discus.ShowComments("PageIdentifierOfYourChoice")

http://disqusforwebpages.codeplex.com/documentation

like image 57
jcreamer898 Avatar answered Oct 04 '22 13:10

jcreamer898


Here's a copule of extesion methods that target both Disqus and IntenseDebate:

firstly, the Disqus helper (with a nod to PieterG):

/// <summary>
/// Display Comments for Post
/// </summary>
/// <param name="html"></param>
/// <param name="postIdentifier"></param>
/// <returns></returns>
public static MvcHtmlString DisqusScript(this HtmlHelper html, string postIdentifier)
{
    var commentsBuilder = new StringBuilder();
    var id = Config.DisqusId; // get the Disqus id from config file
    var devMode = Config.DevMode; // get the devmode ('0' or '1') from config file

    commentsBuilder.Append("<div id=\"disqus_thread\"></div>");

    commentsBuilder.Append("<script type=\"text/javascript\">");
    commentsBuilder.Append("var disqus_shortname = '" + id + "';");
    commentsBuilder.Append("var disqus_identifier = '" + postIdentifier + "';");
    commentsBuilder.Append("var disqus_url = '" + HttpContext.Current.Request.Url + "';");
    commentsBuilder.Append("var disqus_developer = '" + devMode + "';");

    /* * * DON'T EDIT BELOW THIS LINE * * */
    commentsBuilder.Append("(function () {");
    commentsBuilder.Append("var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;");
    commentsBuilder.Append("dsq.src = 'http://" + id + ".disqus.com/embed.js';");

    commentsBuilder.Append("(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);");
    commentsBuilder.Append("})();");
    commentsBuilder.Append("</script>");

    commentsBuilder.Append("<noscript>");
    commentsBuilder.Append("Please enable JavaScript to view the <a href=\"http://disqus.com/?ref_noscript\">comments");
    commentsBuilder.Append("powered by Disqus.</a>");
    commentsBuilder.Append("</noscript>");
    return MvcHtmlString.Create(commentsBuilder.ToString());
}

and then the intensedebate version:

/// <summary>
/// Display Comments for Post
/// </summary>
/// <param name="html"></param>
/// <param name="postIdentifier"></param>
/// <returns></returns>
public static MvcHtmlString IntenseDebateScript(this HtmlHelper html, string postIdentifier)
{
    var commentsBuilder = new StringBuilder();
    var id = Config.IntenseDebateId; // get the IntenseDebate id from config file

    // js variables for embedded wrapper script
    commentsBuilder.Append("<script type=\"text/javascript\">");
    commentsBuilder.Append("var idcomments_acct = '" + id + "';");
    commentsBuilder.Append("var idcomments_post_id = '" + postIdentifier + "';");
    commentsBuilder.Append("var idcomments_post_url = '" + HttpContext.Current.Request.Url + "';");
    commentsBuilder.Append("</script>");

    /* * * DON'T EDIT BELOW THIS LINE * * */
    commentsBuilder.Append("<script type=\"text/javascript\" ");
    commentsBuilder.Append("src = 'http://www.intensedebate.com/js/genericCommentWrapperV2.js'>");
    commentsBuilder.Append("</script>");

    // add the target span for the comments
    commentsBuilder.Append("<span id='IDCommentsPostTitle' style='display:none'></span>");

    commentsBuilder.Append("<noscript>");
    commentsBuilder.Append("Please enable JavaScript to view the IntenseDebate comments");
    commentsBuilder.Append("</noscript>");
    return MvcHtmlString.Create(commentsBuilder.ToString());
}

usage in either case:

// for intensedebate
<%=Html.IntenseDebateScript("comments-id-that-i-can-use") %>

//and for disqus
<%=Html.DisqusScript("another-comments-id-that-i-can-use") %>

enjoy...

like image 30
jim tollan Avatar answered Oct 04 '22 13:10

jim tollan


If you are OK with having the disqus branding, the javascript API call is the way to go. If you need to have a deeper integration -- or need to do things like ensure your comments stay with your site -- you might want to check out the little library I wrote called disqussharp -- its a fairly complete wrapper around v 1.1 of the disqus API and can be used for lots of things.

like image 44
Wyatt Barnett Avatar answered Oct 04 '22 11:10

Wyatt Barnett