我的控制器内有两个动作(shoppingCartController)
public ActionResult Index()
{
//some stuff here
return View(viewModel);
}
public ActionResult AddToCart(int id)
{
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
无论如何,通过在浏览器中键入url来阻止用户直接调用索引操作?
例如:如果用户浏览shoppingCart/index要重定向到Home/Index.
Eri*_*ing 15
您可以[ChildActionOnly]在操作方法上使用该属性以确保不直接调用它,或使用操作中的ControllerContext.IsChildAction属性来确定是否要重定向.
例如:
public ActionResult Index()
{
if(!ControllerContext.IsChildAction)
{
//perform redirect here
}
//some stuff here
return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)
如果你不能让Index动作成为子动作,你总是可以检查引用者,理解它不是万无一失的并且可以被欺骗.看到:
| 归档时间: |
|
| 查看次数: |
36276 次 |
| 最近记录: |