Add Role attribute to MVC3 methods only in Release

2019-06-05 02:27发布

问题:

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