简体   繁体   English

Response.Redirect内部Try / Catch块

[英]Response.Redirect Inside Try/Catch Block

While trying to convert strting into enum in Asp.NET webApplication. 尝试在Asp.NET webApplication中将strstr convert strting into enum时。

Code - 代码-

enum MyEnum
{
    field1,
    field2,
    field3
}

string strField1 = "field1";
MyEnum parsedEnum = (MyEnum)Enum.Parse(typeof(MyEnum), strField1);

I encounter following error - 我遇到以下错误-

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack. 由于代码已优化或本机框架位于调用堆栈的顶部,因此无法评估表达式。

What am I missing. 我想念什么。

EDIT: Updated Code: 编辑:更新代码:

I have been using this enum to redirect user to other page, by validating CommandArgument of the button. 我一直在通过验证按钮的CommandArgument使用此枚举将用户重定向到其他页面。 I obtain this error while debugging the solution, other-wise the code works fine. 我在调试解决方案时遇到此错误,否则代码可以正常工作。

<form id="form1" runat="server">
    <div>
        <asp:Button Text="Redirect" ID="btnRedirect" OnClick="btnRedirect_Click" CommandName="field1" runat="server" />
    </div>
    </form>



    protected void btnRedirect_Click(object sender, EventArgs e)
    {            
        var btn = sender as Button;
        var cmdName = btn.CommandName; //field1
        MyEnum parsedEnum = (MyEnum)Enum.Parse(typeof(MyEnum), cmdName);

        try
        {
            switch (parsedEnum)
            {
                case MyEnum.field1:
                    Response.Redirect("WebForm1.aspx");
                    break;
                case MyEnum.field2:
                    Response.Redirect("WebForm2.aspx");
                    break;
                case MyEnum.field3:
                    Response.Redirect("WebForm3.aspx");
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var err = ex.Message;                
        }
    }

All references to that error message point to adding a second, false parameter to Request.Redirect . 所有对该错误消息的引用均指向Request.Redirect添加第二个false参数。 Is the enum code redirecting the user? enum代码是否重定向用户?

Issue lied in trying to redirect to other page, inside try catch block. 问题在于尝试重定向到其他页面,位于try catch块内。

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack. 由于代码已优化或本机框架位于调用堆栈的顶部,因此无法评估表达式。

Above exception had been caused, inside catch block, the current page thread was being aborted. 造成以上异常的原因是,在catch块内, current page thread was being aborted.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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