在这种情况下无法做出响应。 Response.Write(例如.ToString());
我按照经典代码在 global.asax 中编写了这段代码。每次我启动网站时都会显示此错误。刷新页面后,它会转到正确的页面。我不明白为什么。
在此无法回复 语境。描述:未处理 期间发生异常 执行当前的网络请求。 请查看堆栈跟踪以了解更多信息 有关错误及其位置的信息 它起源于代码。
异常详细信息: System.Web.HttpException:响应是 在此上下文中不可用。
来源错误:
第 26 行:catch(异常前) 第 27 行:{ 第 28 行:Response.Write(ex.ToString()); 第 29 行:} 第 30 行:
<脚本runat="服务器">
void Application_Start(object sender, EventArgs e)
{
try
{
if (Roles.RoleExists("Administrators") == false)
Roles.CreateRole("Administrators");
if (Membership.FindUsersByName("ken").Count == 0)
{
Membership.CreateUser("ken", "123", "[email protected]");
Roles.AddUserToRole("ken", "Administrators");
}
if (Membership.FindUsersByName("dan").Count == 0)
Membership.CreateUser("dan", "123", "[email protected]");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
I wrote this code in the global.asax following the classical code. Each time I start the website it shows this error. After i refresh the page, it goes the correct page.I don't understand why.
Response is not available in this
context. Description: An unhandled
exception occurred during the
execution of the current web request.
Please review the stack trace for more
information about the error and where
it originated in the code.Exception Details:
System.Web.HttpException: Response is
not available in this context.Source Error:
Line 26: catch (Exception ex) Line 27: { Line 28: Response.Write(ex.ToString()); Line 29: } Line 30:
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
try
{
if (Roles.RoleExists("Administrators") == false)
Roles.CreateRole("Administrators");
if (Membership.FindUsersByName("ken").Count == 0)
{
Membership.CreateUser("ken", "123", "[email protected]");
Roles.AddUserToRole("ken", "Administrators");
}
if (Membership.FindUsersByName("dan").Count == 0)
Membership.CreateUser("dan", "123", "[email protected]");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Application_Start 仅在 IIS 启动应用程序时执行一次。问题的实际原因在于 try 块内的代码,您可以使用日志记录框架或通过在 catch 块内设置断点来诊断。
您不能在 catch 块内使用 Response.Write,因为没有与 Application_Start 关联的请求(或响应)。这样做会触发一个新的异常,从而隐藏了问题的原始原因。查看 ASP.NET 应用程序生命周期 了解更多详情。
Application_Start is executed only once when the application is started by IIS. The actual cause of the problem lies in the code inside of the try block, which you can diagnose using a logging framework or by setting a breakpoint inside the catch block.
You may not use Response.Write inside the catch block, as there is no Request (or Response) associated with Application_Start. Doing so triggers a new exception, which hides the original cause of the problem. Look at the ASP.NET Application Lifecycle for more details.
我相信您的应用程序启动代码很可能存在一些问题,导致出现异常,导致您捕获块。由于某种原因,响应对象不可用,因此您会收到未处理的异常。由于应用程序已经启动,因此不会再次访问代码路径,并且您在后续请求中不会看到错误。
我建议您将异常记录在文件中,而不是发出它以响应检查原始异常详细信息,这些详细信息将遮蔽实际问题以及响应不可用的原因。您可以使用 System.Diagnostic.Trace 和跟踪侦听器进行日志记录(或者您可以使用 Log4Net 或 MS Logging App Block 等库)
I believe that most probably you have some issue in your app start up code that results into an exception that take you to catch block. For some reason, the response object in not available and therefore you get an un-handled exception. Because application has already started, the code path is not getting visited again and you don't see the error on subsequent request.
I would suggest that you log the exception in a file instead of emitting it in response to check the original exception details that will shade light on actual issue and why response in not available. You can user System.Diagnostic.Trace and trace listeners for logging (or you may use library such as Log4Net or MS Logging App Block)