【问题标题】:Prevent MVC Action called from outside the world阻止从外部调用的 MVC Action
【发布时间】:2015-05-27 10:33:15
【问题描述】:

我有一个MVC的Action Method:

 [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]

但它不起作用

【问题讨论】:

  • 你在视图中写了什么?
  • 任何类型的 HTML...不是任何特定的东西
  • 您的所有代码显示的是您在成功回调中重定向,那么使用 ajax 到底有什么意义 - 只需进行正常提交!
  • 你确定它不起作用吗?这是带有工作示例的帖子。再次检查您的代码。 stackoverflow.com/questions/10253769/…
  • @StephenMuecke;我的代码只是一个例子..关键是我们如何存档以从外部世界访问它..

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

使用操作中的 ControllerContext.IsChildAction 属性来确定是否要重定向。

例如:

public ActionResult Index()   
{   
    if(!ControllerContext.IsChildAction)   
    {  
       //perform redirect here   
    }

    //do stuff here
    return View(viewModel);
}

【讨论】:

  • 它在我的情况下不起作用..当我们从 JS 调用时,ControllerContext.IsChildAction 是错误的
猜你喜欢
  • 1970-01-01
  • 2011-12-19
  • 2014-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多