【问题标题】:Unable to Redirect URL in ASP.NET WebMethod无法在 ASP.NET WebMethod 中重定向 URL
【发布时间】:2017-10-25 16:31:03
【问题描述】:

我在前端的 ASP.NET (WebForms) 应用程序中使用 PageMethods 来调用后端的 c# 函数,该函数从我的数据库中删除一行。删除部分有效,但我无法将 url 后缀从我当前的 .aspx 文件重定向到一个名为 StartPage.aspx 的新文件。本质上,我想在按下按钮后将 URL 从 UpdateNetEvent.aspx?ID=[Number] 更改为 StartPage.aspx。

我的按钮 HTML 如下:

<asp:Button ID="eventFormResolveBtn" runat="server" Text="Resolve NetEvent" OnClientClick="resolveClicked()" style="background-color:forestgreen;"/>

而对应的Javascript函数调用为:

function resolveClicked() {

            try {

                var currentRowID = document.getElementById('<%=eventFormCurrentRowID.ClientID%>').value;

                PageMethods.resolveData(currentRowID, OnSucceeded, OnFailed);

                function OnSucceeded(result) {
                    window.location = result.d;
                }

                function OnFailed(error) {
                    alert("An error occured on resolveClicked()");
                }
            }
            catch (err) {
                return err;
            }
            return false;
        }

另外,后端的WebMethod调用如下:

[WebMethod]
    public static string resolveData(string CurrentRowID)
    {
       try
        {
            SqlCommand command;
            SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
            command = new SqlCommand("deleteNetEventRow", sqlConn);

            command.Parameters.AddWithValue("@RowID", Convert.ToInt32(CurrentRowID));

            command.CommandType = System.Data.CommandType.StoredProcedure;
            sqlConn.Open();
            command.ExecuteNonQuery();
            sqlConn.Close();
            return "StartPage.aspx";
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERROR: " + ex.ToString());     // Add a log helper to display errors here
            return "Error in resolveData WebMethod";
        }
    }

SQLCommand 调用和执行工作完美,但我无法重定向到另一个 .aspx 页面。

【问题讨论】:

  • 您遇到错误了吗?发生什么了?你检查result.d了吗?
  • 当我在 chrome 中调试时 result.d 得到了正确的字符串值并进行了调用,但之后没有任何反应,它位于同一页面上

标签: c# asp.net redirect webmethod pagemethods


【解决方案1】:

例如

window.location.href = "http://example.com/new_url";

您可以返回服务器的网址,如下所示:

result.d="http://localhost/StartPage.aspx"

function OnSucceeded(result) {
         window.location.href = result.d;
}

【讨论】:

  • 当我使用此方法并将返回“StartPage.aspx”更改为返回“localhost/StartPage.aspx”时,我现在收到 404 Not Found 错误
  • 你的页面的url是什么首字母可以复制粘贴吗
  • localhost:(端口号)/StartPage.aspx
  • 好的,所以当从你的代码部分返回时,url 应该像 result.d="localhost:portnumber/StartPage.aspx"
  • 有趣的是,当我使用 chrome 调试并逐步完成该过程时,它仍然给我 404 Not Found 但是当我使用未打开 chrome 调试器的 Web 表单时,它只会停留在同一页面上
猜你喜欢
  • 1970-01-01
  • 2010-12-24
  • 2022-01-16
  • 1970-01-01
  • 2021-11-02
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多