FileUplaod:未提供所需的防伪令牌或该令牌无效
我正在尝试在 ASP.NET MVC 2 Web 应用程序中上传文件,但出现错误。 未提供所需的防伪令牌或该令牌无效。
这是我的 aspx 代码:
<% using (Html.BeginForm("ImportFile", "Suivi", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<input type="file" id="fileUpload" name="fileUpload" />
<input type="submit" value="Import" />
<% } %>
在我的控制器中有我的方法:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ImportFile(HttpPostedFileBase fileUpload)
{
if( fileUpload == null)
{
//Process files
}
return View();
}
还有错误堆栈:
A required anti-forgery token was not supplied or was invalid.
at System.Web.Mvc.ValidateAntiForgeryTokenAttribute.OnAuthorization(AuthorizationContext filterContext)
at System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
那么我的问题在哪里?
干杯
滑雪场
I'm trying to do an upload file in my ASP.NET MVC 2 web application but I got an error.
A required anti-forgery token was not supplied or was invalid.
There is my aspx code :
<% using (Html.BeginForm("ImportFile", "Suivi", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<input type="file" id="fileUpload" name="fileUpload" />
<input type="submit" value="Import" />
<% } %>
And there is my method in my controller :
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ImportFile(HttpPostedFileBase fileUpload)
{
if( fileUpload == null)
{
//Process files
}
return View();
}
And there is the error stack :
A required anti-forgery token was not supplied or was invalid.
at System.Web.Mvc.ValidateAntiForgeryTokenAttribute.OnAuthorization(AuthorizationContext filterContext)
at System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
So where is my problem ?
Cheers
Skilpit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的控制器操作使用
[ValidateAntiForgeryToken]
属性进行修饰,这意味着它将尝试验证令牌。因此,您需要使用Html.AntiForgeryToken< 将此令牌包含在表单中/code>
助手:
Your controller action is decorated with the
[ValidateAntiForgeryToken]
attribute meaning that it will try to validate the token. So you need to include this token inside the form using theHtml.AntiForgeryToken
helper: