PranasLu's Blog

2010-02-23

Relative path <==> absolute path

Filed under: Programming — Tags: , , , — pranaslu @ 16:10

Web aplikacijos kode neturi būti absoliučių kelių. Aplikacija turi būt nepriklausoma nuo to ar diegiam website root kataloge ar virtualioj directorijoj. ASP.NET’e tai paprasta:

  • ASP.NET control’suose galima naudot ‘~’ simbolį. Pvz.:
    • web control’suose <asp:image runat=”server” id=”Image1″ ImageUrl=”~/Images/SampleImage.jpg” /> arba
    • html control’suose <img src=”/Images/SampleImage.jpg” runat=”server” id=”image1”/>
  • System.Web.VirtualPathUtility klase turi metodus ToAbsolute(), ToAppRelative(). Path turi būti be Query parametrų. Pvz.:
    • ToAbsolute(“~/default.aspx”) ==> “/virtualDir/default.aspx”
    • ToAppRelative(“/virtualDir/default.aspx”) ==> “~/default.aspx”
  • Page.ResolveUrl() ir Page.ResolveClientUrl(). Šie metodai skiriasi tik tiek, kad ResolveClientUrl() grąžina kelią dabartinio puslapio atžvilgiu. Pvz.:
    • Page.ClientScript.RegisterClientScriptInclude(“MyScript”, this.ResolveUrl(“~/JavaScript/Script.js”));
      Rezultatas: <script src=”/virtualDir/JavaScript/Script.js” type=”text/javascript”></script>
    • Page.ClientScript.RegisterClientScriptInclude(“MyScript”, this.ResolveClientUrl(“~/JavaScript/Script.js”));
      Rezultatas: <script src=”../JavaScript/Script.js” type=”text/javascript”></script>

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: