我正在尝试使用
asp.net core
和 TinyMCE
构建一个中等克隆。 Tiny 有一个名为 image_upload_ur
l 的 init 属性。该属性具有 '/WriteArticle'
值,并且该页面具有可以应答 post 请求的 OnPost 方法。但当我向编辑器添加图像时,我会采取HTTP 400 bad requests
。我该怎么办?
tiny initialize options:
automatic_uploads: true,
images_upload_url:'/WriteArticle',
images_upload_path:'/uploads',
WriteArticle.cshtml.cs方法:
public IActionResult OnPost()
{
Console.WriteLine("OnPost");
try
{
var file = Request.Form.Files[0];
var uploads = Path.Combine(_environment.WebRootPath, "uploads");
var filePath = Path.Combine(uploads, file.FileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(fileStream);
}
return new JsonResult(new { location = "/uploads/" + file.FileName });
}
catch (Exception ex)
{
return BadRequest(new { message = ex.Message });
}
}
400(错误请求)响应,因为框架期望
RequestVerificationToken
作为已发布请求的一部分。框架使用此来防止可能的 CSRF 攻击。如果您的请求没有此信息,框架将返回 400 bad request。
或者您可以通过将 IgnoreAntiforgeryTokenAttribute 添加到相关的 PageModel 类来绕过检查:
[IgnoreAntiforgeryToken(Order = 1001)]
public class IndexModel : PageModel
{
public void OnPost()
{
}
}
您可以查看请求验证了解更多信息。