Пример #1
0
        public void Page_Load(object sender, System.EventArgs e)
        {
            HttpContext.Current.RewritePath(Request.ApplicationPath, false);
            IHttpHandler httpHandler = new MvcHttpHandler();

            httpHandler.ProcessRequest(HttpContext.Current);
        }
        /// <summary>
        /// Redirect to another route using a server side transfer (so the client URL does not change)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="routeValues"></param>
        private void ServerTransferToRoute(ActionExecutingContext context, object routeValues)
        {
            var httpContextBase = context.HttpContext;

            var rc = new RequestContext(httpContextBase, context.RouteData);
            string url = RouteTable.Routes.GetVirtualPath(rc,
                new RouteValueDictionary(routeValues)).VirtualPath;

            // MVC 3 running on IIS 7+
            if (HttpRuntime.UsingIntegratedPipeline)
            {
                httpContextBase.Server.TransferRequest(url, true);
            }
            else
            {
                // Pre IIS7

                // Get the current application to get the real HttpContext
                var app = (HttpApplication)httpContextBase.GetService(typeof(HttpApplication));

                // Rewrite the path of the request
                httpContextBase.RewritePath(url, false);

                // Process the modified request
                IHttpHandler httpHandler = new MvcHttpHandler();
                httpHandler.ProcessRequest(app.Context);
            }
        }
Пример #3
0
        public override void ExecuteResult(ControllerContext context)
        {
            //Get url
            string url = "";
            if (!string.IsNullOrEmpty(_url)) url = _url;
            else
            {
                //Create route
                var routeValues = new RouteValueDictionary(_Values);
                routeValues.Add("Action", _Action);
                routeValues.Add("Controller", _Controller);

                //Must persist ajax
                var request = HttpContext.Current.Request;
                if ((request["X-Requested-With"] != null &&
                    request["X-Requested-With"].Equals("XmlHttpRequest", StringComparison.InvariantCultureIgnoreCase)) ||
                    request.QueryString["_"] != null ||
                    context.HttpContext.Items["__IsAjaxRequest"] != null)
                    routeValues.Add("X-Requested-With", "XmlHttpRequest");

                url = RouteTable.Routes.GetVirtualPath(context.RequestContext, routeValues).VirtualPath;
            }

            HttpContext.Current.RewritePath(url, false);
            IHttpHandler httpHandler = new MvcHttpHandler();
            httpHandler.ProcessRequest(HttpContext.Current);

            //httpContext.Server.TransferRequest(url, true);
        }
Пример #4
0
 protected void Page_Load(object sender, EventArgs e)
 {
     string originalPath = Request.Path;
     HttpContext.Current.RewritePath(Request.ApplicationPath, false);
     IHttpHandler httpHandler = new MvcHttpHandler();
     httpHandler.ProcessRequest(HttpContext.Current);
     HttpContext.Current.RewritePath(originalPath, false);
 }
Пример #5
0
 protected void Page_Load(object sender, EventArgs e)
 {
     string path = Request.Path;
     HttpContext.Current.RewritePath(Request.ApplicationPath,false);
     IHttpHandler handler = new System.Web.Mvc.MvcHttpHandler();
     handler.ProcessRequest(HttpContext.Current);
     HttpContext.Current.RewritePath(path);
 }
        public override void ExecuteResult(ControllerContext context)
        {
            var httpContext = HttpContext.Current;

            httpContext.RewritePath(Url, true);

            IHttpHandler httpHandler = new MvcHttpHandler();
            httpHandler.ProcessRequest(HttpContext.Current);
        }
Пример #7
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string path = Request.Path;

            HttpContext.Current.RewritePath(Request.ApplicationPath, false);
            IHttpHandler handler = new System.Web.Mvc.MvcHttpHandler();

            handler.ProcessRequest(HttpContext.Current);
            HttpContext.Current.RewritePath(path);
        }
Пример #8
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);
		}
Пример #9
0
        /// <summary>
        /// Raises the page's Load event.
        /// </summary>
        /// <param name="e">The event arguments.</param>
        protected override void OnLoad(EventArgs e)
        {
            string originalPath = Request.Path;
            HttpContext.Current.RewritePath(Request.ApplicationPath, false);
            IHttpHandler httpHandler = new MvcHttpHandler();
            httpHandler.ProcessRequest(HttpContext.Current);
            HttpContext.Current.RewritePath(originalPath, false);

            base.OnLoad(e);
        }
Пример #10
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);
        }
        protected void Application_Error(object sender, EventArgs eventArgs)
        {
            if (!HttpContext.Current.IsCustomErrorEnabled)
            {
                // Don't handle the error in this case.

                return;
            }

            var exception = HttpContext.Current.Server.GetLastError();
            exception = exception == null ? exception : exception.GetBaseException();

            // Log the error here in some way. This example uses ELMAH so logging is handled via a module.

            var appRelativePath = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;

            if (appRelativePath.StartsWith("~/" + ErrorControllerName + "/", StringComparison.OrdinalIgnoreCase))
            {
                TransferToFatalErrorPage();
                return;
            }

            const string errorRoute = "~/" + ErrorControllerName + "/ServerError";

            try
            {
                if (HttpRuntime.UsingIntegratedPipeline)
                {
                    // For IIS 7+

                    HttpContext.Current.Server.TransferRequest(errorRoute, false, "GET", null);
                }
                else
                {
                    // For IIS 6

                    var originalPath = HttpContext.Current.Request.Path;
                    HttpContext.Current.RewritePath(errorRoute);

                    IHttpHandler handler = new MvcHttpHandler();
                    handler.ProcessRequest(HttpContext.Current);

                    HttpContext.Current.RewritePath(originalPath);
                    HttpContext.Current.Server.ClearError();
                }
            }
            catch (Exception)
            {
                // Log the error here in some way.

                TransferToFatalErrorPage();
            }
        }
Пример #12
0
        public static void Transfer(ControllerContext filterContext, string url)
        {
            lock (objectTransfer)
            {
                // Rewrite path
                HttpContext.Current.RewritePath(GetPath(filterContext, url), false);

                IHttpHandler httpHandler = new System.Web.Mvc.MvcHttpHandler();

                // Process request
                httpHandler.ProcessRequest(HttpContext.Current);
                filterContext.HttpContext.Response.End();
            }
        }
Пример #13
0
        public override void ExecuteResult(ControllerContext context)
        {
            // MVC 3 running on IIS 7+
            if (HttpRuntime.UsingIntegratedPipeline)
            {
                context.HttpContext.Server.TransferRequest(_url);
                //context.HttpContext.Server.TransferRequest(_url, true);
            }
            else
            {
                // Pre MVC 3
                context.HttpContext.RewritePath(_url, false);

                IHttpHandler httpHandler = new MvcHttpHandler();
                httpHandler.ProcessRequest(context.HttpContext.ApplicationInstance.Context);
            }
        }
Пример #14
0
        public void Page_Load(object sender, System.EventArgs e)
        {
            string originalPath = Request.Path;
            HttpContext.Current.RewritePath(Request.ApplicationPath, false);
            IHttpHandler httpHandler = new MvcHttpHandler();
            httpHandler.ProcessRequest(HttpContext.Current);
            HttpContext.Current.RewritePath(originalPath, false);

            //Server.Transfer("/home/index.html");
            //Response.Redirect("/adminbbs/ForbidMain");
            //Response.Redirect("/AdminNews/NewsType/1101");
            //Response.Redirect("/user/index.html");
            Response.Redirect("/bbs/index.html");
            //Response.Redirect("/subject/index.html");
            //Response.Redirect("/AdminLogin/index.html");
            //Response.Redirect("/CompanyManager/index.html");
            //Response.Redirect("/Register/Index.html");
        }
Пример #15
0
        protected void Page_PreInit(object sender, System.EventArgs e)
        {
            string url = "/";
            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);
            }
        }
Пример #16
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);
                }
            }
        }
Пример #17
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);
        }
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
                throw new ArgumentNullException("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);
            }
        }
Пример #19
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);
            }
        }
Пример #20
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var httpContext = HttpContext.Current;

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

                IHttpHandler httpHandler = new MvcHttpHandler();
                httpHandler.ProcessRequest(httpContext);
            }
        }
Пример #21
0
 protected void Page_Load(object sender, EventArgs e)
 {
     if (Request.ApplicationPath != null) HttpContext.Current.RewritePath(Request.ApplicationPath);
     IHttpHandler httpHandler = new MvcHttpHandler();
     httpHandler.ProcessRequest(HttpContext.Current);
 }
Пример #22
0
        protected void RenderAction(string actionName, string controllerName, object routeValues)
        {
            //Get Url
            RouteValueDictionary routes = null;
            if (routeValues != null) routes = new RouteValueDictionary(routeValues);
            else routes = new RouteValueDictionary();
            routes.Add("Action", actionName);
            if (!string.IsNullOrEmpty(controllerName)) routes.Add("Controller", controllerName);
            else routes.Add("Controller", this.ControllerContext.RouteData.Values["Controller"].ToString());
            var url = RouteTable.Routes.GetVirtualPath(this.ControllerContext.RequestContext, routes).VirtualPath;

            //Rewrite path
            System.Web.HttpContext.Current.RewritePath(url, false);
            IHttpHandler httpHandler = new MvcHttpHandler();
            httpHandler.ProcessRequest(System.Web.HttpContext.Current);

            //httpContext.Server.TransferRequest(url, true);
        }
        public virtual void FileNotFoundHandler(object sender, EventArgs evt)
        {
            // Check if this should be enabled
            if (_redirectConfiguration.FileNotFoundHandlerMode == FileNotFoundMode.Off)
                return;

            Logger.Debug("FileNotFoundHandler called");

            HttpContext context = HttpContext.Current;
            if (context == null)
            {
                Logger.Debug("No HTTPContext, returning");
                return;
            }

            if (context.Response.StatusCode != 404)
                return;
            var query = context.Request.ServerVariables["QUERY_STRING"];
            if ((query != null) && query.StartsWith("404;"))
            {
                return;
            }

            Uri notFoundUri = context.Request.Url;

            // Skip resource files
            if (IsResourceFile(notFoundUri))
                return;

            // If we're only doing this for remote users, we need to test for local host
            if (_redirectConfiguration.FileNotFoundHandlerMode == FileNotFoundMode.RemoteOnly)
            {
                // Determine if we're on localhost
                bool localHost = IsLocalhost();
                if (localHost)
                {
                    Logger.Debug("Determined to be localhost, returning");
                    return;
                }
                Logger.Debug("Not localhost, handling error");
            }

            // Avoid looping forever
            if (IsInfiniteLoop(context))
                return;

            var redirect = HandleRequest(GetReferer(context.Request.UrlReferrer), notFoundUri);
            if (redirect.Redirected)
                context.Response.RedirectPermanent(redirect.NewUrl);
            else
            {
                string url = Get404Url();

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

                // do the redirect to the 404 page here
                if (HttpRuntime.UsingIntegratedPipeline)
                {
                    context.Server.TransferRequest(url, true);
                }
                else
                {
                    context.RewritePath(url, false);
                    IHttpHandler httpHandler = new MvcHttpHandler();
                    httpHandler.ProcessRequest(context);
                }
                // return the original status code to the client
                // (this won't work in integrated pipleline mode)
                context.Response.StatusCode = 404;
            }
        }
Пример #24
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);
                }
            }
        }
        private static void Application_Error(object sender, EventArgs e)
        {
            var handleServerErrors =
                ConfigurationManager.AppSettings.Include(ManualServerErrorHandlingMode.ApplicationErrorHandler);

            var handleNotFoundErrors =
                ConfigurationManager.AppSettings.Include(ManualNotFoundErrorHandlingMode.ApplicationErrorHandler);

            if (!handleServerErrors && !handleNotFoundErrors)
            {
                return;
            }

            // Get the last error.

            var exception = HttpContext.Current.Server.GetLastError();

            if (exception is HttpUnhandledException)
            {
                exception = exception.InnerException ?? exception;
            }

            var httpException = exception as HttpException;

            // The exception can be logged here if you want.

            // Determine if the error should be handled by this module, and if so, how.

            var statusCode = httpException != null
                ? httpException.GetHttpCode()
                : (int) HttpStatusCode.InternalServerError;

            if (statusCode == (int)HttpStatusCode.NotFound)
            {
                if (!handleNotFoundErrors)
                {
                    return;
                }
            }
            else if (!handleServerErrors)
            {
                return;
            }

            if (ConfigurationManager.AppSettings.Include(ErrorPageExceptionsMode.HandleInApplicationError))
            {
                // Handle the case where an error page threw an exception; we don't want to loop back to that code.

                if (HttpContext.Current.Request.Path.StartsWithIgnoreCase("/" + ErrorControllerRouteName + "/"))
                {
                    HandleErrorHandlingError(
                        "The error handling code threw an exception.",
                        HttpContext.Current.Server.GetLastError());

                    return;
                }
            }

            try
            {
                if (ConfigurationManager.AppSettings.Include(ManualErrorsRenderingMode.ViewResult))
                {
                    // ***** The View Result Approach *****

                    HttpContext.Current.Response.ClearHeaders();
                    HttpContext.Current.Response.ClearContent();
                    HttpContext.Current.Response.StatusCode = statusCode;

                    if (ConfigurationManager.AppSettings.Include(TrySkipIisCustomErrorsMode.Skip))
                    {
                        HttpContext.Current.Response.TrySkipIisCustomErrors = true;
                    }

                    var routeData = new RouteData();

                    routeData.Values.Add("controller", ErrorControllerRouteName);

                    var controllerContext = new ControllerContext(
                        new HttpContextWrapper(HttpContext.Current),
                        routeData,
                        new FakeController());

                    var viewResult = new ViewResult {ViewName = "ManualErrors"};

                    viewResult.ViewBag.ExceptionMessage = exception.Message;
                    viewResult.ViewBag.StatusCode = HttpContext.Current.Response.StatusCode;
                    viewResult.ViewBag.SessionData = MvcApplication.GetSessionData();

                    viewResult.ExecuteResult(controllerContext);

                    HttpContext.Current.Server.ClearError();
                }
                else if (ConfigurationManager.AppSettings.Include(ManualErrorsRenderingMode.ControllerExecute))
                {
                    // ***** The Controller Execute Approach *****

                    var routeData = new RouteData();

                    routeData.Values.Add("controller", ErrorControllerRouteName);
                    routeData.Values.Add("action", "ManualErrors");
                    routeData.Values.Add("statusCode", statusCode);

                    var requestContext = new RequestContext(new HttpContextWrapper(HttpContext.Current), routeData);

                    var controllerFactory = ControllerBuilder.Current.GetControllerFactory();

                    var controller = controllerFactory.CreateController(requestContext, ErrorControllerRouteName);

                    controller.Execute(requestContext);

                    HttpContext.Current.Server.ClearError();
                }
                else if (ConfigurationManager.AppSettings.Include(ManualErrorsRenderingMode.TransferRequest))
                {
                    // ***** The TransferRequest Approach *****

                    // This method of directing back to MVC does not preserve TempData.

                    if (HttpContext.Current.Session != null)
                    {
                        HttpContext.Current.Session["exception"] = exception;
                    }

                    var url = "~/Error/ManualErrors?statusCode=" + statusCode;

                    HttpContext.Current.Server.TransferRequest(url, false, "GET", null);
                }
                else if (ConfigurationManager.AppSettings.Include(ManualErrorsRenderingMode.MvcHttpHandler))
                {
                    // ***** The MvcHttpHandler Approach *****

                    var path = HttpContext.Current.Request.Path;

                    HttpContext.Current.RewritePath("~/Error/ManualErrors");

                    if (HttpContext.Current.Session != null)
                    {
                        HttpContext.Current.Session["statusCode"] = statusCode;
                    }

                    IHttpHandler handler = new MvcHttpHandler();

                    handler.ProcessRequest(HttpContext.Current);

                    HttpContext.Current.RewritePath(path);

                    HttpContext.Current.Server.ClearError();
                }
            }
            catch (Exception renderException)
            {
                if (ConfigurationManager.AppSettings.Include(ErrorPageExceptionsMode.HandleInApplicationError))
                {
                    HandleErrorHandlingError("The MVC manual error handling code threw an exception.", renderException);
                }
                else
                {
                    throw;
                }
            }
        }