如何在ASP.NET MVC中实现Disqus?

时间:2022-11-21 03:29:41

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).

该网站的部分设计是对一组明显具有自己IDss的不同“项目”进行评论。 (即/食谱/ 23或设备/ 16等等)。

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.

最初,我已经指定了带标签的评论系统。然而,项目发起人已经回来询问是否很容易将Disqus纳入混合。我之前使用过Joomla(从未在.NET中)并且认为这将是一个好主意,因为默认情况下,评论会通过常用的社交网络媒体自动分配。

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.

在ASP.NET MVC上设置Disqus的实现是否相当轻松无缝地工作? ASP.NET MVC中是否有教程或者如何使用Disqus的解决方案?我已经看过这个例子,到目前为止已阅读过文档。

4 个解决方案

#1


15  

Aparrently there is a wonderful nuget package for Disqus.

很明显,Disqus有一个很棒的nuget包。

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

http://disqusforwebpages.codeplex.com/documentation

#2


6  

I'm electing to use the async JavaScript load approach (as opposed to using the fullblown API methods). here's how simple it is using this in ASPNET MVC (It also works for ASP.NET):

我选择使用异步JavaScript加载方法(而不是使用完整的API方法)。这是在ASPNET MVC中使用它的简单方法(它也适用于ASP.NET):

From the documentation:

从文档:

<!-- add the div to receive the comments via ajax -->
<div id="disqus_thread"></div>

<!-- the required javascript link to disqus -->
<script type="text/javascript">
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'mydisqusname';

    // Question pour XWiki : ici il faut que je configure un identifier
    // c'est comme un sujet de Mail. Il faudrait que je mette par exemple
    // l'url de la page XWiki afin que les commentaires soient regroupes

    // ensemble par article. Bref est ce que vous pouvez me mettre un ID ?

    var disqus_identifier = 'comments-league-<%= Html.Encode(Model.ID) %>';
    var disqus_url = '<%= HttpContext.Current.Request.Url %>';
    // using disqus_developer = 1 helps to debug to localhost etc..
    var disqus_developer = 1;


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

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

That's all there is to it.

这里的所有都是它的。

#3


3  

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.

如果您可以使用disqus品牌,那么javascript API调用就是您的选择。如果你需要进行更深入的集成 - 或者需要做一些事情,比如确保你的评论留在你的网站上 - 你可能想看看我写的名为disqussharp的小库 - 它是一个相当完整的包装器,围绕v 1.1 disqus API,可以用于很多事情。

#4


2  

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

这是针对Disqus和Disqus的extesion方法的副本:

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

首先,Disqus帮手(向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:

然后是strongdebate版本:

/// <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...

请享用...

#1


15  

Aparrently there is a wonderful nuget package for Disqus.

很明显,Disqus有一个很棒的nuget包。

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

http://disqusforwebpages.codeplex.com/documentation

#2


6  

I'm electing to use the async JavaScript load approach (as opposed to using the fullblown API methods). here's how simple it is using this in ASPNET MVC (It also works for ASP.NET):

我选择使用异步JavaScript加载方法(而不是使用完整的API方法)。这是在ASPNET MVC中使用它的简单方法(它也适用于ASP.NET):

From the documentation:

从文档:

<!-- add the div to receive the comments via ajax -->
<div id="disqus_thread"></div>

<!-- the required javascript link to disqus -->
<script type="text/javascript">
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'mydisqusname';

    // Question pour XWiki : ici il faut que je configure un identifier
    // c'est comme un sujet de Mail. Il faudrait que je mette par exemple
    // l'url de la page XWiki afin que les commentaires soient regroupes

    // ensemble par article. Bref est ce que vous pouvez me mettre un ID ?

    var disqus_identifier = 'comments-league-<%= Html.Encode(Model.ID) %>';
    var disqus_url = '<%= HttpContext.Current.Request.Url %>';
    // using disqus_developer = 1 helps to debug to localhost etc..
    var disqus_developer = 1;


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

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

That's all there is to it.

这里的所有都是它的。

#3


3  

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.

如果您可以使用disqus品牌,那么javascript API调用就是您的选择。如果你需要进行更深入的集成 - 或者需要做一些事情,比如确保你的评论留在你的网站上 - 你可能想看看我写的名为disqussharp的小库 - 它是一个相当完整的包装器,围绕v 1.1 disqus API,可以用于很多事情。

#4


2  

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

这是针对Disqus和Disqus的extesion方法的副本:

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

首先,Disqus帮手(向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:

然后是strongdebate版本:

/// <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...

请享用...