【问题标题】:Temporary Disable Edit Action in ASP.NET MVC without Delete Its Action and View在 ASP.NET MVC 中临时禁用编辑操作而不删除其操作和视图
【发布时间】:2014-12-18 09:07:29
【问题描述】:

客户要求我禁用调查模块上的编辑功能,但这仍不清楚。暂时应将其从视图中删除,因此用户无法编辑调查。

所以,我去掉了Index视图上的编辑按钮,但我仍然可以通过地址栏访问编辑页面,例如:http://localhost:1306/Survey/Edit/1

我无法删除编辑操作和编辑视图,因为要求仍不清楚。有什么办法可以处理这种情况?

【问题讨论】:

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


    【解决方案1】:

    您可以使用NonAction 属性或使用private 方法,两种解决方案都可以:

    [NonAction]
    public ActionResult Edit()
    {
    }
    
    private ActionResult Edit()
    {
    }
    

    【讨论】:

    • 谢谢,与私人合作,但我找不到[NoAction] 属性? The type or namespace name 'NoAction' could not be found (are you missing a using directive or an assembly reference?)
    • @Willy 我的错,它叫NonAction
    • 我是否也应该将NonAction 也放入Edit POST Action 中?
    • @Willy 是的,最好把它放在两个动作上,这样就不会调用 POST 编辑动作
    【解决方案2】:

    通过使用非操作方法,您可以像这样实现这一目标

    public ActionResult Edit()
    {
         // Your Code for doing someThing
    }
    

    请使用以下代码代替上述代码。

    private ActionResult Edit()
    {
       // Your Code for doing someThing
    }
    

    如果你这样使用意味着你将无法访问 Edit 操作方法。

    【讨论】:

    • 你有什么理由使用private而不是[NonAction]
    • 避免使用属性限制访问action方法的简单方法。使用“私人”比“非行动”更好
    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2011-10-31
    • 2014-07-07
    • 2015-06-10
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    相关资源
    最近更新 更多