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.
Printable View
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.
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
I don't understand still, can you give an example?
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:
C#Code:Dim siteUri As New Uri("http://localhost/test")
From the second link, use the AbsolutePath property of the Uri Class to return the information that you want:Code:Uri siteUri = new Uri("http://localhost/test");
VB:
C#:Code:Dim path As String = siteUri.AbsolutePath
Hope that helps!!Code:string path = siteUri.AbsolutePath;
Gary
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:
Since Request.Url is already an instance of a Uri Class.Code:Uri uri = new Uri(Request.Url.ToString());
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
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.
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
But the username is always changing.... it can be user1, user2, user3...
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
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.
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
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?
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
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?
Hey,
If you are requesting a file, it will always have an extension, regardless of what directory, or sub-directory, it is in.
Gary
Is there a url property for that then if it has extension?
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