Razor 页面方法 RedirectToPage 调用操作,但不加载页面(无 MVC)
Razor Page Method RedirectToPage calls action, but does not load the page (No MVC)
我有一个包含几个 Razor Pages 的项目。第一个是登录,在提交时调用此方法。
public RedirectToPageResult OnPostLogin()
{
string salt; // grab the unique salt we stored on the database
string hashedPassword;
using (var conn = new SqlConnection(_config.GetConnectionString("UserDBContext")))
{
salt = conn.Query<string>(GetDBSalt(), new // grabs the salt from the database
{
@Email = Email
}).FirstOrDefault();
if (salt != null)
{
hashedPassword = PasswordHash.Hash(Password, Convert.FromBase64String(salt)); // hashes the password and salt to compare against the database password
}
else
{
hashedPassword = "";
}
var result = conn.Query<Guid>(CheckUserSql(), new
{
@Email = Email, // ditto below
@Password = hashedPassword // checks if the hashed password matches the password we have on the database
}).FirstOrDefault();
var resultString = result.ToString();
if (resultString != "00000000-0000-0000-0000-000000000000")
{
//Response.Redirect($"/CardPage/{resultString}");
return RedirectToPage("CardPage", "Card", new { id = resultString });
}
else
{
return RedirectToPage("LogIn");
}
}
}
然后进入cshtml页面,CardPage,调用了这个OnGet方法。
public void OnGetCard(string id)
{
using (var conn = new SqlConnection(_config.GetConnectionString("UserDBContext")))
{
var cards = conn.Query<string>(GetUserSql(), new
{
@Id = id
}).FirstOrDefault();
this.HttpContext.Session.SetString("Id", id);
CardsSelected = cards;
}
}
这工作得很好,它把 id 移到下一页,我可以在这个页面上做我需要做的事情。但是,当我尝试使用此 OnPost 方法在 CardPage 上执行完全相同的操作时。此 OnPost 是从 AJAX 方法调用的。
public RedirectToPageResult OnPost([FromBody] Cards cards)
{
var urlId = HttpContext.Session.GetString("Id");
var cardString = cards.cards;
using (var conn = new SqlConnection(_config.GetConnectionString("UserDBContext")))
{
conn.Execute(SetCardsSql(), new
{
@Cards = cardString,
@Id = urlId
});
}
return RedirectToPage("HierarchyPage", "Build", new { id = urlId });
}
还有这个OnGet方法
public void OnGetBuild(string id)
{
using (var conn = new SqlConnection(_config.GetConnectionString("UserDBContext")))
{
var cards = conn.Query<string>(GetUserSql(), new
{
@Id = id
}).FirstOrDefault();
this.HttpContext.Session.SetString("Id", id);
CardsSelected = ParseCardsSelected(cards);
}
string slotHTML;
int slotID = 1;
// create the rows of the pyramid and insert its slots
for (int currentRowNumber = 1; currentRowNumber <= MAX_ROWS; currentRowNumber++)
{
slotHTML = "";
for (int n = 1; n <= currentRowNumber; n++, slotID++)
{
// generates slot HTML and assigns an unique ID to each slot
slotHTML += string.Format(CARD_SLOT_HTML, slotID);
}
Rows += string.Format(SLOT_ROW_HTML, currentRowNumber, slotHTML);
}
Rows = new string(Rows.Where(c => !char.IsWhiteSpace(c) || c.Equals(' ')).ToArray());
}
方法被调用并执行,然后转到我设置模型的 cshtml 页面。所有这些都正确执行,但 页面未在浏览器中加载 。我一直在寻找解决方案,但我找不到任何有类似问题的东西。我有一个临时解决方法,即采用应该加载的 url 并手动加载它,它就是这样工作的。
总结一下,前两种方法有效,后两种方法无效。在第二个序列中,在 OnGet 中调用方法,但页面未加载到视图中。手动放入 url 时,页面会正确加载到视图中。登录 --> CardPage 有效,CardPage --> HierarchyPage 无效。
如果需要,我会用更多代码更新这个问题。
如果使用 Ajax ,您应该重定向到另一个页面并在成功回调函数中从客户端传递参数:
$.ajax({
...
}).done(function (data) {
window.location.replace(data.redirectUrl);
})
服务器端将 return Json 结果与参数 :
public ActionResult OnPost([FromBody]Cards cards)
{
...
return new JsonResult(new { redirectUrl = Url.Page("HierarchyPage", "Build", new { id = urlId }) });
}
我有一个包含几个 Razor Pages 的项目。第一个是登录,在提交时调用此方法。
public RedirectToPageResult OnPostLogin()
{
string salt; // grab the unique salt we stored on the database
string hashedPassword;
using (var conn = new SqlConnection(_config.GetConnectionString("UserDBContext")))
{
salt = conn.Query<string>(GetDBSalt(), new // grabs the salt from the database
{
@Email = Email
}).FirstOrDefault();
if (salt != null)
{
hashedPassword = PasswordHash.Hash(Password, Convert.FromBase64String(salt)); // hashes the password and salt to compare against the database password
}
else
{
hashedPassword = "";
}
var result = conn.Query<Guid>(CheckUserSql(), new
{
@Email = Email, // ditto below
@Password = hashedPassword // checks if the hashed password matches the password we have on the database
}).FirstOrDefault();
var resultString = result.ToString();
if (resultString != "00000000-0000-0000-0000-000000000000")
{
//Response.Redirect($"/CardPage/{resultString}");
return RedirectToPage("CardPage", "Card", new { id = resultString });
}
else
{
return RedirectToPage("LogIn");
}
}
}
然后进入cshtml页面,CardPage,调用了这个OnGet方法。
public void OnGetCard(string id)
{
using (var conn = new SqlConnection(_config.GetConnectionString("UserDBContext")))
{
var cards = conn.Query<string>(GetUserSql(), new
{
@Id = id
}).FirstOrDefault();
this.HttpContext.Session.SetString("Id", id);
CardsSelected = cards;
}
}
这工作得很好,它把 id 移到下一页,我可以在这个页面上做我需要做的事情。但是,当我尝试使用此 OnPost 方法在 CardPage 上执行完全相同的操作时。此 OnPost 是从 AJAX 方法调用的。
public RedirectToPageResult OnPost([FromBody] Cards cards)
{
var urlId = HttpContext.Session.GetString("Id");
var cardString = cards.cards;
using (var conn = new SqlConnection(_config.GetConnectionString("UserDBContext")))
{
conn.Execute(SetCardsSql(), new
{
@Cards = cardString,
@Id = urlId
});
}
return RedirectToPage("HierarchyPage", "Build", new { id = urlId });
}
还有这个OnGet方法
public void OnGetBuild(string id)
{
using (var conn = new SqlConnection(_config.GetConnectionString("UserDBContext")))
{
var cards = conn.Query<string>(GetUserSql(), new
{
@Id = id
}).FirstOrDefault();
this.HttpContext.Session.SetString("Id", id);
CardsSelected = ParseCardsSelected(cards);
}
string slotHTML;
int slotID = 1;
// create the rows of the pyramid and insert its slots
for (int currentRowNumber = 1; currentRowNumber <= MAX_ROWS; currentRowNumber++)
{
slotHTML = "";
for (int n = 1; n <= currentRowNumber; n++, slotID++)
{
// generates slot HTML and assigns an unique ID to each slot
slotHTML += string.Format(CARD_SLOT_HTML, slotID);
}
Rows += string.Format(SLOT_ROW_HTML, currentRowNumber, slotHTML);
}
Rows = new string(Rows.Where(c => !char.IsWhiteSpace(c) || c.Equals(' ')).ToArray());
}
方法被调用并执行,然后转到我设置模型的 cshtml 页面。所有这些都正确执行,但 页面未在浏览器中加载 。我一直在寻找解决方案,但我找不到任何有类似问题的东西。我有一个临时解决方法,即采用应该加载的 url 并手动加载它,它就是这样工作的。
总结一下,前两种方法有效,后两种方法无效。在第二个序列中,在 OnGet 中调用方法,但页面未加载到视图中。手动放入 url 时,页面会正确加载到视图中。登录 --> CardPage 有效,CardPage --> HierarchyPage 无效。
如果需要,我会用更多代码更新这个问题。
如果使用 Ajax ,您应该重定向到另一个页面并在成功回调函数中从客户端传递参数:
$.ajax({
...
}).done(function (data) {
window.location.replace(data.redirectUrl);
})
服务器端将 return Json 结果与参数 :
public ActionResult OnPost([FromBody]Cards cards)
{
...
return new JsonResult(new { redirectUrl = Url.Page("HierarchyPage", "Build", new { id = urlId }) });
}