简体   繁体   中英

Response.Redirect Inside Try/Catch Block

While trying to convert strting into enum in Asp.NET webApplication.

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. 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 . Is the enum code redirecting the user?

Issue lied in trying to redirect to other page, inside try catch block.

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.

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