|
-
Oct 19th, 2009, 01:38 AM
#1
Thread Starter
Frenzied Member
get the path info when it's without default.aspx
Is there a way to get the string out of this kind of URL:
http://localhost/test
I need to get test out of it.
-
Oct 19th, 2009, 01:49 AM
#2
Re: get the path info when it's without default.aspx
Hey,
You can convert your url to a Uri Class:
http://msdn.microsoft.com/en-us/library/system.uri.aspx
This then provides a number of members that you can use to get what you want. For instance, in this case:
http://msdn.microsoft.com/en-us/libr...olutepath.aspx
Hope that helps!!
Gary
-
Oct 19th, 2009, 01:52 AM
#3
Thread Starter
Frenzied Member
Re: get the path info when it's without default.aspx
I don't understand still, can you give an example?
-
Oct 19th, 2009, 01:59 AM
#4
Re: get the path info when it's without default.aspx
Hey,
Did you read the documentation that I linked to?
From the first link, create a new instance of the Uri class, using your url:
VB:
Code:
Dim siteUri As New Uri("http://localhost/test")
C#
Code:
Uri siteUri = new Uri("http://localhost/test");
From the second link, use the AbsolutePath property of the Uri Class to return the information that you want:
VB:
Code:
Dim path As String = siteUri.AbsolutePath
C#:
Code:
string path = siteUri.AbsolutePath;
Hope that helps!!
Gary
-
Oct 19th, 2009, 02:10 AM
#5
Re: get the path info when it's without default.aspx
 Originally Posted by vbbit
Is there a way to get the string out of this kind of URL:
http://localhost/test
I need to get test out of it.
Or Else go for this one
Code:
Response.Write(Request.Url.AbsolutePath)
Please mark you thread resolved using the Thread Tools as shown
-
Oct 19th, 2009, 02:13 AM
#6
Thread Starter
Frenzied Member
Re: get the path info when it's without default.aspx
I need the if statement to do something. This would not work because anything after that schema "/" will always be there. I put the below code in the BeingRequestion method in global and it executes every time. I only one to rewrite the url when the "test" user is provided.
Code:
Uri uri = new Uri(Request.Url.ToString());
Response.Write(uri.AbsoluteUri.ToString());
Response.Write(uri.PathAndQuery.ToString());
if (uri.PathAndQuery.Length > 0) //this is what I need
{
string sUser = System.IO.Path.GetFileNameWithoutExtension(Request.PhysicalPath);
app.Context.RewritePath("~/default.aspx?user=" + sUser);
}
-
Oct 19th, 2009, 02:13 AM
#7
Re: get the path info when it's without default.aspx
 Originally Posted by danasegarane
Or Else go for this one
Code:
Response.Write(Request.Url.AbsolutePath)
Hey,
That also works 
I didn't want to make the assumption that the OP was talking about the url of the current request, but if they are, then this would be a perfectly valid approach.
Gary
Last edited by gep13; Oct 19th, 2009 at 02:16 AM.
-
Oct 19th, 2009, 02:16 AM
#8
Re: get the path info when it's without default.aspx
 Originally Posted by vbbit
I need the if statement to do something. This would not work because anything after that schema "/" will always be there. I put the below code in the BeingRequestion method in global and it executes every time. I only one to rewrite the url when the "test" user is provided.
Code:
Uri uri = new Uri(Request.Url.ToString());
Response.Write(uri.AbsoluteUri.ToString());
Response.Write(uri.PathAndQuery.ToString());
if (uri.PathAndQuery.Length > 0) //this is what I need
{
string sUser = System.IO.Path.GetFileNameWithoutExtension(Request.PhysicalPath);
app.Context.RewritePath("~/default.aspx?user=" + sUser);
}
Hey,
Ok, so this is not needed:
Code:
Uri uri = new Uri(Request.Url.ToString());
Since Request.Url is already an instance of a Uri Class.
Can you provide a concrete example of when you want the url rewrite to take place, and I am not sure that I follow from the example that you have provided.
Gary
-
Oct 19th, 2009, 02:21 AM
#9
Thread Starter
Frenzied Member
Re: get the path info when it's without default.aspx
I only want that when the user goes to http://localhost/username
Then rewrite it. The others like http://localhost/default.aspx, http://localhost/image/banner.jpg, etc... I don't want to rewrite. That's why I am hoping that IF statement to check the condition first.
-
Oct 19th, 2009, 02:29 AM
#10
Re: get the path info when it's without default.aspx
Hey,
So, why can't you use the approach that I suggested.
Namely, find the AbsolutePath for the current Uri, if it contains the value you are looking for, then do the redirect.
Gary
-
Oct 19th, 2009, 02:32 AM
#11
Thread Starter
Frenzied Member
Re: get the path info when it's without default.aspx
But the username is always changing.... it can be user1, user2, user3...
-
Oct 19th, 2009, 02:35 AM
#12
Re: get the path info when it's without default.aspx
Hey,
I understand that, but you should be able to get the username of the currently logged in user, and do the comparison to that.
It is difficult to suggest too much more, without knowing more about your application.
Gary
-
Oct 19th, 2009, 02:39 AM
#13
Thread Starter
Frenzied Member
Re: get the path info when it's without default.aspx
Have you ever played with myspace? It's like that. I have not logged into my myspace account, but I can go to my friend's myspace like http://myspace.com/testinguser and it pulls up his myspace account. So that's leaving me no logged in user to compare to that.
-
Oct 19th, 2009, 02:46 AM
#14
Re: get the path info when it's without default.aspx
Hey,
No, I haven't played with MySpace, but I think that I see what you are getting at.
This is really the concept of Routes, which was first seen with the release of the ASP.Net MVC Framework. It is also going to come into being in .Net 4.0, with the release of Visual Studio 2010.
You might want to take a look at the following:
http://aspnet.4guysfromrolla.com/articles/051309-1.aspx
Gary
-
Oct 19th, 2009, 02:48 AM
#15
Thread Starter
Frenzied Member
Re: get the path info when it's without default.aspx
So I guess.. I am stuck with this logic. Is there any other way to rewrite my url that can give me the same result per discussion here?
-
Oct 19th, 2009, 02:53 AM
#16
Re: get the path info when it's without default.aspx
Hey,
Short of implementing your own routing techniques, then I am not really sure what else to suggest to you.
I guess you could check to make sure that there is nothing no file requested in the Url, i.e. default.aspx or banner.jpg, and if there isn't assume a redirect it needed, but that seems a bit clunky.
Gary
-
Oct 19th, 2009, 02:56 AM
#17
Thread Starter
Frenzied Member
Re: get the path info when it's without default.aspx
That could work, but is it possible to determine from a url to see if it's a file or not I have sub directories too.. some files are in there?
-
Oct 19th, 2009, 02:57 AM
#18
Re: get the path info when it's without default.aspx
Hey,
If you are requesting a file, it will always have an extension, regardless of what directory, or sub-directory, it is in.
Gary
-
Oct 19th, 2009, 03:06 AM
#19
Thread Starter
Frenzied Member
Re: get the path info when it's without default.aspx
Is there a url property for that then if it has extension?
-
Oct 19th, 2009, 03:10 AM
#20
Re: get the path info when it's without default.aspx
Hey,
Again, did you read the documentation that I linked you to?
This would seem to do the trick:
http://msdn.microsoft.com/en-us/libr...ri.isfile.aspx
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|