简体   繁体   中英

Response.Redirect unreachable code

Is there any circumstance that the red can be called given the green?

无法访问的代码段

That looks like unit test coverage, which should still reach the code since the method itself hasn't returned. However, if the automated testing you're using actually participates in the HTTP context, then it won't reach that other code if the response has ended.

You can prevent the response from ending by passing false to Redirect() :

Response.Redirect(PageRedirect, false);

However , this is probably a bad idea. Consider the logical flow of what this code is doing. A redirect should end the response. You can choose not to end it if there's more server-side processing to be done, but this can often be pretty unintuitive. But if that server-side processing is another redirect , what is that even supposed to do? Redirect the user twice? Redirect once and ignore the second? Ignore the first and perform the second redirect? It's pretty unintuitive.

I'd recommend organizing the code such that the response terminates in the redirect. While it's not technically a return from the method, in this context it really should be logically treated like one.

Never.

Most code analyzers will not figure out the code after HttpResponse.Redirect(url) is not executed - so compiler/analizers will complain about code in red possibly is wrong.

In reality it always throws ThreadAbort exception to avoid running more request handling code inadvertently - HttpResponse.Redirect .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM