【问题标题】:asp net global asax rewrite infinite loopasx 全局 asax 重写无限循环
【发布时间】:2022-01-16 22:30:51
【问题描述】:

这是我的 Global.asax

   public void GetRewrites()
   {
      if (rewrites == null)
         rewrites = Rewrite.getRules(); //reads from the DB
   }

   protected void Application_Start(object sender, EventArgs e)
   {
      RouteTable.Routes.Clear();
      RouteTable.Routes.EnableFriendlyUrls();
      RouteTable.Routes.MapPageRoute("", "home", "~/Default.aspx");
      RouteTable.Routes.MapPageRoute("", "login", "~/Login.aspx");
      ...others
   }

   protected void Application_BeginRequest(object sender, EventArgs e)
   {
      String fullOriginalPath = Request.Url.ToString();
      //if link contains .aspx is not a rewrite
      if(fullOriginalPath.Contains(".aspx"))
      {
         return;
      }

      GetRewrites();
     
      int index = 0;
      if (fullOriginalPath.Contains("www.mysite.com"))
         index = fullOriginalPath.IndexOf('/', fullOriginalPath.IndexOf("www.mysite.com")) + 1;
      else
         index = fullOriginalPath.IndexOf('/', fullOriginalPath.IndexOf("localhost")) + 1;

      string chiave = fullOriginalPath.Substring(index).ToLower();

      //check if exists in RouteTable
      //skip first 2 element 
      for (int i = 2; i< RouteTable.Routes.Count; i++)
      {
         System.Web.Routing.Route rx = RouteTable.Routes[i] as System.Web.Routing.Route;
         if (rx.Url == chiave)
            return;
      }

      //if not exists in redirect rules it must go to the home page
      if(rewrites.Where(x=>x.Chiave == chiave).Count() == 0)
      {
         Response.Redirect("home");
         return;
      }
  }

问题来了:

如果我去 localhost:4210/home 就可以了

如果我在服务器上发布我的应用程序并尝试访问链接 www.mysite.com/home

我得到了错误 网站重定向次数过多。

有人可以对动机有一个想法吗?

【问题讨论】:

  • 请问,您能否添加有关返回的错误和浏览器控制台的更多详细信息?
  • 我认为问题在于您正在计算“索引”。对于发布代码时,您应该在末尾删除“+1”,因为它在“/”字符所在位置之后开始新字符串(它适用于 localhost,因为在“/”之前还有端口) .

标签: asp.net url-rewriting global-asax


【解决方案1】:

问题是在我的 web.config 文件中(在服务器上而不是本地) 有这些要重写的行

 <rule name="home page" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
        <add input="{HTTP_HOST}{REQUEST_URI}" pattern="www.mysite./Home" />
      </conditions>
      <action type="Redirect" url="https://www.mysite." redirectType="Permanent" />
    </rule>

评论了这条规则现在它有效 PS(该网站是 .com 并且不是空的,但不允许我写它)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    相关资源
    最近更新 更多