C#で相対パスや相対URIを取得するコードを紹介します。
相対URIや相対パスを取得する場合には Uriクラスと MakeRelativeメソッドを利用します。
以下のコードはhttp://news.ipentec.comに対する"http://news.ipentec.com/pub/data/"の相対URIを求めます。
protected void Page_Load(object sender, EventArgs e)
{
Uri uri1 = new Uri("http://news.ipentec.com");
Uri uri2 = new Uri("http://news.ipentec.com/pub/data/");
Label1.Text = uri1.MakeRelative(uri2);
}
結果は
protected void Page_Load(object sender, EventArgs e)
{
Uri uri1 = new Uri("http://news.ipentec.com/");
Uri uri2 = new Uri("http://news.ipentec.com/pub/data/");
Label1.Text = uri1.MakeRelative(uri2);
}
この場合にも、