在url重写后,asp.net获取查询字符串值

时间:2016-07-17 10:51:49

标签: c# asp.net iis url-rewriting

我使用iis url重写, 我添加了这条规则

<rule name="rewrite to details pages" stopProcessing="true">
    <match url="^details/([0-9]+)" />
    <action type="Rewrite" url="pages/details.aspx?id={R:1}" />
</rule>

重写此网址:

  

/pages/details.aspx?id=12

  

/信息/ 12

所以,请求新url:/ details / 12后,尝试使用此代码获取查询字符串:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        long itemid;
        if (long.TryParse(Request.QueryString["id"], out itemid))
        {
           // value is null
        }
        else
        {
            Response.Redirect("default.aspx");
        }
    }
    catch
    {
        Response.Redirect("default.aspx");
    }
}

我无法获得任何价值

我也尝试过:

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //var rawURl = Request.RawUrl;
            //Uri currentUrl = new Uri(Request.Url.GetLeftPart(UriPartial.Authority) + Request.ServerVariables["SCRIPT_NAME"] + "?" + Request.ServerVariables["QUERY_STRING"]);

            Uri theRealURL = new Uri(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.RawUrl);
            string yourValue = HttpUtility.ParseQueryString(theRealURL.Query).Get("id"); 

            long itemid;
            if (yourValue , out itemid))
            {
               // no query strings
            }
            else
            {
                Response.Redirect("default.aspx");
            }
        }
        catch
        {
            Response.Redirect("default.aspx");
        }
    }

但是theRealURL没有任何查询字符串! 调试结果:

  

{http://localhost:46476/pages/details.aspx}

那么如何在重写url后获取查询字符串?

2 个答案:

答案 0 :(得分:2)

如果您访问/ details / 12,您应该可以在details.aspx页面上使用Request.QueryString [“id”]并检索值“12”。我在本地进行了测试,它对我有用。你有其他可能干扰的规则吗?

还要确保安装了最新的IIS重写:http://www.iis.net/downloads/microsoft/url-rewrite

我使用以下代码在本地工作:

web.config:

...
<system.webServer>
<rewrite>
  <rules>
    <rule name="rewrite to details pages" stopProcessing="true">
      <match url="^details/([0-9]+)" />
      <action type="Rewrite" url="details.aspx?id={R:1}" />
    </rule>
  </rules>
</rewrite>
</system.webServer>
...

<强> details.aspx:

<h1>Details Page</h1>
<strong>ID:</strong> <asp:Literal ID="litId" runat="server" />

<强> details.aspx.cs

public partial class details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {
        try {
            long itemid = -1;
            string req = Request.QueryString["id"];
            if (long.TryParse(req, out itemid)) {
                litId.Text = itemid.ToString();
            }
            else {
                litId.Text = "Couldn't parse!";
            }
        }
        catch (Exception ex){
            litId.Text = "An error occured:" + ex.Message;
        }
    }
}

现在,如果我访问 / details / 12345 ,页面会显示:

详细信息页面

ID: 12345

如果这对您不起作用,则其他内容会干扰您的重写,或者您的IIS设置与我的不同。

我不需要在我的开发服务器上执行此操作,但您可以尝试在您的web.config规则中附加 appendQueryString =“true”。 e.g。

<action type="Rewrite" url="details.aspx?id={R:1}" appendQueryString="true" />

答案 1 :(得分:0)

    How about if you dont use urlstring but use cookies instead for passing    variable to other page. try to look at this.
Set Cookies:

            HttpCookie thisCookie = new HttpCookie("id");
            thisCookie["id"] = "12";
            this.Response.Cookies.Add(thisCookie);



On another page:


HttpCookie GetCookies =    page.Request.Cookies["id"];

   string getcookies =  GetCookies ["id"].toString();


希望有所帮助......