我有一个拥有博客页面的应用程序。我在博客页面上添加了一个社交共享按钮。但是我的问题是如何获取编码的URL并将其传递给社交共享URL。我指的是这篇文章。 https://www.c-sharpcorner.com/UploadFile/cd7c2e/sharing-url-on-facebok-using-Asp-Net/。在本文中,URL是预定义的。但我需要使网址动态化。如何获取该URL并传递给社交共享URL?
这是我的代码:
select name,
sum(case when subject = 'math' then marks else 0 end) as group1,
sum(case when subject <> 'math' then marks else 0 end) as group2
from table t
group by name;
答案 0 :(得分:0)
要为博客创建共享按钮,您需要要共享的页面的URL。要获取页面的URL,可以使用 @ Request.Url 。在某些链接中,您还需要从模型中添加其他信息。让我们一一看一下。
Facebook:
<a href="https://www.facebook.com/sharer/sharer.php?u=@Request.Url">
<i class="fa fa-facebook"></i>
</a>
Twitter:
<a href="@string.Concat("https://twitter.com/home?status=","%0A",@Request.Url)">
<i class="fa fa-Twitter"></i>
</a>
Linkedin:
<a href="@string.Concat("https://www.linkedin.com/shareArticle?mini=true&url=",@Request.Url, "&title=",@Model.TitleOfPost,"&summary=",@Model.PostSummary )">
<i class="fa fa-Linkedin"></i>
</a>
Google Plus:
<a href="https://plus.google.com/share?url=@Request.Url">
<i class="fa fa-google-plus"></i>
</a>
答案 1 :(得分:0)
假设您已使用viewmodel属性提供了网址,如下例所示:
public class Mybolgs
{
public string UrlAddress { get; set; }
public string Title { get; set; }
public string Summary { get; set; }
}
并在控制器操作中分配站点URL,如下所示:
var model = new Mybolgs();
model.UrlAddress = "https://example.com/path/to/share";
// other properties
return View(model);
然后,您可以使用@Url.Encode()
帮助程序将它们作为查询字符串参数插入锚点链接的href
属性内:
<!-- Facebook -->
<a href="http://www.facebook.com/sharer/sharer.php?s=100&p[url]=@Url.Encode(Model.UrlAddress)&p[images]=&p[title]=@Model.Title&p[summary]=@Model.Summary">
<!-- Google Plus -->
<a href="https://plus.google.com/share?url=@Url.Encode(Model.UrlAddress)">
<!-- Pinterest -->
<a href="http://pinterest.com/pin/create/button/?url=@Url.Encode(Model.UrlAddress)&description=@Model.Title">
如果要在控制器操作中获取当前页面URL并将其作为编码URL传递给锚链接,请使用HttpContext.Current.Request.Url.AbsoluteUri
(或更简单的Request.Url.AbsoluteUri
)代替硬编码URL字符串:< / p>
var model = new Mybolgs();
model.UrlAddress = HttpContext.Current.Request.Url.AbsoluteUri;
// other properties
return View(model);