Results 1 to 20 of 20

Thread: get the path info when it's without default.aspx

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    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.

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: get the path info when it's without default.aspx

    I don't understand still, can you give an example?

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: get the path info when it's without default.aspx

    Quote Originally Posted by vbbit View Post
    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

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    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);
                    
                }

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: get the path info when it's without default.aspx

    Quote Originally Posted by danasegarane View Post
    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

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: get the path info when it's without default.aspx

    Quote Originally Posted by vbbit View Post
    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

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    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.

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: get the path info when it's without default.aspx

    But the username is always changing.... it can be user1, user2, user3...

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    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.

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    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?

  16. #16
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    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?

  18. #18
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: get the path info when it's without default.aspx

    Is there a url property for that then if it has extension?

  20. #20
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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
  •  



Click Here to Expand Forum to Full Width