QueryString始终在webform

时间:2017-01-02 04:56:43

标签: c# asp.net

我使用Facebook分享功能分享图片网址,如果点击Facebook上的图片,则将用户返回到同一页面。

我有一些后端逻辑,用户将在ImageShare.aspx页面上完成某些操作后重定向到页面

下面是分享时的示例网址

http://www.facebook.com/sharer/sharer.php?u=http://domain.com/ImageShare.aspx?img=f3082420-8b92-4301-b093-bf8ce3f91772.jpg&slide=1&lg=1&pageurl=http://domain.com/photo/2016/album-images

CODE

System.Text.StringBuilder sbMetaDetails = new System.Text.StringBuilder();
string urlReferer;
string pageURL=null;
if (HttpContext.Current.Request.UrlReferrer != null)
{
pageURL = Request["pageurl"] + "#lg=1&slide=" + Request["slide"];
.....
.....
.....
}

Request["pageurl"] AND Request["slide"]网址始终返回null。我不确定为什么会这样。

我无法弄清楚为什么我为这些值Request["pageurl"] AND Request["slide"]

获取null

更新:

我注意到这个问题似乎不是查询。当我检查表单的来源时,它显示它只显示动作网址,只有img查询字符串 <form method="post" action="./ImageShare.aspx?img=f3082420-8b92-4301-b093-bf8ce3f91772.jpg" id="form1">

我正在尝试将以下网址传递给facebook http://www.facebook.com/sharer/sharer.php?u=http://domain.com/ImageShare.aspx?img=f3082420-8b92-4301-b093-bf8ce3f91772.jpg&slide=1&lg=1&pageurl=http://domain.com/photo/2016/album-images

但我的og:url标签并不像我想要的那样。页面来源

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title><meta property='og:image' content='http://www.domain.com/Images/gallery/images/f3082420-8b92-4301-b093-bf8ce3f91772.JPG'/>
<meta property='og:type' content='article'/>
<meta property='og:title' content='Title of the image'/>
<meta property='og:description' content='Description of teh image'/>
<meta property='og:url' content='#lg=1&slide='/>
<meta property='og:site_name' content='http://www.domain.com'/>
</head>
<body>
    <form method="post" action="./ImageShare.aspx?img=f3082420-8b92-4301-b093-bf8ce3f91772.jpg" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTU3MzMzMjM1OQ9kFgQCAQ9kFgICAQ8WAh4EVGV4dAXIAzxtZXRhIHByb3BlcnR5PSdvZzppbWFnZScgY29udGVudD0naHR0cDovL3d3dy5oYWJ0b29yVGVubmlzLmNvbS9JbWFnZXMvZ2FsbGVyeS9pbWFnZXMvZjMwODI0MjAtOGI5Mi00MzAxLWIwOTMtYmY4Y2UzZjkxNzcyLkpQRycvPg0KPG1ldGEgcHJvcGVydHk9J29nOnR5cGUnIGNvbnRlbnQ9J2FydGljbGUnLz4NCjxtZXRhIHByb3BlcnR5PSdvZzp0aXRsZScgY29udGVudD0nQWduZXMgQnVrdGEnLz4NCjxtZXRhIHByb3BlcnR5PSdvZzpkZXNjcmlwdGlvbicgY29udGVudD0nQWduZXMgQnVrdGEgb2YgSHVuZ2FyeSBkdXJpbmcgaGVyIDFzdCByb3VuZCBxdWFsaWZ5aW5nIG1hdGNoJy8+DQo8bWV0YSBwcm9wZXJ0eT0nb2c6dXJsJyBjb250ZW50PScjbGc9MSZzbGlkZT0nLz4NCjxtZXRhIHByb3BlcnR5PSdvZzpzaXRlX25hbWUnIGNvbnRlbnQ9J2h0dHA6Ly93d3cuaGFidG9vcnRlbm5pcy5jb20nLz4NCmQCAw9kFgICAQ8PFgIeCEltYWdlVXJsBVtodHRwOi8vd3d3LmhhYnRvb3JUZW5uaXMuY29tL0ltYWdlcy9nYWxsZXJ5L2ltYWdlcy9mMzA4MjQyMC04YjkyLTQzMDEtYjA5My1iZjhjZTNmOTE3NzIuanBnZGRkDzpF0HTS6mi1GJe2v3ZqBKOICQk=" />
</div>

<div class="aspNetHidden">

    <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="1F8C24FB" />
</div>
    <div>

    </div>
        <img id="ImgName" src="http://www.domain.com/Images/gallery/images/f3082420-8b92-4301-b093-bf8ce3f91772.jpg" />
    </form>
</body>
</html>

页面的完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CMS.DataAccessLayer;
using CMS.SqlHelper;
using CMS.DateConvertion;
using CMS.BusinessObjects;
using CMS.DataAccessLayer;
using System.Web.Routing;
using System.Text.RegularExpressions;

public partial class ImageShare2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       showDetails();

    }



    public void showDetails()
    {
        try
        {
            DataSet ds = new DataSet();


            var img = Request["img"];

            if (!String.IsNullOrEmpty(img))
            {
                String strSql = "SELECT * FROM PhotoAlbumImages WHERE PhotoImageLarge ='" + img + "'";
                ds = DataProvider.Connect_Select(strSql);
                DataTable dt = ds.Tables[0];
                int rCount = ds.Tables[0].Rows.Count;
                int ctr = 0;
                var Title = String.Empty;
                string  imgURL = String.Empty;
                ImgName.ImageUrl = "http://www.Domain.com/Images/gallery/images/" + img;
                //ogImage.Content = "~/Images/gallery/images/" + img;


                System.Text.StringBuilder sbMetaDetails = new System.Text.StringBuilder();
                string urlReferer;
                string pageURL=null;

                //Response.Write("<br/><br/>Request.UrlReferrer : " + Request.UrlReferrer +"<br/>");

                if (HttpContext.Current.Request.UrlReferrer != null)
                //if (HttpContext.Current.Request.UrlReferrer == null)
                {

                   // urlReferer = HttpContext.Current.Request.UrlReferrer.AbsoluteUri.ToString().ToLower();

                    //pageURL = Helper.GetQueryStringValue("purl");
                    //Response.Write("Helper.GetQueryStringValue('pageurl')<br> " + Request.QueryString["pageurl"]);
                    pageURL = Request.QueryString["pageurl"] + "#lg=1&slide=" + Request.QueryString["slide"];
                    //BUILD Redirect URL




                    foreach (DataRow dr in dt.Rows)
                    {
                        Title = dr["PhotoTitle"].ToString();

                        imgURL = "http://www.Domain.com/Images/gallery/images/" + dr["PhotoImageLarge"].ToString();

                        sbMetaDetails.AppendLine("<meta property='og:image' content='" + imgURL + "'/>");
                        sbMetaDetails.AppendLine("<meta property='og:type' content='article'/>");
                        sbMetaDetails.AppendLine("<meta property='og:title' content='" + Title + "'/>");
                        sbMetaDetails.AppendLine("<meta property='og:description' content='" + dr["PhotoDesc"].ToString() + "'/>");
                        //sbMetaDetails.AppendLine("<meta property='og:url' content='" + Request.Url.ToString() + "'/>");

                        if (!string.IsNullOrEmpty(pageURL))
                        {
                            sbMetaDetails.AppendLine("<meta property='og:url' content='" + pageURL + "'/>");                           
                            Response.Write("AA<br>");
                        }
                        else
                        {
                            sbMetaDetails.AppendLine("<meta property='og:url' content='" + Request.Url.ToString() + "'/>");
                            Response.Write("AB<br>");
                        }
                        sbMetaDetails.AppendLine("<meta property='og:site_name' content='http://www.Domain.com'/>");
                    }

                    litCtrMetaForSM.Text = sbMetaDetails.ToString();

                    //redirect
                    //Response.Redirect(pageURL);

                }
                else
                {

                    urlReferer = "";
                    foreach (DataRow dr in dt.Rows)
                    {
                        Title = dr["PhotoTitle"].ToString();


                        imgURL = "http://www.Domain.com/Images/gallery/images/" + dr["PhotoImageLarge"].ToString();
                        // ogImage.Content = imgURL;

                        sbMetaDetails.AppendLine("<meta property='og:image' content='" + imgURL + "'/>");

                        sbMetaDetails.AppendLine("<meta property='og:type' content='article'/>");
                        sbMetaDetails.AppendLine("<meta property='og:title' content='" + Title + "'/>");
                        sbMetaDetails.AppendLine("<meta property='og:description' content='" + dr["PhotoDesc"].ToString() + "'/>");
                        //sbMetaDetails.AppendLine("<meta property='og:url' content='" + Request.Url.ToString() + "'/>");
                        if (pageURL == null)
                        {
                            sbMetaDetails.AppendLine("<meta property='og:url' content='" + Request.Url.ToString() + "'/>");
                        }
                        else
                        {
                            sbMetaDetails.AppendLine("<meta property='og:url' content='" + pageURL + "'/>");
                        }
                        sbMetaDetails.AppendLine("<meta property='og:site_name' content='http://www.Domain.com'/>");
                        litCtrMetaForSM.Text = sbMetaDetails.ToString();
                        //Response.Redirect(pageURL);
                    }
                }








            }

        }
        catch (Exception ex) { Response.Write(ex.Message.ToString());}

    }




}

代码背后

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageShare.aspx.cs" Inherits="ImageShare2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <asp:Literal ID="litCtrMetaForSM" runat="server"></asp:Literal>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
        <asp:Image ID="ImgName" runat="server" />
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

将您的代码修改为以下内容:

System.Text.StringBuilder sbMetaDetails = new System.Text.StringBuilder();

string urlReferer;
string pageURL=null;

if (HttpContext.Current.Request.UrlReferrer != null)
{
    pageURL = HttpContext.Current.Request.QueryString["pageurl"] + "#lg=1&slide=" + HttpContext.Current.Request.QueryString["slide"];
    .....
    .....
    .....
}

答案 1 :(得分:0)

由于使用Request []和Request.QueryString [],因为Request []检查以QueryString(https://stackoverflow.com/a/3178558/4630403)开头的资源,因此不会出现问题。

在网站访问您的方法时检查浏览器网址并验证&#34; pageurl&#34; &#34; slide&#34;存在于其中。

如果不是,那么您的表单/ ajax调用不会发送带有请求的数据。

- 编辑 -

请将您要来的地方的代码发布到ImageShare2页面(表格/链接)。