0

我有一个名为 mydomain.com 的域。我使用了来自https://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/链接的 URL 重写。当我在 mydomain.com 中创建的虚拟目录上应用它时,它可以正常工作,但是当我直接在我的域上应用它时,即从根目录它不起作用。

void RewriteModule_BeginRequest(object sender, EventArgs e)
    {

        RewriteModuleSectionHandler cfg = (RewriteModuleSectionHandler)ConfigurationManager.GetSection("modulesSection/rewriteModule");

        // module is turned off in web.config
        if (!cfg.RewriteOn) return; 

        string path = HttpContext.Current.Request.Path;

        // there us nothing to process
        if (path.Length == 0) return; 

        // load rewriting rules from web.config
        // and loop through rules collection until first match
        XmlNode rules = cfg.XmlSection.SelectSingleNode("rewriteRules");
        foreach (XmlNode xml in rules.SelectNodes("rule"))
        {
            try
            {
                Regex re = new Regex(cfg.RewriteBase + xml.Attributes["source"].InnerText, RegexOptions.IgnoreCase);
                Match match = re.Match(path);
                if (match.Success)
                {
                    path = re.Replace(path, xml.Attributes["destination"].InnerText);
                    if (path.Length != 0)
                    {
                        // check for QueryString parameters
                        if (HttpContext.Current.Request.QueryString.Count != 0)
                        {
                            // if there are Query String papameters
                            // then append them to current path
                            string sign = (path.IndexOf('?') == -1) ? "?" : "&";
                            path = path + sign + HttpContext.Current.Request.QueryString.ToString();
                        }
                        // new path to rewrite to
                        string rew = cfg.RewriteBase + path;
                        // save original path to HttpContext for further use
                        HttpContext.Current.Items.Add(
                            "OriginalUrl",
                            HttpContext.Current.Request.RawUrl);
                        // rewrite
                        HttpContext.Current.RewritePath(rew);
                    }
                    return;
                }
                else
                {
                   // HttpContext.Current.RewritePath("~/FnF.aspx");
                }
            }
            catch (Exception ex)
            {
                throw (new Exception("Incorrect rule.", ex));
            }
        }
        return;
    }
 public class RewriteModuleSectionHandler : IConfigurationSectionHandler
{

    private XmlNode _XmlSection;
    private string _RewriteBase;
    private bool _RewriteOn;

    public XmlNode XmlSection
    {
        get { return _XmlSection; }
    }

    public string RewriteBase
    {
        get { return _RewriteBase; }
    }

    public bool RewriteOn
    {
        get { return _RewriteOn; }
    }
    public object Create(object parent, object configContext, System.Xml.XmlNode section)
    {
        // set base path for rewriting module to
        // application root
        _RewriteBase = HttpContext.Current.Request.ApplicationPath;
        if (!HttpContext.Current.Request.ApplicationPath.EndsWith("/"))
        {
            _RewriteBase = HttpContext.Current.Request.ApplicationPath + "/";
        }

        // process configuration section 
        // from web.config
        try
        {
            _XmlSection = section;
            _RewriteOn = Convert.ToBoolean(section.SelectSingleNode("rewriteOn").InnerText);
        }
        catch (Exception ex)
        {
            throw (new Exception("Error while processing RewriteModule configuration section.", ex));
        }
        return this;
    }
}
4

1 回答 1

0

使用Regex re = new Regex("^" + cfg.RewriteBase + xml.Attributes["source"].InnerText+"$", RegexOptions.IgnoreCase); 而不是Regex re = new Regex(cfg.RewriteBase + xml.Attributes["source"].InnerText, RegexOptions.IgnoreCase);现在它对我有用。

于 2013-09-21T12:52:05.780 回答