因为要做静态页面生成到指定目录中去,但是只能取到当前页面中的html,如果代码中有链接,那么放到其他目录中就会出现问题,如图片、文件链接、页面地址链接
/abc/a.aspx  根目录地址
ftp://abc.com/a.rar 外联地址
http://abc.com/a.htm 外联地址
类似这些事不需要转的单页面中用相对地址的例如:
abc.com/cc.com  同目录中的子目录(目录命名可以带.号,虽然不常见但是是允许的)
www.a.com/?i=123 (注意www.a.com在链接中是作为目录解析的)
../a/a.aspx?i=123  上级目录的子目录,带多个连续.号的
a/a/?i=123 子目录下默认页面的参数传递,不带.号的
a.aspx?i=1 同目录下页面,不带/号的各位有什么办法没?
能用.net内置功能最好,因为最完善
没有的话也只有自己写了,诶

解决方案 »

  1.   

      public static string GetAbsolutePath(string Sourcepath,string Relativepath)
            {
                Sourcepath = Sourcepath.ToLower();
                Relativepath = Relativepath.ToLower();
                if (Relativepath.IndexOf("http://") > -1)
                {
                    return Relativepath;
                }
                else if (Relativepath.Substring(0, 1) == "/")
                {
                    string[] sd = Sourcepath.Replace("http://", "").Split('/');
                    return "http://" + sd[0] + Relativepath;
                }
                else
                {
                    string[] sd = Relativepath.Split('/');
                    if (sd[0] != "..")
                    {
                        if ((sd[0].IndexOf(".net") > -1) || (sd[0].IndexOf(".cn") > -1) || (sd[0].IndexOf(".org") > -1) || (sd[0].IndexOf(".com") > -1) || (sd[0].IndexOf(".cc") > -1))
                        {
                            return "http://" + Relativepath;
                        }
                        else
                        {                        string[] sd1 = Sourcepath.Replace("http://", "").Split('/');
                            int i = sd1.Length;
                            if (i == 1)
                            {
                                return "http://" +Sourcepath+"/"+Relativepath;
                            }
                            else
                            {
                                string sf = "http://";
                                for (int j = 0; j < i - 1; j++)
                                {
                                    sf = sf + sd1[j]+"/";
                                }
                                return sf +  Relativepath;
                            }
                        }
                    }
                    else
                    {
                        string[] sd1 = Sourcepath.Replace("http://", "").Split('/');
                        int i = sd1.Length;
                        int n = 0;
                        string[] sd2 = Relativepath.Split('/');
                        int k = sd2.Length;
                        string kd = "";
                        for (int j = 0; j < k; j++)
                        {
                            if (sd2[j] == "..")
                            {
                                n = n + 1;
                            }
                            else
                            {
                                kd = kd +"/"+ sd2[j];
                            }
                        }
                        //kd = kd.Substring(0, kd.Length - 1);
                            string sf = "http://";
                            for (int j = 0; j < i - n-1; j++)
                            {
                                sf = sf + sd1[j]+"/";
                            }
                            return sf.Substring(0,sf.Length-1) + kd;
                       
                    }            }
            }
      

  2.   

    abc.com/cc.com 同目录中的子目录(目录命名可以带.号,虽然不常见但是是允许的)
    www.a.com/?i=123 这样写会按目录解释的。<a href属性必须是http://开头才是按域名解析的如果是模板,生成的页面又不在一个文件夹下,最好生成的时候就采用绝对路径生成
      

  3.   

    以上函数是china-code.net写的,大致是http://www.china-code.net/2/1.html与/2.html自动比较生成绝对地址http://www.china-code.net/2.html
      

  4.   

    net_lover
     
    (【孟子E章】) 老兄,请教一下如何获取自定义控件中模板的值<listpage:ccpage Text="button" runat="server" >
    <ItemTemplate>
    fffffff
    </ItemTemplate>
    </listpage:ccpage>如何把上面控件中fffffff提取出来?以下为一些参考代码
     using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI;
    using System.ComponentModel;
    using System.Web;
    using System.Configuration;
    using System.Text.RegularExpressions;
    using System.Collections;
    using cc;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Collections.Specialized;
    using System.Web.UI.WebControls;
    namespace cc
    {  [ParseChildren(true)]
      public class ccpage : Control, INamingContainer
      {
      private ITemplate itemPlate;
      [TemplateContainer(typeof(ccpage))]
      [PersistenceModeAttribute(PersistenceMode.InnerProperty)]
      [TemplateInstanceAttribute(TemplateInstance.Single)]
      [BrowsableAttribute(false)]
      public ITemplate ItemTemplate
      {
      get { return itemPlate; }
      set { itemPlate = value; }
      }  private string text;
      public string Text
      {
      get { return text; }
      set { text = value; }
      }  protected override void Render(HtmlTextWriter writer)
      {
      itemPlate.InstantiateIn(this); //在这里可以输出 fffffff
      itemPlate.InstantiateIn(this); //在这里可以第二次输出 fffffff
      base.Render(writer);
      }  }
    }
    http://topic.csdn.net/u/20100907/10/67db7b85-5564-4784-a75e-6659d6a63bd7.html
      

  5.   

    你TNND抗着两颗红星,还有脸打出 “学习”这么屈辱的俩字儿来 BS你 嘿嘿
      

  6.   

    先用正则得到所有的页面源码中所有的url,
    (?<=href\s*=)(?:[ \s""']*)(?!#|mailto|location.|javascript|.*css|.*this\.)[^""']*(?:[ \s>""'])
    再替换
    或生成时
    使用VirtualPathUtility.ToAbsolute("~/");
    HttpRuntime.AppDomainAppVirtualPath;
    Request.ApplicationPath;
    Page.ResolveUrl("~");
      

  7.   

    我用的是"http://" + Request.Url.Authority
    每个连接的时候都要改动一下...
      

  8.   

    有缺陷:
    除http外所有协议不支持,如ftp
    除com,net,gov之外的所有域名都不支持,如edu,tw,us......
    当然是可以修改的 呵呵
      

  9.   


    这个是有缺陷,但是不难补充,以上函数我只用到http,所以就写成这样了,请见谅。