【发布时间】:2019-12-10 18:51:55
【问题描述】:
我正在做一个 .Net Core 2.2 项目并尝试自定义(或者更确切地说添加)一些身份页面。我的项目不是剃刀页面。但是,当我搭建所需的身份页面时,它们似乎是 Razor 页面。
Identity 创建一个 Areas 目录结构,如下所示:
区域/页面/帐户/管理
我添加了一个 Razor 页面,该页面在 Manage 文件夹中创建了以下文件:
ManageProfilePicture.cshtml
ManageProfilePicture.cshtml.cs
我正在使用默认的OnGet 和OnPut 方法来做其他事情(选择、上传和操作图像),因此我想调用一个单独的ajax 方法来保存图像。正如其他 SO 问题和文章所建议的那样,我尝试使用 handler 和验证令牌,但在我的 ajax 中调用链接时似乎除了 404 之外什么都没有:
我在后面代码中的方法是这样的:
public async Task<IActionResult> OnPostSaveProfilePictureAsync(string t, string l, string h, string w, string fileName)
{
var userName = User.Identity.Name;
...
}
我的 javascript 看起来像这样:
$('#saveProfilePicture').on('click', function () {
var img = $('#preview-pane .preview-container img');
$('#avatar-crop-box button').addClass('disabled');
$.ajax({
type: "POST",
url: "/Identity/Account/Manage/ManageProfilePicture?handler=SaveProfilePicture",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: {
...
}
我知道我正在尝试该代码中的 url,因为我从我的 ajax 调用的 data 部分(长度、宽度等值)中有 console.log(ged) 其他变量.另外,我可以在 404 消息中看到每次 url 的变化。
我的 ajax cshtml 按钮是:
<button id="saveProfilePicture" class="btn btn-default">SaveProfilePictureAsync</button> (inside and outside different form tags)
对于 ajax url,我试过了:
/Identity/Account/Manage/ManageProfilePicture?handler=SaveProfilePicture
/ManageProfilePicture?handler=SaveProfilePicture
我已经尝试过该网址的一百万种其他变体(示例):
将pagehandler= 替换为handler= 以及/SaveProfilePicture 以替换整个handler 查询字符串参数。
将SaveProfilePicture 与我实际调用的ActionResult 的所有变体(OnPastSaveProfilePictureAsync、SaveProfilePictureAsync 等)交换
以下代码(非 js 帖子)让我了解我的方法:
<form asp-page-handler="saveProfilePicture" method="post">
<input type="submit" id="btnSubmit" value="Save" />
</form>
并显示(在 Firefox 检查元素中)/ManageProfilePicture?handler=SaveProfilePicture 作为文件部分,/Identity/Account/Manage/ManageProfilePicture?handler=SaveProfilePicture 在引用属性中。
确切的错误总是(当我更改它时,url 会发生变化):
XML 解析错误:找不到根元素 地点:https://localhost:44366/Identity/Account/Manage/ManageProfilePicture?handler=saveProfilePicture 第 1 行第 1 列:
在这一点上,我觉得我只是错过了一些完全基本的东西并且没有想法。有谁知道我做错了什么?
更新:根据 cmets,不同的浏览器显示的错误略有不同:
铬:
加载资源失败:服务器响应状态为400()
歌剧:
发布 https://localhost:44366/Identity/Account/Manage/ManageProfilePicture?handler=SaveProfilePicture400
边缘:
HTTP400: BAD REQUEST - 请求无法由 服务器由于语法无效。 (XHR)邮政- https://localhost:44366/Identity/Account/Manage/ManageProfilePicture?handler=SaveProfilePicture
开始看起来好像从来都不是 404 并且可能没有正确处理验证令牌?
【问题讨论】:
-
stackoverflow.com/questions/46487220/… 看看这个.. 你在其他浏览器中尝试过吗?
-
@MohanRamalingam 有趣...我做到了。我也试过 Chrome 和 Edge。我认为错误是相同的,但它们并没有......略有不同,至少给了我一些继续......我会发布不同的错误......
标签: c# .net-core asp.net-identity razor-pages