Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rewriting URL not working when using three segments

This code works perfectly to url rewrite 2 segments of the URL.

For example

/nottinghamshire/newark

However, if I add string t = Request.QueryString["t"].Replace("-", " ").ToLower().Replace(".aspx", "") to the mix, meaning

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

/nottinghamshire/newark/plumbers

It fails, and 404s like so:
enter image description here

yet this works?

?r=nottinghamshire&c=newark&t=plumbers

This is my code

Web config:

  <rule name="rewritereview">
    <match url="^([^/]+)/([^/]+)?$" />
    <conditions>
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      <add input="{REQUEST_URI}" pattern="\.js|\.css|\.img|\.scimg" negate="true" />
    </conditions>
    <action type="Rewrite" url="/city.aspx?r={R:1}&amp;c={R:2}&amp;t={R:3}" appendQueryString="false" />
  </rule>

Code behind on city.aspx.cs

string r = Request.QueryString["r"].Replace("-", " ").ToLower();
string c = Request.QueryString["c"].Replace("-", " ").ToLower().Replace(".aspx","");
string t = Request.QueryString["t"].Replace("-", " ").ToLower().Replace(".aspx", "");

if (r != null && c != null && t != null)
{
  // populate page
}
else // 404?
{

}

What am I doing wrong?

>Solution :

Try Updating the match URL. adding an extra "/([^/]+)"

<match url="^([^/]+)/([^/]+)/([^/]+)?$" />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading