簡體   English   中英

發布到 IIS 時出現 Response.Redirect 的問題

[英]Problem with Response.Redirect when posting to IIS

你好社區我希望你能幫助我解決這個問題,因為當我在 IIS 中發布 web 應用程序時收到一條消息。

消息是ERR_TOO_MANY_REDIRECTS

我使用 windows 身份驗證。

default.aspx 頁面繼承了我的母版頁的設計。

在母版頁上,我只創建動態菜單並編寫自定義角色和權限來限制用戶,如果您沒有權限,我不會通過在地址欄中的 URL 中鍵入它們來訪問 webForms。

在此處輸入圖像描述

上表顯示了用戶可以訪問的頁面。

if 的邏輯是這樣的:當你啟動我的應用程序時, default.aspx頁面被啟動,因為它被設置為默認值,所以它進入第一個else然后進入else if,因為在保存的頁面列表中找不到這個頁面用戶可以訪問,但它顯示為主頁。 我有一個顯示在 default.aspx 中的菜單,單擊此菜單會將我重定向到frmLogistics.aspx頁面,因此它會進入else頁面,然后進入true ,因為用戶有權訪問此頁面。 如果我將frmAdmin.aspx放在瀏覽器的地址欄中,它會進入else然后進入else ,因為在用戶可以訪問的已保存頁面中找不到該頁面,因此它會通過響應重定向我。 重定向default.aspx但它會重新啟動並再次運行並進入else,否則如果

這是我的母版頁的代碼。

     DataSet Menu = new DataSet();
     DataTable _fatherMenu = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
     if (!IsPostBack)
        {               

            Transactions t = new Transactions();
            System.Security.Principal.WindowsIdentity user = System.Security.Principal.WindowsIdentity.GetCurrent();


            string url_page = Request.Url.AbsolutePath.Substring(Request.Url.AbsolutePath.LastIndexOf("/") + 1);
            List<string> saved_pages = new List<string>();


            Menu = t.Mostrar_Menu(user.Name);
            _fatherMenu = Menu.Tables[1];
            if (_fatherMenu.Rows.Count == 0)
            {
                Response.Redirect("https://www.google.com.mx", true);
            }
            else
            {

                foreach (DataRow item in _fatherMenu.Rows)
                {
                    string page_name;
                    page_name = item[4].ToString();
                    page_name = page_name.Replace("/FolderTest/", "");
                    saved_pages.Add(page_name);
                }

                rptMenu.DataSource = t.Mostrar_MenuSubmenu(user.Name); 
                rptMenu.DataBind(); //Menu loaded in Repeater control


                bool item_found = saved_pages.Any(x => x == url_page);
                if (item_found == true)
                {
                    form1.Action = Request.RawUrl;
                }
                else if (item_found == false && url_page.EndsWith("default.aspx", StringComparison.CurrentCultureIgnoreCase))
                {
                    form1.Action = Request.RawUrl;
                }
                else
                {
                    //Here a page is not redirected runs again a second time and enters
                    //else if (item_found == false && url_page == "default.aspx")

                    Response.Redirect("~/default.aspx", true);

                }

            }


        }

    }

這是我在 default.aspx 中的代碼,我只顯示用戶數據。

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Transacciones t = new Transacciones();
            EUsuario user_identity = new EUsuario();
            System.Security.Principal.WindowsIdentity user = System.Security.Principal.WindowsIdentity.GetCurrent();

            user_identity = t.Consult_Data(user.Name);

            if (user_identity.User != "" || user_identity.UserName != "" || user_identity.Position)
            {
                lblUserWindows.Text = user_identity.User;
                lblUser_Name.Text = user_identity.UserName;
                lblPosition.Text = user_identity.Position;

            }

        }
    }

在我的本地主機中完美運行,但在服務器上發布時出現錯誤 ERR_TOO_MANY_REDIRECTS。

ERR_TOO_MANY_REDIRECTS錯誤發生在頁面重定向到自身的次數過多時。 那是瀏覽器放棄而不是一遍又一遍地做同樣的事情。

在您的代碼中,這意味着您每次頁面運行時都會點擊此行:

Response.Redirect("~/default.aspx", true);

我的猜測是該站點在服務器上的虛擬目錄下運行。 例如, http://example.com/mysite/default.aspx

在這種情況下, Request.Url.AbsolutePath將是/mysite/default.aspx ,而不僅僅是您期望的/default.aspx

所以改變

string url_page = Request.Url.AbsolutePath.Replace("/", "");

string url_page = url.AbsolutePath.Substring(url.AbsolutePath.LastIndexOf("/") + 1);

這將占用路徑的最后一部分,在最后/之后。

你也應該改變

else if (item_found == false && url_page == "default.aspx")

else if (item_found == false && url_page.EndsWith("default.aspx", StringComparison.CurrentCultureIgnoreCase))

忽略大小寫很重要,因為可以使用Default.aspxDEFAULT.ASPX或任何變體來訪問它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM