Tuesday, March 13, 2012

Stuck on a Regular Expression...!?

I have the following rule in my web.config that is used in a url redirect

<RewriterRule>

___<LookFor>~/Category/(\w+)\.aspx></LookFor>

___<SendTo><![CDATA[~/Page.aspx?m=13&i=$1]]></SendTo>

</RewriterRule>

This works fine. I'm trying to do a slightly more complex one but I'm having problems with the correct expression and was hoping someone could help.

I'm trying to set it up so it when you browse to /Category/Product/892_23.aspx

It goes to Page.aspx?m=13&c=892&i=23 (eg category 892 and item 23)

The categoryID and productID can be any number of digits, that is why I have put in the '_' so I can see where one ends and the other starts.

What I'm not sure about is how to create the expression that will split them up��

Any help would be great. Thanks

I'm writing this off the top of my head, so I may get it wrong. However, try the following:
<RewriterRule>
<LookFor>~/Category/Products/(\d+)_(\d+)\.aspx></LookFor>
<SendTo><![CDATA[~/Page.aspx?m=13&c=$1&i=$2]]></SendTo>
</RewriterRule>

Thanks for that, that worked first time...

I'm sorry to hyjack my own thread but I have one last question.

I have the following site structure:

SiteRoot/Page.aspx

Page.aspx loads other content pages (ascx) into it based on parameters passed in the querystring.

Eg. Page.aspx?m=1&i=1 would display the "News" module (moduleID=1) and news item 1 (newsID=1)

This works fine. I want to implement URLrewriting. I have followed this articlehttp://msdn.microsoft.com/library/?url=/library/en-us/dnaspp/html/urlrewriting.asp and have hit a problem.
I have set up in my web.config file rules:

<RewriterRule>
___<LookFor>~/News/(\w)\.aspx</LookFor>
___<SendTo>~/Page.aspx?m=1&i=$1</SendTo>
</RewriterRule>

This works fine so when I type MySite/News/1.aspx into the browser it rewrites to MySite/Page.aspx?m=1&i=1 and shows the page.

The problem is the images on the News Item page. They have <img src='images/image1.jpg'> this shows fine on MySite/Page.aspx?m=1&i=1 (Page.aspx is on the root). However when I go to MySite/News/1.aspx, the source for the image is broken. It is now looking for the images folder in the News folder which does not exist.

Do I have to put <img src="http://pics.10026.com/?src=~/images/image.jpg" runat='server'/> on every image reference, or is there another way around this.

Thanks again


ricc wrote:

The problem is the images on the News Item page. They have <img src='images/image1.jpg'> this shows fine on MySite/Page.aspx?m=1&i=1 (Page.aspx is on the root). However when I go to MySite/News/1.aspx, the source for the image is broken. It is now looking for the images folder in the News folder which does not exist.

Do I have to put <img src="http://pics.10026.com/?src=~/images/image.jpg" runat='server'/> on every image reference, or is there another way around this.

Yes, I approved your new thread wherein you asked the same question.
I didn't provide an answer, because frankly I don't know how other developers deal with this problem. To me, adding a tilde to the start of all links would be a royal pain. If you would like to see how I deal with this problem, please see my reply in the following thread:
UserControls and relative links

0 comments:

Post a Comment