Tuesday, March 15, 2011

How to Convert "~/default.aspx" to "http://www.website.com/default.aspx" C#?

I know ASP.NET does this automatically, but for some reason I can't seem to find the method.

Help anyone? Just as the title says.

If I do a Response.Redirect("~/Default.aspx"), it works, but I don't want to redirect the site. I just want the full URL.

Can anyone help me out?

From stackoverflow
  • In a web control, the method is ResolveUrl("~/Default.aspx")

  • Take a look at the VirtualPathUtility class.

  • For the "/#{path}/Default.aspx" part, use:

    Page.ResolveUrl("~/Default.aspx")
    

    If you need more:

    Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port
    
  • There are at least three ways of doing this. I asked if there was any difference, but I didn't get any answer.

    1. Control.ResolveUrl
    2. Control.ResolveClientUrl
    3. VirtualPathUtility.ToAbsolute
    Aaron Hoffman : the difference between ResolveUrl and ResolveClientUrl is that ResolveClientUrl returns a path relative to the current page, ResolveUrl returns a path relative to the site root: http://www.andornot.com/about/developerblog/2007/06/resolveurl-vs-resolveclienturl.aspx
  • Here's what I use:

    Response.Redirect(Response.ApplyAppPathModifier("~/default.aspx"))
    
    Thiago Silva : is there a difference between using the Response.ApplyAppPathModifier() and Page.ResolveUrl(), in particular when dealing with MVC pages (not web forms)?
    Thiago Silva : oh, and I mean not for Redirect, but rather for getting URLs (e.g. setting src attributes, etc).
  • Here's an article that explains the difference between the various ways to resolving paths in ASP.NET -

    Different approaches for resolving URLs in ASP.NET

0 comments:

Post a Comment