【问题标题】:edit Global.asax file and implement it in website编辑 Global.asax 文件并在网站上实现它
【发布时间】:2017-12-13 10:36:20
【问题描述】:

我正在运行的网站包含 Global.asax 文件和 App_global.asax.compiled 也 App_global.asax.dll 但现在我想编辑或添加一些现有的 url 重写代码。 但是当我在 global.asax 文件中编写代码时,它没有被占用。

经过大量谷歌搜索和搜索后,我知道我必须在更改完成后再次编译该全局文件。但我不能这样做,因为 websi## Heading ##te 是实时的,我没有解决方案。

Global.asax 中的现有代码:

 protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext myContext=HttpContext.Current;

        Regex rewrite_regex = new Regex(@"(.+)\/((.+)\.*)", RegexOptions.IgnoreCase);

       try
       {
              // 'see if we need to rewrite the URL
           Match match_rewrite =rewrite_regex.Match(myContext.Request.Path.ToString());

           string str = match_rewrite.Groups[2].Captures[0].ToString();
           string root = match_rewrite.Groups[0].Captures[0].ToString();
           if (Regex.Match(root, "/News/", RegexOptions.IgnoreCase).Success)
           {           
               myContext.RewritePath("~/Default2.aspx?nid="+str);
           }
           else if (Regex.Match(root, "/Search/", RegexOptions.IgnoreCase).Success)
           {
               myContext.RewritePath("~/Default.aspx?text=" + str);
           }
           else if (Regex.Match(root, "/Find/", RegexOptions.IgnoreCase).Success)
           {
               myContext.RewritePath("~/Default.aspx?text=" + str);
           }

任何帮助都会非常有用。

【问题讨论】:

    标签: asp.net .net global-asax


    【解决方案1】:

    由于您没有代码,因此更简单的方法是使用IIS redirect rules? 这会将规则添加到您的 web.config。您可以创建多个基于正则表达式的规则来满足您的不同条件

    例如

    <rule name="Redirect from blog">
          <match url="^blog/([_0-9a-z-]+)/([0-9]+)" />
          <action type="Redirect" url="article/{R:2}/{R:1}" redirectType="Found" />
        </rule>
    

    【讨论】:

      猜你喜欢
      • 2017-12-22
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多