Add Role attribute to MVC3 methods only in Release mode?

拥有回忆 提交于 2019-12-08 07:51:50

问题


Is it possible to add an [Authorize(Roles="Admin")] (as an example) to an MVC3 Controller Method but ONLY in Release mode?

The Test environment I have access to at the moment has no AD, but Live does - so I'd like to add the attribute only in release mode.

EDIT: Problem when using #if

using...
using...
using MyWebsite.Helpers;

namespace MyWebsite.Controllers.Admin
{
#if !DEBUG
    [RedirectAuthorize(Roles = "Admin")]
#endif
    [DatabaseDependant]
    public class AdminController : Controller
    {
      ...
    }

As soon as I add the #if then I get an error on using MyWebsite.Helpers; saying this cannot be found (and my custom attribute [DatabaseDependant] is not found as a result)

If I recompile, then it works...temporarily...until I pretty much edit anything...whereupon I have to re-compile again.

Am I missing a trick here? Why is adding the #if making this happen?


回答1:


You could surround it with:

#if !DEBUG
[Authorize(Roles="Admin")]
#endif


来源:https://stackoverflow.com/questions/8879455/add-role-attribute-to-mvc3-methods-only-in-release-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!