locked
_PageStart.cshtml RRS feed

  • Question

  • User-1874496678 posted

    Hi

    I am having problems with _PageStart.cshtml.

    If I use RunPage(), I get error on almost every page, including Login page. If I rem RunPage(); the site works normally except that I don't get email for any other errors.

    With Login page it will actually log me in but redirect me to my error.cshtml page, same with logout page.

    Most commonly the error points to Response.Redirect or Context.RedirectLocal line.

    My relevant login page code is as follows (same as in Mike's book)

     if (WebSecurity.Login(username, password, rememberMe)) {
                    var returnUrl = Request.QueryString["ReturnUrl"];
                    if (returnUrl.IsEmpty()){
                        Response.Redirect("~/Default.cshtml");
                    } else {
                        Context.RedirectLocal(Request.QueryString["ReturnUrl"]);
                    }
                }

    _PageStart.cshtml code is

        try{
               RunPage();     
        }
        catch(Exception ex){
            var message = ex.Message + "\n";
            var stack = ex.StackTrace;
     
            var logentry = DateTime.Now + "\n" + message + stack;
            logentry += "\n==================================\n";
            var location = Server.MapPath("~/App_Data/");
            var filename = "errorlog.txt";
            var path = Path.Combine(location, filename);
            File.AppendAllText(path, logentry);
            WebMail.Send(
            to:"my@email.com",
            subject: "Error on website",
            body: "The following error occurred \n" + message + stack,
            isBodyHtml: false
            );
            Response.Redirect("~/Error.cshtml");
          // 
        }
    

    this is the stack trace error

    The following error occurred
    Thread was being aborted.
       at System.Threading.Thread.AbortInternal()
       at System.Threading.Thread.Abort(Object stateInfo)
       at System.Web.HttpResponse.AbortCurrentThread()
       at System.Web.HttpResponse.End()
       at System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent)
       at System.Web.HttpResponseWrapper.Redirect(String url)
       at System.Web.WebPages.HttpContextExtensions.RedirectLocal(HttpContextBase context, String url)
       at ASP._Page_Account_Login_cshtml.Execute() in d:\virtualservers\zdidnfyo\www.mywebsite.org\wwwroot\Account\Login.cshtml:line 48
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors)
       at System.Web.WebPages.WebPage.ExecutePageHierarchy()
       at System.Web.WebPages.StartPage.RunPage()
       at ASP._Page__PageStart_cshtml.Execute() in d:\virtualservers\zdidnfyo\www.mywebsite.org\wwwroot\_PageStart.cshtml:line 3

    Line 48 is the Context.RedirectLocal line

    any help is most appreciated

    Thanks

    Hamid

    Monday, June 27, 2016 11:14 PM

Answers

  • User-821857111 posted

    When you call Response.Redirect (or RedirectLocal) the ASP.NET framework throws a ThreadAbortException. Although the code you posted features in my book, it isn't the best way to implement global error handling - for the reason you just found. I'd recommend using ELMAH instead: http://www.mikesdotnetting.com/article/266/error-handling-in-asp-net-razor-web-pages

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 28, 2016 7:19 AM
  • User-1327394822 posted

    You can add this to the page start between the current try and catch

    catch (System.Threading.ThreadAbortException){
                // do nothing
            }

    or you need to tell asp.net whether to run the code ahead 

    if(WebSecurity.IsAuthenticated){
            Response.Redirect("~/Account/", false);
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 28, 2016 1:05 PM

All replies

  • User-821857111 posted

    When you call Response.Redirect (or RedirectLocal) the ASP.NET framework throws a ThreadAbortException. Although the code you posted features in my book, it isn't the best way to implement global error handling - for the reason you just found. I'd recommend using ELMAH instead: http://www.mikesdotnetting.com/article/266/error-handling-in-asp-net-razor-web-pages

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 28, 2016 7:19 AM
  • User-1327394822 posted

    You can add this to the page start between the current try and catch

    catch (System.Threading.ThreadAbortException){
                // do nothing
            }

    or you need to tell asp.net whether to run the code ahead 

    if(WebSecurity.IsAuthenticated){
            Response.Redirect("~/Account/", false);
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 28, 2016 1:05 PM
  • User-1874496678 posted

    Thank you both

    much appreciated

    Thursday, June 30, 2016 1:03 AM