Showing posts with label url. Show all posts
Showing posts with label url. Show all posts

Monday, March 26, 2012

Strings and wildcard

I am doing some things with page url's, and of couse, I test on the local host.
Only thing is, when I post to the server, the name of the host chages.

I can do an if x or y then.... but as usual, want to save code space and incase the name of the server changes, need to do something else.

What I have been searching for, unsuccessfully, is a way to insert the wildcard * into the string... such as...

If myurl = "http://" & * & "pagename.aspx" Then

Well, the * is not declared and can't seem to find how to tell the string doesn't matter the host name.

Suggestions?

Thanks all,

ZathI don't recommend approaching this problem with a wildcard. There is a simpler solution.
Try something along these lines:

 Dim requestedUrl As String = System.Web.HttpContext.Current.Request.Path.ToLower()
Select Case requestedUrl
Case "default.aspx"
' settings for homepage
Case "about.aspx"
' settings for about page
Case "contact.aspx"
' settings for contact page
Case Else
' settings for page not found
End Select
The long first line will give us the requested URL, but without the host and application name.
So this ignores the host and application name, in the same way your wildcard would have done.

I hope this helps.
Thanks!

That works even better!

After posting the message, I was actually looking for a way to get the host name to input it that way and solve the problem.

Zath
The following is a test page that I use quite often.
It allows me to see various Request and HTTP values.

In situations such as the above, you can look at the results of this test page,
and see whether there are any existing values that will match your needs.

For example, this test page will show you which Request property to use if
your URL testing also takes into account QueryStrings.

Anyway, I thought that the following may help you.

<%@. Page Language="C#" Debug="True" Trace="True" %><html>
<head>
</head>
<body>
<h2>Request Report</h2>
<hr />
<p><asp:Label id="Message" runat="server" /></p>
</body>
</html>
<script runat="server">
public void Page_Load (object sender, EventArgs e)
{
Message.Text += "Page.Request.ApplicationPath: " + Page.Request.ApplicationPath + "<br " + "/>";
Message.Text += "Page.Request.FilePath: " + Page.Request.FilePath + "<br " + "/>";
Message.Text += "Page.Request.Path: " + Page.Request.Path + "<br " + "/>";
Message.Text += "Page.Request.PathInfo: " + Page.Request.PathInfo + "<br " + "/>";
Message.Text += "Page.Request.PhysicalApplicationPath: " + Page.Request.PhysicalApplicationPath + "<br " + "/>";
Message.Text += "Page.Request.PhysicalPath: " + Page.Request.PhysicalPath + "<br " + "/>";
Message.Text += "Page.Request.RawUrl: " + Page.Request.RawUrl + "<br " + "/>";
Message.Text += "Page.Request.UserAgent: " + Page.Request.UserAgent + "<br " + "/>";
Message.Text += "Page.Request.UserHostAddress: " + Page.Request.UserHostAddress + "<br " + "/>";
Message.Text += "Page.Request.UserHostName: " + Page.Request.UserHostName + "<br " + "/>";
Message.Text += "Page.Request.Url.AbsolutePath: " + Page.Request.Url.AbsolutePath + "<br " + "/>";
Message.Text += "Page.Request.Url.Host: " + Page.Request.Url.Host + "<br " + "/>";
Message.Text += "Page.Request.Url.LocalPath: " + Page.Request.Url.LocalPath + "<br " + "/>";
Message.Text += "Page.Request.Url.PathAndQuery: " + Page.Request.Url.PathAndQuery + "<br " + "/>";
Message.Text += "<br " + "/>";

Message.Text += "<hr />Request.UrlReferrer<br>";
if (Request.UrlReferrer != null)
{
Uri referrer = Request.UrlReferrer;
Message.Text += "Request.UrlReferrer.AbsolutePath: " + referrer.AbsolutePath + "<br>";
Message.Text += "Request.UrlReferrer.AbsoluteUri: " + referrer.AbsoluteUri + "<br>";
Message.Text += "Request.UrlReferrer.Authority: " + referrer.Authority + "<br>";
Message.Text += "Request.UrlReferrer.Fragment: " + referrer.Fragment + "<br>";
Message.Text += "Request.UrlReferrer.Host: " + referrer.Host + "<br>";
Message.Text += "Request.UrlReferrer.LocalPath: " + referrer.LocalPath + "<br>";
Message.Text += "Request.UrlReferrer.PathAndQuery: " + referrer.PathAndQuery + "<br>";
Message.Text += "Request.UrlReferrer.Query: " + referrer.Query + "<br>";
Message.Text += "Request.UrlReferrer.Scheme: " + referrer.Scheme + "<br>";
Message.Text += "Request.UrlReferrer.UserInfo: " + referrer.UserInfo + "<br>";
Message.Text += "<br " + "/>";
}

Message.Text += "<hr />Request.Headers" + "<br " + "/>";
OutputCollection(Request.Headers);

Message.Text += "<hr />Request.Browser" + "<br " + "/>";
HttpBrowserCapabilities bc = Request.Browser;
Message.Text += "Type = " + bc.Type + "<br>";
Message.Text += "Name = " + bc.Browser + "<br>";
Message.Text += "Version = " + bc.Version + "<br>";
Message.Text += "Major Version = " + bc.MajorVersion + "<br>";
Message.Text += "Minor Version = " + bc.MinorVersion + "<br>";
Message.Text += "Platform = " + bc.Platform + "<br>";
Message.Text += "Is Beta = " + bc.Beta + "<br>";
Message.Text += "Is Crawler = " + bc.Crawler + "<br>";
Message.Text += "Is AOL = " + bc.AOL + "<br>";
Message.Text += "Is Win16 = " + bc.Win16 + "<br>";
Message.Text += "Is Win32 = " + bc.Win32 + "<br>";
Message.Text += "Supports Frames = " + bc.Frames + "<br>";
Message.Text += "Supports Tables = " + bc.Tables + "<br>";
Message.Text += "Supports Cookies = " + bc.Cookies + "<br>";
Message.Text += "Supports VB Script = " + bc.VBScript + "<br>";
Message.Text += "Supports JavaScript = " + bc.JavaScript + "<br>";
Message.Text += "Supports Java Applets = " + bc.JavaApplets + "<br>";
Message.Text += "Supports ActiveX Controls = " + bc.ActiveXControls + "<br>";
Message.Text += "CDF = " + bc.CDF + "<br>";

Message.Text += "<hr />Request.ServerVariables" + "<br " + "/>";
OutputCollection(Request.ServerVariables);

}

private void OutputCollection(NameValueCollection coll)
{
int loop1, loop2;
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1<arr1.Length; loop1++)
{
Message.Text += "Key: " + arr1[loop1] + "<br " + "/>";
// Get all values under this key.
String[] arr2=coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2<arr2.Length; loop2++)
{
Message.Text += "Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br " + "/>" + "<br " + "/>";
}
}
}
</script>


Don't you think usingTrace would be simpler for this?
> Don't you think using Trace would be simpler for this?

The test page does have tracing turned on, so trace information is included on the page.

But Trace alone will not show the value of the Request's properties, or the Request.UrlReferrer's properties.

I know that much of my output is redundant for a page with Trace output. But the idea is tosee how the values in the HTTP headers and the form collection translate into the various properties available in the current Request object.

A simple page like this excuses me from having to remember which of the Request properties include the querystring, and which do not. Even though it was a test page I made when I was new to ASP.NET, I still find it helpful. I simply thought it may help the original poster, too.
Thanks SomeNewKid, that test page does help a LOT!

Yes, it is because I am doing a new user email confirm activate new account page.
The URL will have a querry sting in it for confirmation.

Zath

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