Ahmad Alaa
Ahmad Alaa

Reputation: 817

asp.net get query string value after url rewriting

I using iis url rewriting, I added this rule

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

to rewrite this url :

/pages/details.aspx?id=12

to

/details/12

so, after requesting new url : /details/12, and try to get query string using this code :

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");
    }
}

i cant get any value

and i also tried :

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");
        }
    }

but theRealURL comes without any query strings !! Debugging result:

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

so how can i get query string after rewrite url ?

Upvotes: 2

Views: 3712

Answers (2)

Vijunav Vastivch
Vijunav Vastivch

Reputation: 4191

    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();


Hope it helps..

Upvotes: 0

Andy-Delosdos
Andy-Delosdos

Reputation: 3720

If you visit /details/12 you should be able to use Request.QueryString["id"] on your details.aspx page and retrieve the value '12'. I've tested it locally and it works for me. Do you have any other rules that might be interfering?

Also make sure you've got the latest IIS rewrite installed: http://www.iis.net/downloads/microsoft/url-rewrite.

I got it working locally using the following code:

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;
        }
    }
}

Now if i visit /details/12345 the page displays:

Details Page

ID: 12345

If this doesn't work for you, then something else is interfering with your rewrite or your IIS setup is different to mine.

I don't need to do this on my dev server, but you could try appending appendQueryString="true" to your web.config rule. e.g.

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

Upvotes: 2

Related Questions