C# 防止从外部调用MVC操作
我有一个MVC的动作方法:C# 防止从外部调用MVC操作,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我有一个MVC的动作方法: [HttpPost] public ActionResult callAction(int Id, string ffs, string sid) { //Business Logic return View(); } 这个动作是从JS调用的 var _url = vpath + '/contraller/callAction' + sidpath + '?Id=' + id + '&ffs=' + bfSelected +
[HttpPost]
public ActionResult callAction(int Id, string ffs, string sid)
{
//Business Logic
return View();
}
这个动作是从JS调用的
var _url = vpath + '/contraller/callAction' + sidpath + '?Id=' + id + '&ffs=' + bfSelected + '&s=' + fps;
clickCount = 1;
$.post(_url, function (data) {
if (data.Completed) {
location.href = data.ReturnUrl;
}
});
现在我想阻止这种行动被外界所呼吁。应仅从同一应用程序调用此操作。
我使用了[ChildActionOnly]
但它不起作用使用操作中的ControllerContext.IsChildAction属性来确定是否要重定向 例如:
public ActionResult Index()
{
if(!ControllerContext.IsChildAction)
{
//perform redirect here
}
//do stuff here
return View(viewModel);
}
您在视图中写了什么?任何类型的HTML…没有任何特定的东西。您的代码显示的所有内容都是在成功回调中重定向,那么使用ajax到底有什么意义呢?只需执行正常的提交!你确定这不管用吗?这是一篇带有工作示例的文章。请再次检查您的代码@斯蒂芬穆克;我的代码只是一个例子。关键是我们如何存档以从外部世界访问它。在我的情况下它不起作用。当我们从JS调用时,ControllerContext.IsChildAction是错误的