views:

690

answers:

4

Hi,

In my project, my /PropertDetail.aspx can get 2 querystrings.

1st one for the PropertyId /PropertDetail.aspx?PropertyId=5

2nd one for the Language /PropertDetail.aspx?PropertyId=5&Language=2

EDIT: and this page can get one of them or can get both of them, so my rewriter rule needs to handle both of them

So, i have set these rules to web.config

<rewriter>
        <rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="$1" processing="stop" />
        <rewrite url="^/(.+)-(.+).aspx$" to="/PropertyDetail.aspx?PropertyId=$2" processing="stop"/>
        <!--http://localhost:1562/Harika-Gayrimenkul-5.aspx--&gt;
        <rewrite url="^/(.+)-(.+)-(.+).aspx$" to="/PropertyDetail.aspx?PropertyId=$2&#038;Language=$3" processing="stop"/>
        <!--http://localhost:1562/Great-Property-5-2.aspx--&gt;
</rewriter>

It is all OK if there is no Language querystring, but when there is a language querystring it gets the 3rd expression as the PropertyId instead of Language

How can i define these two rules for the same page ?

Thanks

+3  A: 

Combined answer:

<rewriter>
 <rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="$1" processing="stop"/>
 <rewrite url="^.+?([\d]+?)-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1&amp;Language=$2" processing="stop"/>
 <rewrite url="^.+?-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1" processing="stop"/>

</rewriter>

That's working well now for many combinations:

/This-is-a-really-long-property-title-555-12

returns PropertyId=555 and Language=12.

/This-is-another-really-long-property-title-666

returns PropertyId=666.

Iain M Norman
it didnt work out. But there is something that i forgot to mention about, the length of the string may be much more like Great-Property-For-PeopleWhen i use the rules above my querystring gets PropertyId="Great-Estate-5-2"
Barbaros Alp
Just testing it. Found out it's not working quite right.So the number of dashes before the required ids is variable?
Iain M Norman
Great-Estate-5-2Property Title=Great Estate | PropertyId=5 | LanguageId=2
Barbaros Alp
There you go try the new one working for me.And for more than one digit now as well, as I assume your IDs will get to more than 10 :)
Iain M Norman
:) thank you very much, i have tried it and it s ok when i have 2 querystrings(propertyId and the Language) but if i have just only PropertyId querystring i get an error. Saying Source can not be found 404. What might be the reason :/
Barbaros Alp
now my problem is with that url/Great-Property-10if there is just one querystring there is a problem, but if there is two querystring it works great
Barbaros Alp
@Barbaros, take what teknohippy has and add a third rewrite rule to the end: <rewrite url="^.+?-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1" processing="stop"/>
JasonMArcher
@Barbaros, for future reference in this case the 404 means that nothing was matched by any rule.
Iain M Norman
Thank you very much teknohippy for your help and JasonMArcher for the last golden shot :) adding <rewrite url="^.+?-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1" processing="stop"/> as the third rule did the trick
Barbaros Alp
@teknohippy, could you please add this as the 3rd rule, i will mark it as answer. <rewrite url="^.+?-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1" processing="stop"/>
Barbaros Alp
+2  A: 
Tomalak
That's what i am looking for but i got an error saying System.ArgumentException: "^/(.+?)-(.+?)-?(.+?+)?\.aspx$
Barbaros Alp
Still the varying number of dashes is going to cause trouble.
Iain M Norman
Regarding the argument exception: Maybe the backslash needs to be escaped (double backslashes instead of one)?
Tomalak
Now the exception s gone but can't get querystrings now. thank you for your time and effort. What might be the proplem, have u ever used urlrewriter.net component
Barbaros Alp
+1  A: 

This is the final solution that we have come up with.

<rewriter>
    <rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="$1" processing="stop"/>
    <rewrite url="^.+?([\d]+?)-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1&amp;Language=$2" processing="stop"/>
    <rewrite url="^.+?-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1" processing="stop"/>
</rewriter>
  • 1st rule is about the file types which we don't need.
  • 2nd rule is about "if page gets 2 querystring"
  • 3rd rule is about if the page get only one querystring

Thank you very much for your helps teknohippy and JasonMArcher

Barbaros Alp
Tomalak
At least if you don't mind that the "Language" parameter would always be *there* in the end, though sometimes empty.
Tomalak
Oh, and I've updated my answer accordingly. Not that it would make much of a difference at this point, but I did not want to leave it incorrect. ;-)
Tomalak
Thank you very much Tomalak, i have checked your answer, really thank you very much for your help.
Barbaros Alp
A: 

Even I have a problem in redirecting using "rewriter" Here is what I want to do .. If any user opens the website say www.tvguidemagazine it opens properly but when the user adds a word "customercare" to the site www.tvguidemagazine/customercare it returns "invalid virtual path error" instead of the customercare page.

What I have done is.. In the web.config file i have added a rule

Please help me out of this problem