我在执行操作方法方面遇到麻烦。ActionMethod发布中的断点甚至没有触发,此方法的Html返回:无法找到资源(404)。控制器的名称是of的,登录用户的角色是好的,PartialView存在。什么能成为理由?
这是一种看法:
<div id="info"></div>
@if (User.IsInRole("admin") && item.IsPublished == false)
{
<p>
@Ajax.ActionLink("Publish", "Publish", new { id = item.RecommendationID }, new AjaxOptions() { Confirm="Are you sure?", HttpMethod="POST", UpdateTargetId="info" })
</p>
}这是一种行动方法:
[HttpPost]
[Authorize(Roles = "admin")]
[ValidateAntiForgeryToken]
public ActionResult Publish(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Recommendation recommendation = db.Recommendations.Find(id);
if (recommendation == null)
{
return HttpNotFound();
}
recommendation.IsPublished = true;
db.SaveChanges();
return PartialView("RecommendationPublished");
}编辑:好的--现在我知道HttpPost属性导致了我的问题-- HttpGet工作正常。但在这种情况下,此操作将是不安全的(更新数据库字段)。如何编写和维护安全规则?
发布于 2014-05-04 14:04:50
据我所知,您不能同时使用AntiForgeryToken和Ajax.ActionLink。您需要使用一些javascript解析伪造令牌,并将其附加到post请求中。
How to include the @Html.AntiForgeryToken() when deleting an object using a Delete link
如果你不想那样的话,就移除伪造的令牌。还要确保你使用的是jquery.unobtrusive-ajax.js
https://stackoverflow.com/questions/23456854
复制相似问题