当前位置: 首页>>代码示例>>C#>>正文


C# HttpContext.ClearError方法代码示例

本文整理汇总了C#中System.Web.HttpContext.ClearError方法的典型用法代码示例。如果您正苦于以下问题:C# HttpContext.ClearError方法的具体用法?C# HttpContext.ClearError怎么用?C# HttpContext.ClearError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Web.HttpContext的用法示例。


在下文中一共展示了HttpContext.ClearError方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TryExecuteAppErrorController

        /// <summary>
        /// Executes the GStore controller with an error view. controller must be set
        /// </summary>
        /// <param name="errorPage"></param>
        /// <param name="httpStatusCode"></param>
        /// <param name="ex"></param>
        /// <param name="clearError"></param>
        /// <param name="context"></param>
        /// <param name="controller"></param>
        /// <returns></returns>
        public static bool TryExecuteAppErrorController(ErrorPage errorPage, int httpStatusCode, Exception ex, bool clearError, HttpContext context, BaseController controller)
        {
            string url = string.Empty;
            string rawUrl = string.Empty;
            if (context != null)
            {
                url = context.Request.Url.ToString();
                rawUrl = context.Request.RawUrl;
            }

            if (controller == null)
            {
                System.Diagnostics.Trace.WriteLine("--Failure. Controller is null; no error controller is available ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                return false;
            }

            var routeData = new RouteData();

            //if we can find the current area from the url, we can use a area-specific error handler to incorporate area layout/design
            string area = context.Request.DetermineAreaFromRequest();
            if (!string.IsNullOrEmpty(area))
            {
                routeData.DataTokens.Add("area", area);
            }
            routeData.Values["controller"] = "GStore";
            routeData.Values["action"] = "AppError";
            routeData.Values["exception"] = ex;
            routeData.Values["errorpage"] = errorPage;
            routeData.Values["httpStatusCode"] = httpStatusCode;

            System.Web.Mvc.IController errorController = null;
            if (controller != null)
            {
                try
                {
                    errorController = controller.GStoreErrorControllerForArea;
                }
                catch (Exception exAreaError)
                {
                    System.Diagnostics.Trace.WriteLine("--Failure. Error getting GStoreErrorControllerForArea for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Exception: " + exAreaError.Message + " Original Exception: " + ex.Message);
                    return false;
                }
            }

            var rc = new RequestContext(new HttpContextWrapper(context), routeData);
            try
            {
                System.Diagnostics.Trace.WriteLine("--Executing error controller for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                errorController.Execute(rc);
                System.Diagnostics.Trace.WriteLine("--Success executing error controller for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                if (clearError)
                {
                    context.ClearError();
                }
                return true;
            }
            catch (Exception exErrorController)
            {
                System.Diagnostics.Trace.WriteLine("--Failure. Error executing error controller for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Exception: " + exErrorController.Message + " Original Exception: " + ex.Message);
                return false;
            }
        }
开发者ID:berlstone,项目名称:GStore,代码行数:72,代码来源:ExceptionHandler.cs

示例2: HandleErrors

        /// <summary>
        /// Fixes a bug in some configurations of ASP.Net and Mono where a thrown HttpException will cause a redirect with an improper status code
        /// </summary>
        public static void HandleErrors(HttpContext Context)
        {
            try
            {
                if (RunIt(Context) == false)
                {
                    return;
                }
            }
            catch
            {
                throw new NotSupportedException("CustomErrorsFixer does not work in Medium trust");
            }
            HttpException ex = Context.Error as HttpException;
            if(ex==null){
                try{
                    ex=Context.Error.InnerException as HttpException;
                }catch{
                    ex=null;
                }
            }

            if (ex != null){
                Context.Response.StatusCode = ex.GetHttpCode();
            }else{
                Context.Response.StatusCode = 500;
            }
            Context.ClearError();
            Context.Server.Transfer(GetCustomError(Context.Response.StatusCode.ToString()));

            HttpContext.Current.Response.End();
        }
开发者ID:Earlz,项目名称:lucidmvc,代码行数:35,代码来源:CustomErrorsFixer.cs

示例3: TryDisplayStaticPage

        public static bool TryDisplayStaticPage(ErrorPage errorPage, int httpStatusCode, Exception ex, bool clearError, HttpContext context, BaseController controller)
        {
            string url = string.Empty;
            string rawUrl = string.Empty;
            if (context != null)
            {
                url = context.Request.Url.ToString();
                rawUrl = context.Request.RawUrl;
            }

            if (controller != null && controller.CurrentStoreFrontOrNull != null)
            {
                //start with storefront error pages folder
                Models.StoreFront storeFront = controller.CurrentStoreFrontOrThrow;

                string customErrorFolder = storeFront.StoreFrontVirtualDirectoryToMap(controller.Request.ApplicationPath) + "/ErrorPages/";
                string customErrorPath = customErrorFolder + errorPage.ErrorPageFileName();
                bool customErrorFileExists = false;
                if (System.IO.File.Exists(context.Server.MapPath(customErrorPath)))
                {
                    customErrorFileExists = true;
                }
                else
                {
                    //if store front error page not found, look for client custom error page
                    customErrorFolder = storeFront.Client.ClientVirtualDirectoryToMap(context.Request.ApplicationPath) + "/ErrorPages/";
                    customErrorPath = customErrorFolder + errorPage.ErrorPageFileName();
                    if (System.IO.File.Exists(context.Server.MapPath(customErrorPath)))
                    {
                        customErrorFileExists = true;
                    }
                }

                if (customErrorFileExists)
                {
                    try
                    {
                        context.Response.Clear();
                        context.Response.StatusCode = httpStatusCode;
                        System.Diagnostics.Trace.WriteLine("--Displaying custom error static page: " + customErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                        context.Server.Execute(customErrorPath);
                        context.Response.Flush();
                        System.Diagnostics.Trace.WriteLine("--Success displaying custom error static page: " + customErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                        if (clearError)
                        {
                            context.ClearError();
                        }
                        return true;

                    }
                    catch (Exception exCustomPageError)
                    {
                        System.Diagnostics.Trace.WriteLine("--Failure. Error displaying custom error static page: " + customErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Exception: " + exCustomPageError.Message + " Original Exception: " + ex.Message);
                        string error = exCustomPageError.Message;
                    }
                }
            }

            //unknown client or client does not have a custom error page, show server static error page
            string serverErrorFolder = "~/Content/Server/ErrorPages/";
            string serverErrorPath = serverErrorFolder + errorPage.ErrorPageFileName();
            if (!System.IO.File.Exists(context.Server.MapPath(serverErrorPath)))
            {
                System.Diagnostics.Trace.WriteLine("--Failure. Error: Server error static page not found: " + serverErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                return false;
            }

            try
            {
                context.Response.Clear();
                context.Response.StatusCode = httpStatusCode;
                System.Diagnostics.Trace.WriteLine("--Displaying server error static page: " + serverErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl);
                context.Server.Execute(serverErrorPath);
                context.Response.Flush();
                System.Diagnostics.Trace.WriteLine("--Success displaying server error static page: " + serverErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl);
                if (clearError)
                {
                    context.Server.ClearError();
                }
                return true;
            }
            catch (Exception exServerPageError)
            {
                System.Diagnostics.Trace.WriteLine("--Failure. Error displaying server error static page: " + serverErrorPath + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Exception: " + exServerPageError.Message + " Original Exception: " + ex.Message);
                return false;
            }
        }
开发者ID:berlstone,项目名称:GStore,代码行数:87,代码来源:ExceptionHandler.cs

示例4: TryDisplayControllerErrorView

        public static bool TryDisplayControllerErrorView(ErrorPage errorPage, int httpStatusCode, Exception ex, bool clearError, HttpContext context, BaseController controller)
        {
            string url = string.Empty;
            string rawUrl = string.Empty;
            if (context != null)
            {
                url = context.Request.Url.ToString();
                rawUrl = context.Request.RawUrl;
            }

            try
            {
                if (controller == null)
                {
                    return false;
                }

                System.Diagnostics.Trace.WriteLine("--Executing controller.TryDisplayErrorView for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                bool resultSuccess = controller.TryDisplayErrorView(ex, errorPage, httpStatusCode, false);
                System.Diagnostics.Trace.WriteLine("--" + (resultSuccess ? "Success" : "Failure") + " executing controller.TryDisplayErrorView. Result: " + resultSuccess.ToString() + " for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Original Exception: " + ex.Message);
                if (resultSuccess)
                {
                    if (clearError)
                    {
                        context.ClearError();
                    }
                    return true;
                }
                return false;
            }
            catch (Exception exController)
            {
                System.Diagnostics.Trace.WriteLine("--Error executing controller.TryDisplayErrorView for ErrorPage: " + errorPage.ToString() + " Url: " + url + " RawUrl: " + rawUrl + " Exception: " + exController.Message + " Original Exception: " + ex.Message);
                return false;
            }
        }
开发者ID:berlstone,项目名称:GStore,代码行数:36,代码来源:ExceptionHandler.cs

示例5: HandleError

        public static void HandleError(HttpContext ctx)
        {
            if (ctx.Request.IsLocal || ctx.Request.Url.IsLoopback || ctx.Request.Url.Host == "localhost")
                return;

            var req = ctx.Request;

            if (req == null)
            {
                ctx.ClearError();
                ctx.Response.Redirect("~/Errors/500.aspx?s=1", true);
                return;
            }

            string u = req.Url.PathAndQuery.ToLower();

            if (u.StartsWith("/Errors/"))
            {
                ctx.ClearError();
                ctx.Response.Write("An unrecoverable error has been encountered. We apologize for any inconvience.");
                ctx.Response.End();
                return;
            }

            if (u.StartsWith("/scriptresource.axd"))
            {
                ctx.ClearError();
                ctx.Response.Redirect("~/Errors/500.aspx?s=2", true);
                return;
            }

            Exception ex = ctx.Error;

            LogError(ex,ctx);

            ctx.ClearError();

            if( ctx.Request.ContentType.ToLower().StartsWith( "application/json" ) || (ctx.Request.Headers["X-Requested-With"] == "XMLHttpRequest") )
            {
                ctx.Response.ContentType = "application/json";
                ctx.Response.StatusCode = 200;
                ctx.Response.TrySkipIisCustomErrors = true;
                ctx.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new Framework.API.ReturnObject
                {
                    StatusCode = 500,
                    Message = ex.ToString(),
                    Result = ex
                }));
                ctx.Response.End();
            }
            else
            {
                ctx.Response.ContentType = "application/json";
                ctx.Response.StatusCode = 200;
                ctx.Response.TrySkipIisCustomErrors = true;
                ctx.Response.Write( Newtonsoft.Json.JsonConvert.SerializeObject( new Framework.API.ReturnObject {
                    StatusCode = 500,
                    Message = ex.ToString(),
                    Result = ex
                } ) );
                ctx.Response.Flush();
                ctx.Response.End();
                //ctx.Response.Redirect("~/Errors/500.aspx?s=3", true);
            }
        }
开发者ID:REMSLogic,项目名称:REMSLogic,代码行数:65,代码来源:Manager.cs

示例6: ProcessException

		private static void ProcessException(HttpContext context)
		{
			if (context.Error != null)
			{
				string detail = EnvironmentHelper.GetEnvironmentInfo() + "\r\n" +
					context.Error.GetAllStackTrace();

				Exception realEx = ExceptionHelper.GetRealException(context.Error);

				context.ClearError();

				realEx.TryWriteAppLog(detail);

				ResponseErrorMessage(context, realEx.Message, detail);
			}
		}
开发者ID:jerryshi2007,项目名称:AK47Source,代码行数:16,代码来源:ApplicationErrorModule.cs

示例7: ShowCustomErrorPage

        private void ShowCustomErrorPage(Exception exception, HttpContext httpContext, string currentController, string currentAction)
        {
            var controller = new ErrorController();
            var routeData = new RouteData();
            var action = "Index";

            if (exception is HttpException)
            {
                var httpEx = exception as HttpException;

                switch (httpEx.GetHttpCode())
                {
                    case 404:
                        action = "NotFound";
                        break;

                    // others if any
                }
            }

            httpContext.ClearError();
            httpContext.Response.Clear();
            httpContext.Response.StatusCode = exception is HttpException ? ((HttpException)exception).GetHttpCode() : 500;
            httpContext.Response.TrySkipIisCustomErrors = true;

            routeData.Values["controller"] = "Error";
            routeData.Values["action"] = action;

            controller.ViewData.Model = new HandleErrorInfo(exception, currentController, currentAction);
            ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
        }
开发者ID:piotrosz,项目名称:MVCSamples,代码行数:31,代码来源:Global.asax.cs


注:本文中的System.Web.HttpContext.ClearError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。