相关疑难解决方法(0)

Response.End()被认为是有害的吗?

这篇知识库文章说,ASP.NET Response.End()中止了一个帖子.

反射器显示它看起来像这样:

public void End()
{
    if (this._context.IsInCancellablePeriod)
    {
        InternalSecurityPermissions.ControlThread.Assert();
        Thread.CurrentThread.Abort(new HttpApplication.CancelModuleException(false));
    }
    else if (!this._flushing)
    {
        this.Flush();
        this._ended = true;
        if (this._context.ApplicationInstance != null)
        {
            this._context.ApplicationInstance.CompleteRequest();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎很苛刻.正如知识库文章所说,以下应用程序中的任何代码Response.End()都不会被执行,这违反了最不惊讶的原则.它几乎就像Application.Exit()在WinForms应用程序中.造成线程终止异常Response.End()不开捕,所以代码周围的try...... finally不会满足.

这让我想知道我是否应该总是避免Response.End().

任何人都可以建议,我什么时候应该使用Response.End(),何时Response.Close()何地HttpContext.Current.ApplicationInstance.CompleteRequest()

参考:Rick Strahl的博客文章.


根据我收到的输入,我的回答是,是的,Response.End是有害的,但在某些有限的情况下它是有用的.

  • 使用Response.End()作为一个不可捕获抛出,立即终止HttpResponse在特殊的条件.在调试过程中也很有用. 避免Response.End()完成常规反应.
  • 用于Response.Close()立即关闭与客户端的连接.根据此MSDN博客文章,此方法不适用于正常的HTTP请求处理. 你不太可能有充分的理由来调用这种方法.
  • 用于 …

.net asp.net

197
推荐指数
5
解决办法
8万
查看次数

为什么我的asp.net应用程序抛出ThreadAbortException?

自我解释的问题.

即使没有任何问题,为什么这个东西会冒泡进入我的尝试捕获?

为什么它会出现在我的日志中,数百次?

我知道这是一个新问题,但是如果这个网站要获得搜索排名并用新手绘制,我们就要问他们

asp.net multithreading

21
推荐指数
2
解决办法
1万
查看次数

"EndResponse"可以提高ASP.Net页面的性能

Response.Redirect我的员工页面中有一个.它重定向到Salary页面.

Response.Redirect ("Salary.aspx");
Run Code Online (Sandbox Code Playgroud)

它工作正常,直到我添加异常处理如下.

try
{
   Response.Redirect ("Salary.aspx");
}
catch(Exception ex)
{
//MyLog();
    throw new Exception();
}

//Remaining code in event handler
Run Code Online (Sandbox Code Playgroud)

这引起了一个新的异常,说"线程正在被中止".我开始知道可以通过将endResponse重定向设置为false 来避免这种情况.

Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();
Run Code Online (Sandbox Code Playgroud)

新异常的解释:它总是抛出异常但由框架处理.因为我添加了一个try..catch它被捕到了(我正在抛出一个新的异常)

注意:CompleteRequest确实绕过了其他HTTP过滤器和模块,但它不会绕过当前页面生命周期中的其他事件

注意:Response.Redirect将此异常抛出到当前页面的结束处理.ASP .Net本身处理此异常并调用ResetAbort继续处理.

  1. "将endResponse设置为false"是否可以提高性能,因为不会抛出异常?
  2. "将endResponse设置为false"是否会降低性能,因为页面生命周期事件未终止?

陷阱

  1. 如果将endResponse设置为false,则将执行eventhandler中的剩余代码.因此,我们需要if检查剩余的代码(检查:是否未满足重定向条件).

参考

  1. 为什么Response.Redirect导致System.Threading.ThreadAbortException?
  2. ASP.NET异常"线程被中止"导致方法退出

c# asp.net health-monitoring

10
推荐指数
1
解决办法
939
查看次数

ASP.NET应用程序中的Thread.Abort导致w3wp.exe崩溃

不要在此qustion上设置重复标志 - 它不是"为什么发生ThreadAbortException",它是关于"为什么w3wp.exe进程在ThreadAbortException之后终止".

假设我们有简单的Web应用程序,其代码示例如下:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Redirect("http://google.com");
}
Run Code Online (Sandbox Code Playgroud)

事实上这意味着什么(参见Response.End()被认为是有害的?):

protected void Page_Load(object sender, EventArgs e)
{
    ...response write some data...
    System.Threading.Thread.CurrentThread.Abort();
}
Run Code Online (Sandbox Code Playgroud)

在我的计算机(Windows 10 Pro + IIS)上,此代码导致IIS池进程终止,错误代码为0x0(重定向不执行).在其他计算机(不是Windows 10)上,此代码仅生成ThreadAborted异常,但进程继续工作(重定向执行).

有人可以检查这个样本并解释发生了什么吗?

更新 这里有一些与此问题相关的Windows事件日志.

记录#1

发生未处理的异常,并终止该过程.

应用ID:/ LM/W3SVC/1/ROOT/AS

进程ID:6700

例外:System.Threading.ThreadAbortException

消息:线程正在中止.

StackTrace:位于System.Web.Hosting.PipelineRuntime.ProcessRequestNotification的System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr rootedObjectsPointer,IntPtr nativeRequestContext,IntPtr moduleData,Int32标志)的System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext context) (IntPtr rootedObjectsPointer,IntPtr nativeRequestContext,IntPtr moduleData,Int32 flags)

记录#2

Faulting application name: w3wp.exe, version: 10.0.10240.16384, time stamp: 0x559f3dad
Faulting module name: KERNELBASE.dll, version: 10.0.10240.16384, time stamp: 0x559f3b2a
Exception code: 0xe0434352
Fault …
Run Code Online (Sandbox Code Playgroud)

c# iis threadabortexception windows-10 .net-4.6

8
推荐指数
2
解决办法
2798
查看次数

Response.Redirect异常

执行行:

Response.Redirect("Whateva.aspx", true);
Run Code Online (Sandbox Code Playgroud)

结果是:

A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code

例外是由于"真实"部分,告诉它立即结束当前请求.

这应该是怎么回事?
如果我们考虑:

  • 通常认为异常很重,很多时候提前结束请求的原因是避免处理页面的其余部分.
  • 性能监视中会出现异常,因此监视解决方案将显示错误数量的异常.

有没有其他方法来实现相同的目标?

asp.net

6
推荐指数
2
解决办法
8149
查看次数

Response.Redirect(URL,false) - 重定向后事件管理

可能重复:
Response.Redirect导致System.Threading.ThreadAbortException

ASP/C#.NET(Web表单,而不是MVC)

更新:刚发现一个相关的帖子(可能会重复这个帖子):为什么Response.Redirect导致System.Threading.ThreadAbortException?

~~~

经过一番研究后,我逐渐认识到,一般来说,在使用Response.Redirect()时,最好为第二个参数传递FALSE,以避免System.Threading.ThreadAbortException.(http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx)

我的问题是,"是否有一种推荐的方式(模式)用于管理(即跳过)重定向后触发的页面事件中的处理,当为第二个参数传递false时?"

当我在Page_Load()中检查并重定向过期的会话时,这对我来说主要是一个问题.每次重定向然后在每个事件的顶部检查该标志时,可能必须设置"_Rreirected"标志似乎非常繁琐.我过去不必担心这个问题因为我总是为第二个参数传递TRUE,不知道更好.

下面是一些代码,显示了我不想要做的事情(在处理每个事件之前检查_Redirected).也许我正在寻找的是更好的会话到期处理模式.

任何有关如何改进此处理的建议都将不胜感激.

private bool _Redirected = false;    

protected void Page_Load(object sender, EventArgs e)
{
  if (Session["key"] == null)
  {
    Response.Redirect("SessionExpired.aspx", false);
    Context.ApplicationInstance.CompleteRequest();

    _Redirected = true;
  }       
}

protected void Page_PreRender(object sender, EventArgs e)
{
  if (!_Redirected)
  {
    // do Page_PreRender() stuff...
  }
}

protected void Button1_Click(object sender, EventArgs e)
{
  if (!_Redirected)
  {
    // do Button1_Click() stuff...

    Response.Redirect("Button1Page.aspx", false);
    Context.ApplicationInstance.CompleteRequest();

    _Redirected = true;
  }
}

protected void Button2_Click(object sender, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net session response.redirect

6
推荐指数
1
解决办法
9921
查看次数

在Response.Redirect中使用endResponse

在ASP.NET页面中执行response.redirect时,我收到错误:
错误:无法获取
传入的两个变量的值(一个值从查询字符串检索,另一个从viewstate检索)

我从未见过之前的这个错误,所以我做了一些调查,发现建议使用"endResponse"的"False"值,

例如Response.Redirect("mypage.aspx",False)

这个有效.

我的问题是:在response.redirect中对"endResponse"值使用"False"有什么副作用?
即服务器的缓存有什么影响?页面是否在一段时间内保留在内存中?它会影响查看同一页面的不同用户吗?等等

谢谢!

asp.net

5
推荐指数
2
解决办法
9918
查看次数

Response.Redirect方法的endResponse参数的默认值是什么

我想知道HttpResponse.Redirect Method (String, Boolean)方法的endResponse参数的默认值

.net c# asp.net

4
推荐指数
1
解决办法
3688
查看次数

使用 Response.Redirect() 时“抛出异常:mscorlib.dll 中的‘System.Threading.ThreadAbortException’”

在 ASP.NET Web 表单中按钮的 OnClick 方法中,我调用了 Response.Redirect(),这会导致系统中止线程并显示错误消息:

Exception thrown: 'System.Threading.ThreadAbortException' in mscorlib.dll
Run Code Online (Sandbox Code Playgroud)

这里有一些与此类似的问题,使用我更改的解决方案:

Response.Redirect("~/UI/Home.aspx");
Run Code Online (Sandbox Code Playgroud)

Response.Redirect("~/UI/Home.aspx", false);
Context.ApplicationInstance.CompleteRequest();
Run Code Online (Sandbox Code Playgroud)

但是我仍然遇到同样的问题。我使用调试器运行了代码,一切都成功执行,直到我调用 Response.Redirect();。

点击函数

protected void btnLogin_Click(object sender, EventArgs e)
    {
        SiteUser s = null;
        try
        {
            string email = txtEmail.Text;
            string pwd = txtPwd.Text;
            s = DBConnection.login(email, pwd);                
        }
        catch (Exception ex)
        {
            Console.Write(ex);
            lblLoginError.Text = "Error logging in.";
        }
        if (s != null)
        {
            Session["UserSession"] = s;
            Response.Redirect("~/UI/Home.aspx", false);
            Context.ApplicationInstance.CompleteRequest();
        }
        else
        {
            lblLoginError.Text = "User not found. Please check your …
Run Code Online (Sandbox Code Playgroud)

c# asp.net exception mscorlib threadabortexception

4
推荐指数
1
解决办法
2万
查看次数

无法评估表达式

我正在使用一个类检查我的应用程序中的某些单词以防止SQL注入.

在类中,有一个for循环尝试将特定单词与黑名单中的单词匹配.如果匹配,我必须重定向到系统的错误页面.

但是,当找到匹配并且我尝试重定向时,我不断收到错误"无法评估表达式".

这是代码:

Private Sub CheckInput(ByVal parameter As String)
Try
    Dim errorPage As String = "error_page.aspx?Injection=" & parameter

    For i As Integer = 0 To blackList.Length - 1
        If (parameter.IndexOf(blackList(i), StringComparison.OrdinalIgnoreCase) >= 0) Then
            'Handle the discovery of suspicious Sql characters here 
            'generic error page on your site 
            HttpContext.Current.Response.Redirect(errorPage)
        End If
    Next

Catch ex As Exception
    Throw ex
End Try
Run Code Online (Sandbox Code Playgroud)

一旦Try块捕获到错误,它就会一直给出错误并且不会重定向到错误页面.

有任何想法吗?

.net vb.net asp.net visual-studio

2
推荐指数
1
解决办法
1万
查看次数