Esempio n. 1
0
        public void Page_Load(object sender, System.EventArgs e)
        {
            // 改变当前路径路由处理程序可以正确解释请求,
            // 然后恢复原来的路径,以便在OutputCache模块
            // 可以正确处理响应(如果缓存已启用).

            string originalPath = Request.Path;

            HttpContext.Current.RewritePath(Request.ApplicationPath, false);
            IHttpHandler httpHandler = new MvcHttpHandler();

            httpHandler.ProcessRequest(HttpContext.Current);
            HttpContext.Current.RewritePath(originalPath, false);
        }
Esempio n. 2
0
        public void Page_Load(object sender, System.EventArgs e)
        {
            // Change the current path so that the Routing handler can correctly interpret
            // the request, then restore the original path so that the OutputCache module
            // can correctly process the response (if caching is enabled).

            string originalPath = Request.Path;

            HttpContext.Current.RewritePath(Request.ApplicationPath, false);
            IHttpHandler httpHandler = new MvcHttpHandler();

            httpHandler.ProcessRequest(HttpContext.Current);
            HttpContext.Current.RewritePath(originalPath, false);
        }
Esempio n. 3
0
    public void ProcessRequest(HttpContext httpContext)
    {
        if (httpContext.User.IsInRole(Role))
        {
            RouteValueDictionary routeValues = new RouteValueDictionary(RouteValues);
            // put logic here to create path similar to what you were doing
            // before but you will need to replace any keys in your route
            // with the values from the dictionary created above.
            httpContext.RewritePath(path);
        }
        IHttpHandler handler = new MvcHttpHandler();

        handler.ProcessRequest(httpContext);
    }
Esempio n. 4
0
        public override void ExecuteResult(ControllerContext context)
        {
            using (new TransactionScope(TransactionScopeOption.RequiresNew)) {
                var httpContext = HttpContext.Current;

                // See http://stackoverflow.com/questions/799511/how-to-simulate-server-transfer-in-asp-net-mvc/799534
                // MVC 3 running on IIS 7+
                if (HttpRuntime.UsingIntegratedPipeline)
                {
                    httpContext.Server.TransferRequest(Url, true);
                }
                else
                {
                    // Pre MVC 3
                    httpContext.RewritePath(Url, false);

                    IHttpHandler httpHandler = new MvcHttpHandler();
                    httpHandler.ProcessRequest(httpContext);
                }
            }
        }
Esempio n. 5
0
        public override void ExecuteResult(ControllerContext context)
        {
            var httpContext = HttpContext.Current;

            //Transfering breaks temp data so just destroy it.  You wont need it on the error page anyway.
            context.Controller.TempData.Clear();

            // MVC 3 running on IIS 7+
            if (HttpRuntime.UsingIntegratedPipeline)
            {
                httpContext.Server.TransferRequest(Url, false);
            }
            else
            {
                // Pre MVC 3
                var thisJttpContext = context.HttpContext;
                thisJttpContext.RewritePath(Url, false);
                IHttpHandler httpHandler = new MvcHttpHandler();
                httpHandler.ProcessRequest(HttpContext.Current);
            }
        }
Esempio n. 6
0
        void Application_Error(object sender, EventArgs e)
        {
            var error = Server.GetLastError();
            var code  = (error is HttpException) ? (error as HttpException).GetHttpCode() : 500;

            if (code != 404)
            {
                //
            }

            Response.Clear();
            Server.ClearError();

            string path = Request.Path;

            Context.RewritePath(string.Format("~/Errors/Http{0}", code), false);
            IHttpHandler httpHandler = new MvcHttpHandler();

            httpHandler.ProcessRequest(Context);
            Context.RewritePath(path, false);
        }
Esempio n. 7
0
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var httpContext = System.Web.HttpContext.Current;

            // MVC 3+
            if (System.Web.HttpRuntime.UsingIntegratedPipeline)
            {
                httpContext.Server.TransferRequest(this.Url, true);
            }
            else
            {
                // Pre MVC 3
                httpContext.RewritePath(this.Url, false);

                System.Web.IHttpHandler httpHandler = new MvcHttpHandler();
                httpHandler.ProcessRequest(httpContext);
            }
        }
Esempio n. 8
0
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var httpContext = HttpContext.Current;

            // MVC 3 running on IIS 7+
            if (HttpRuntime.UsingIntegratedPipeline)
            {
                httpContext.Server.TransferRequest(Url, true);
            }
            else
            {
                // Pre MVC 3
                httpContext.RewritePath(Url, false);

                IHttpHandler httpHandler = new MvcHttpHandler();
                httpHandler.ProcessRequest(httpContext);
            }
        }
Esempio n. 9
0
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (context.Controller.TempData != null && context.Controller.TempData.Count > 0)
            {
                throw new ApplicationException("TempData won't work with Server.TransferRequest!");
            }

            if (HttpRuntime.UsingIntegratedPipeline)
            {
                HttpContext.Current.Server.TransferRequest(Url, true);
            }
            else
            {
                HttpContext.Current.RewritePath(Url, false);
                IHttpHandler httpHandler = new MvcHttpHandler();

                httpHandler.ProcessRequest(HttpContext.Current);
            }
        }
Esempio n. 10
0
        protected void Application_EndRequest(object sender, EventArgs e)
        {
            // only handle rewrite mode, ignore redirect configuration (if it ain't broke don't re-implement it)
            if (CustomErrors.RedirectMode == CustomErrorsRedirectMode.ResponseRewrite && HttpContext.IsCustomErrorEnabled)
            {
                int statusCode = HttpContext.Response.StatusCode;

                // if this request has thrown an exception then find the real status code
                Exception exception = HttpContext.Error;
                if (exception != null)
                {
                    // set default error status code for application exceptions
                    statusCode = (int)HttpStatusCode.InternalServerError;
                }

                HttpException httpException = exception as HttpException;
                if (httpException != null)
                {
                    statusCode = httpException.GetHttpCode();
                }

                if ((HttpStatusCode)statusCode != HttpStatusCode.OK)
                {
                    Dictionary <int, string> errorPaths = new Dictionary <int, string>();

                    foreach (CustomError error in CustomErrors.Errors)
                    {
                        errorPaths.Add(error.StatusCode, error.Redirect);
                    }

                    // find a custom error path for this status code
                    if (errorPaths.Keys.Contains(statusCode))
                    {
                        string url = errorPaths[statusCode];

                        // avoid circular redirects
                        if (!HttpContext.Request.Url.AbsolutePath.Equals(VirtualPathUtility.ToAbsolute(url)))
                        {
                            HttpContext.Response.Clear();
                            HttpContext.Response.TrySkipIisCustomErrors = true;

                            HttpContext.Server.ClearError();

                            // do the redirect here
                            if (HttpRuntime.UsingIntegratedPipeline)
                            {
                                HttpContext.Response.StatusCode = statusCode;
                                HttpContext.Server.TransferRequest(url, true);
                            }
                            else
                            {
                                HttpContext.RewritePath(url, false);

                                IHttpHandler httpHandler = new MvcHttpHandler();
                                httpHandler.ProcessRequest(HttpContext);
                            }

                            // return the original status code to the client
                            // (this won't work in integrated pipleline mode)
                            HttpContext.Response.StatusCode = statusCode;
                        }
                    }
                }
            }
        }
Esempio n. 11
0
        private void ContextError(object sender, EventArgs e)
        {
            WindsorAccessor.Instance.Container.Resolve <ILogWriter>()
            .Error(@"Error ------------------------------------------------------------");

            var httpContext = HttpContext.Current;

            var imageRequestTypes =
                httpContext.Request.AcceptTypes.Where(a => a.StartsWith("image/")).Select(a => a.Count());

            if (imageRequestTypes.Count() > 0)
            {
                httpContext.ClearError();
                return;
            }

            var lastException = HttpContext.Current.Server.GetLastError().GetBaseException();
            var httpException = lastException as HttpException;
            var statusCode    = (int)HttpStatusCode.InternalServerError;

            if (httpException != null)
            {
                statusCode = httpException.GetHttpCode();
                if ((statusCode != (int)HttpStatusCode.NotFound) &&
                    (statusCode != (int)HttpStatusCode.ServiceUnavailable))
                {
                    WindsorAccessor.Instance.Container.Resolve <ILogWriter>()
                    .Error(EventIds.Error, "exception=", httpException);
                }
            }

            var redirectUrl = string.Empty;

            if (httpContext.IsCustomErrorEnabled)
            {
                var errorsSection = WebConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;
                if (errorsSection != null)
                {
                    redirectUrl = errorsSection.DefaultRedirect;

                    if (httpException != null && errorsSection.Errors.Count > 0)
                    {
                        var item = errorsSection.Errors[statusCode.ToString()];

                        if (item != null)
                        {
                            redirectUrl = item.Redirect;
                        }
                    }
                }
            }

            httpContext.Response.Clear();
            httpContext.Response.StatusCode             = statusCode;
            httpContext.Response.TrySkipIisCustomErrors = true;
            httpContext.ClearError();

            if (!string.IsNullOrEmpty(redirectUrl))
            {
                var mvcHandler = httpContext.CurrentHandler as MvcHandler;
                if (mvcHandler == null)
                {
                    httpContext.Server.Transfer(redirectUrl);
                }
                else
                {
                    var uriBuilder = new UriBuilder(
                        httpContext.Request.Url.Scheme,
                        httpContext.Request.Url.Host,
                        httpContext.Request.Url.Port,
                        httpContext.Request.ApplicationPath);

                    uriBuilder.Path += redirectUrl;

                    string path = httpContext.Server.UrlDecode(uriBuilder.Uri.PathAndQuery);
                    HttpContext.Current.RewritePath(path, false);
                    IHttpHandler httpHandler = new MvcHttpHandler();

                    httpHandler.ProcessRequest(HttpContext.Current);
                }
            }
        }
Esempio n. 12
0
        public void Page_Load(object sender, System.EventArgs e)
        {
            IHttpHandler httpHandler = new MvcHttpHandler();

            httpHandler.ProcessRequest(HttpContext.Current);
        }
Esempio n. 13
0
        /// <summary></summary>
        protected void Application_EndRequest(object sender, EventArgs e)
        {
            var httpContext = HttpContext.Current;

            if (!_isRewrite || !httpContext.IsCustomErrorEnabled)
            {
                return;
            }


            int    status  = httpContext.Response.StatusCode;
            string message = "" + httpContext.Response.StatusDescription;

            var httpException = httpContext.Error as HttpException;

            if (httpException != null)
            {
                status = httpException.GetHttpCode();
            }
            else if (httpContext.Error != null)
            {
                status = (int)HttpStatusCode.InternalServerError;
            }


            if (HttpStatusCode.OK.Equals(status))
            {
                return;
            }
            if (!_errorPaths.ContainsKey(status))
            {
                return;
            }

            string url = _errorPaths[status];

            /* avoid circular redirects */
            if (httpContext.Request.Url.AbsolutePath.Equals(VirtualPathUtility.ToAbsolute(url)))
            {
                return;
            }

            if (message.StartsWith("[") && message.EndsWith("]"))
            {
                var queryString = HttpUtility.ParseQueryString("");
                queryString["message"] = message.Trim('[', ']');
                url = url + "?" + queryString;
            }

            httpContext.Response.Clear();
            httpContext.Server.ClearError();
            httpContext.Response.TrySkipIisCustomErrors = true;


            if (HttpRuntime.UsingIntegratedPipeline)
            {
                httpContext.Server.TransferRequest(url, true);
            }
            else
            {
                httpContext.RewritePath(url, false);

                IHttpHandler httpHandler = new MvcHttpHandler();
                httpHandler.ProcessRequest(httpContext);
            }

            httpContext.Response.StatusCode = status;
        }