2

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 ?

2 Answers 2

2

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" />
Sign up to request clarification or add additional context in comments.

2 Comments

no i havnt any other roles, and iam sure that i have latest IIS rewrite installed. i also updated my question, please take a look
Updated the answer :)
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();


Hope it helps..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.