出于某种原因,我的一个 C# 页面在提交表单后不会重定向到另一个页面。 Response.Redirect() 适用于除此页面之外的所有其他页面。我不明白为什么。我完成了整个调试过程,它加载了其他页面的所有信息,但只是不传输。这是我的页面结构的设置。我尝试删除缓存并跟踪 fiddler 上的流量,一切似乎都很正常。有人有什么想法吗?
https://pastebin.com/B57X0Phu - 该页面的整个 c#。
https://pastebin.com/cJbYYGP2 - 整个 asp.net 页面
https://pastebin.com/ZUdHMwg4 - 将数据发送到 C# 页面的 Javascript 文件。
protected void AddMealButton_Click(MealFoodViewModel addmealFormHttpRequest)
{
int mealID = 0;
string addmealFormHttpRequestString = JsonConvert.SerializeObject(addmealFormHttpRequest);
using (HttpClient client = CreateClient())
{
HttpResponseMessage response = client.PostAsJsonAsync("api/Meals", addmealFormHttpRequestString).Result;
if (response.IsSuccessStatusCode)
{
string foodstring = response.Content.ReadAsStringAsync().Result;
Meal mealView = JsonConvert.DeserializeObject<Meal>(foodstring); ;
mealID = mealView.MealID;
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
}
}
using (HttpClient client = CreateClient())
{
HttpResponseMessage response = client.PostAsJsonAsync("api/MealFoods/" + mealID, addmealFormHttpRequestString).Result;
if (response.IsSuccessStatusCode)
{
if (Response.IsClientConnected)
{
Response.Redirect("~/mealplanner.aspx", false); //Where it redirects. It gives me no errors
}
else
{
Response.End();
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ErrorHandler();", true);
}
}
}
最佳答案
这还不清楚,有一个报告的问题指出 response.redirect 只是在某些情况下不起作用。
很多人报告了它的错误,甚至在 asp.net 官方论坛上也是如此,一些人说重启 VS 并试一试可能会奏效。
有人说他们尝试了 Server.Transfer()
我不喜欢,因为它不是正确的解决方案,我不想做 A Server.Transfer()
但好吧......
这里的要点是关于这个有很多错误,最简单和最快的解决方案是使用客户端:
window.location = 'location'
或者:
// similar behavior as an HTTP redirect
window.location.replace("http://sidanmor.com");
或者:
// similar behavior as clicking on a link
window.location.href = "http://sidanmor.com";
关于c# - Response.Redirect 仅适用于一页,其余工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48860776/