#if #endif的C#替代属性 [英] C# alternatives to #if #endif for attributes
问题描述
在我所做的最后几个MVC应用程序中,每个Controller操作之前都有一小段代码:
In the last couple of MVC applications that I have made, each Controller action is preceded by this little snippet of code:
[HttpGet] // or [HttpPost]
#if (DEBUG == false)
[RequireHttps]
#endif
public ActionResult Index()
{
// do something
}
我这样做是为了让ISS Express可以在没有任何特殊HTTPS处理的情况下加载网站,但是我希望整个网站在加载到生产服务器后通过登录屏幕时需要HTTPS。
我想知道的是,如果有替代方法可以使用#if ... #endif条件编译只为Release版本添加属性。
我尝试过:
我搜索过但除了编写一个完整的类来包装Controller类之外,我还没有找到任何合适的替代方法。有条件的属性看起来很有希望,但我没有看到它们如何应用于这种情况。
I do this so that ISS Express can load up the website without any special HTTPS processing but I want the whole site to require HTTPS once past the login screen once it's loaded to the production server.
What I would like to know is if there is an alternative to using #if...#endif conditional compilation to add in attributes only for Release builds.
What I have tried:
I searched but I haven't been able to find any decent alternatives other than writing a whole class to wrap the Controller class. Conditional attributes look promising but I am not seeing how they could be applied to this situation.
推荐答案
我相信这些行是因为开发人员不会被打扰在本地IIS中设置一个自签名证书...
如果你正在编写一个仅支持HTTPS的站点,这很简单,也很有建议...
并使用(在应用程序启动时)在站点级别设置HTTPS:
I believe that those lines are there because the developer won't be bothered to set up a 'Self Signed Certificate' in the local IIS...
It is easy and very advised if you are writing an HTTPS-only site anyway...
And set HTTPS on site level using (on application start):
GlobalFilters.Filters.Add(new RequireHttpsAttribute());
这篇关于#if #endif的C#替代属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!