在 Global.Asax - Application_Start 中查找 ASP.NET 应用程序是否在本地运行
HttpContext.Current.Request.IsLocal 在 Global.Asax/Application_Start 中不可用(Request 在上下文中不可用)。
我还能如何安全地确定我的 ASP.NET MVC 应用程序是否在本地启动?
这是有条件地重写我的 web.config(取决于应用程序是部署(远程)还是测试(本地))。
谢谢你!
HttpContext.Current.Request.IsLocal is not available in Global.Asax/Application_Start (Request is not available in the context).
How else could I safely determine if my ASP.NET MVC application is started locally or not?
This is to rewrite my web.config conditionally (depending on whether the application is deployed (remote) or in testing (local)).
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 IIS/cassini/任何加载您的应用程序时(在发出任何 HTTP 请求之前),将触发 Application_Start 事件。
阅读您的评论,您希望这是一次“一次性操作”,这确实没有意义。您的应用程序并不是“本地启动”,而是在其整个生命周期中可能会在本地和/或远程多次请求。考虑到这一点,您需要检查大卫评论的每个请求。
也许,如果你多解释一下你想要实现的目标会更好吗?
The Application_Start event will be fired when IIS/cassini/whatever loads up your app (way before any HTTP requests have been made).
Reading your comments you want this to be a "one time operation" which really makes no sense. Your application is not so much "started locally" but it may be requested locally and/or remotely several times throughout its life cycle. With this in mind you need to check on each request as David commented.
Maybe, it would be better if you explained a little more what you are trying to achieve?
在
BeginRequest
方法而不是Application_Start
中检查这一点可能更合适,因为第一个请求可能是本地的,但稍后您可以在其他域上调用应用程序,并且它将不再是本地的。It might be more appropriate to check this in the
BeginRequest
method instead of theApplication_Start
because the first request might be local but later you could call the application on some other domain and it will no longer be local.