コード例 #1
0
        public void CreateFromVirtualPathTest()
        {
            var contents   = "test";
            var textWriter = new StringWriter();

            var httpResponse = new Mock <HttpResponseBase>();

            httpResponse.SetupGet(r => r.Output).Returns(textWriter);
            var httpContext      = Utils.CreateTestContext(response: httpResponse.Object);
            var mockBuildManager = new Mock <IVirtualPathFactory>();
            var virtualPath      = "~/hello/test.cshtml";
            var page             = Utils.CreatePage(p => p.Write(contents));

            mockBuildManager.Setup(c => c.Exists(It.Is <string>(p => p.Equals(virtualPath)))).Returns <string>(_ => true).Verifiable();
            mockBuildManager.Setup(c => c.CreateInstance(It.Is <string>(p => p.Equals(virtualPath)))).Returns(page).Verifiable();

            // Act
            IHttpHandler handler = WebPageHttpHandler.CreateFromVirtualPath(virtualPath, new VirtualPathFactoryManager(mockBuildManager.Object));

            Assert.IsType <WebPageHttpHandler>(handler);
            WebPageHttpHandler webPageHttpHandler = (WebPageHttpHandler)handler;

            webPageHttpHandler.ProcessRequestInternal(httpContext.Object);

            // Assert
            Assert.Equal(contents, textWriter.ToString());
            mockBuildManager.Verify();
        }
コード例 #2
0
        public void CreateFromVirtualPathReturnsIHttpHandlerIfItCannotCreateAWebPageType()
        {
            // Arrange
            var pageVirtualPath    = "~/hello/test.cshtml";
            var handlerVirtualPath = "~/handler.asmx";
            var page        = new DummyPage();
            var handler     = new Mock <IHttpHandler>().Object;
            var mockFactory = new Mock <IVirtualPathFactory>();

            mockFactory.Setup(c => c.Exists(It.IsAny <string>())).Returns(true);
            mockFactory.Setup(c => c.CreateInstance(pageVirtualPath)).Returns(page);
            mockFactory.Setup(c => c.CreateInstance(handlerVirtualPath)).Returns(handler);

            // Act
            var handlerResult = WebPageHttpHandler.CreateFromVirtualPath(
                handlerVirtualPath,
                mockFactory.Object
                );
            var pageResult = WebPageHttpHandler.CreateFromVirtualPath(
                pageVirtualPath,
                mockFactory.Object
                );

            // Assert
            Assert.Equal(handler, handlerResult);
            Assert.NotNull(pageResult as WebPageHttpHandler);
        }
コード例 #3
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var filePath = requestContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath;

            if (filePath == "~/")
            {
                filePath = "~/views/index.cshtml";
            }
            else
            {
                if (!filePath.StartsWith("~/views/", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath.Insert(2, "views/");
                }

                if (!filePath.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath += ".cshtml";
                }
            }

            var handler = WebPageHttpHandler.CreateFromVirtualPath(filePath); // returns NULL if .cshtml file wasn't found

            if (handler == null)
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", "/views/404");
                handler = WebPageHttpHandler.CreateFromVirtualPath("~/views/404.cshtml");
            }
            else
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", filePath.Substring(1, filePath.Length - 8));
            }

            return(handler);
        }
コード例 #4
0
    public virtual IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var httpContext = requestContext.HttpContext;

        // Parse incoming URL (we trim off the first two chars since they're always "~/")
        string requestPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo;

        if (requestPath.EndsWith(".static"))
        {
            return(new global::SEOUtils.HtmlSnapshotHttpHandler());
        }

        /*if (Regex.IsMatch(requestPath, global::Settings.Instance.WebSite.PageAlias.Regex))
         * {
         *  var pageToken = global::Service.Data2.Client.PageToken(requestPath);
         *  if (!(pageToken == null || pageToken.Page == null || pageToken.Token == null))
         *  {
         *      //HttpUtility.UrlEncode uses '+' instead of '%20' for spaces in strings
         *      string query = Uri.EscapeDataString(pageToken.Token);
         *      requestContext.HttpContext.Response.Redirect(String.Format("{0}?{1}={2}", pageToken.Page, global::Settings.Instance.WebSite.NavToken.QueryParamName, query));
         *      return null;
         *  }
         * }*/

        return(WebPageHttpHandler.CreateFromVirtualPath(String.Format("~{0}.cshtml", global::Settings.Instance.WebSite.HomePage.ToLower())));
    }
コード例 #5
0
        public void CreateFromVirtualPathNonWebPageTest()
        {
            var handler = new WebPageHttpHandler(new DummyPage());
            var result  = WebPageHttpHandler.CreateFromVirtualPath("~/hello/test.cshtml",
                                                                   (path, type) => handler);

            Assert.AreEqual(handler, result);
        }
コード例 #6
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            try
            {
                //if (IESCookie.GetCookieValue("ies") == string.Empty)
                //{
                //    HttpContext.Current.Response.Redirect("login.aspx");
                //}


                // Use cases:
                //     ~/            -> ~/views/index.cshtml
                //     ~/about       -> ~/views/about.cshtml or ~/views/about/index.cshtml
                //     ~/views/about -> ~/views/about.cshtml
                //     ~/xxx         -> ~/views/404.cshtml
                var filePath = requestContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath;

                if (filePath == "~/")
                {
                    filePath = "~/views/index.cshtml";
                }
                else
                {
                    if (!filePath.StartsWith("~/views/", StringComparison.OrdinalIgnoreCase))
                    {
                        filePath = filePath.Insert(2, "views/");
                    }

                    if (!filePath.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
                    {
                        filePath = filePath += ".cshtml";
                    }
                }
                //if (!requestContext.HttpContext.Request.ApplicationPath.Equals("/"))
                //{
                //    filePath = filePath.Replace("~", requestContext.HttpContext.Request.ApplicationPath);
                //}
                var handler = WebPageHttpHandler.CreateFromVirtualPath(filePath); // returns NULL if .cshtml file wasn't found

                if (handler == null)
                {
                    requestContext.RouteData.DataTokens.Add("templateUrl", "/views/404");
                    handler = WebPageHttpHandler.CreateFromVirtualPath("~/views/404.cshtml");
                }
                else
                {
                    requestContext.RouteData.DataTokens.Add("templateUrl", filePath.Substring(1, filePath.Length - 8));
                    //requestContext.RouteData.DataTokens.Add("templateUrl", filePath);
                }

                return(handler);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #7
0
        /// <summary>
        /// Overridable Method. Default implementation uses Razor WebPages.
        /// </summary>
        /// <param name="exception"></param>
        protected virtual void RenderCustomException(TException exception)
        {
            //executa a página
            var handler = WebPageHttpHandler.CreateFromVirtualPath(ErrorViewPath);

            handler.ProcessRequest(ApplicationInstance.Context);

            ApplicationInstance.Session.Remove("exception");
        }
コード例 #8
0
        public void CreateFromVirtualPathTest()
        {
            var contents    = "test";
            var tw          = new StringWriter(new StringBuilder());
            var httpContext = CreateContext(tw);
            var handler     = WebPageHttpHandler.CreateFromVirtualPath("~/hello/test.cshtml",
                                                                       (path, type) => Utils.CreatePage(p => p.Write(contents)));

            handler.ProcessRequest(httpContext);
            Assert.AreEqual(contents, tw.ToString());
        }
コード例 #9
0
        /// <summary>
        /// Overridable Method. Default implementation uses Razor WebPages.
        /// </summary>
        /// <param name="exception"></param>
        protected virtual void RenderCustomException(TException exception)
        {
            //stores exception in session for later retrieve
            Application.Session["exception"] = exception;

            //executa a página
            var handler = WebPageHttpHandler.CreateFromVirtualPath(ErrorViewPath);

            handler.ProcessRequest(Application.Context);

            Application.Session.Remove("exception");
        }
コード例 #10
0
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var substitutedVirtualPath = GetSubstitutedVirtualPath(requestContext);
        int index = substitutedVirtualPath.IndexOf('?');

        if (index != -1)
        {
            substitutedVirtualPath = substitutedVirtualPath.Substring(0, index);
        }
        requestContext.HttpContext.Items[ContextExtensions.RouteKey] = requestContext.RouteData.Values;
        return(WebPageHttpHandler.CreateFromVirtualPath(substitutedVirtualPath));
    }
コード例 #11
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var substitutedVirtualPath = this.GetSubstitutedVirtualPath(requestContext);
            var index = substitutedVirtualPath.IndexOf('?');

            if (index != -1)
            {
                substitutedVirtualPath = substitutedVirtualPath.Substring(0, index);
            }

            requestContext.HttpContext.SetRouteValue(requestContext.RouteData.Values);
            return(WebPageHttpHandler.CreateFromVirtualPath(substitutedVirtualPath));
        }
コード例 #12
0
ファイル: DefaultRouteHandler.cs プロジェクト: SeanKG/ChatApp
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            // Use cases:
            //     ~/            -> ~/views/index.cshtml
            //     ~/about       -> ~/views/about.cshtml or ~/views/about/index.cshtml
            //     ~/views/about -> ~/views/about.cshtml
            //     ~/xxx         -> ~/views/404.cshtml
            var filePath = requestContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath;

            var basePath = HttpContext.Current.Request.Url.AbsoluteUri;

            if (!basePath.EndsWith("/"))
            {
                basePath += '/';
            }

            requestContext.RouteData.DataTokens.Add("VIRTUAL_PATH", basePath);

            if (filePath == "~/")
            {
                filePath = "~/views/index.cshtml";
            }
            else
            {
                if (!filePath.StartsWith("~/views/", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath.Insert(2, "views/");
                }

                if (!filePath.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath += ".cshtml";
                }
            }

            var handler = WebPageHttpHandler.CreateFromVirtualPath(filePath); // returns NULL if .cshtml file wasn't found

            if (handler == null)
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", "/views/404");
                handler = WebPageHttpHandler.CreateFromVirtualPath("~/views/404.cshtml");
            }
            else
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", filePath.Substring(1, filePath.Length - 8));
            }

            return(handler);
        }
コード例 #13
0
        public void CreateFromVirtualPathNonWebPageTest()
        {
            // Arrange
            var virtualPath      = "~/hello/test.cshtml";
            var handler          = new WebPageHttpHandler(new DummyPage());
            var mockBuildManager = new Mock <IVirtualPathFactory>();

            mockBuildManager.Setup(c => c.CreateInstance(It.IsAny <string>())).Returns(handler);
            mockBuildManager.Setup(c => c.Exists(It.Is <string>(p => p.Equals(virtualPath)))).Returns <string>(_ => true).Verifiable();

            // Act
            var result = WebPageHttpHandler.CreateFromVirtualPath(virtualPath, new VirtualPathFactoryManager(mockBuildManager.Object));

            // Assert
            Assert.Equal(handler, result);
            mockBuildManager.Verify();
        }
コード例 #14
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            // Use cases:
            //     ~/            -> ~/views/index.cshtml
            //     ~/about       -> ~/views/about.cshtml or ~/views/about/index.cshtml
            //     ~/views/about -> ~/views/about.cshtml
            //     ~/xxx         -> ~/views/404.cshtml
            var filePath = requestContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath;

            //if (filePath.ToLower().Contains("api/"))
            //{
            //    HttpControllerRouteHandler routeHandler = ((System.Web.Routing.Route)(requestContext.RouteData.Route)).RouteHandler as HttpControllerRouteHandler;
            //}

            if (filePath == "~/")
            {
                filePath = "~/views/index.cshtml";
            }
            else
            {
                if (!filePath.StartsWith("~/views/", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath.Insert(2, "views/");
                }

                if (!filePath.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath += ".cshtml";
                }
            }

            var handler = WebPageHttpHandler.CreateFromVirtualPath(filePath); // returns NULL if .cshtml file wasn't found

            if (handler == null)
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", "/views/404");
                handler = WebPageHttpHandler.CreateFromVirtualPath("~/views/404.cshtml");
            }
            else
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", filePath.Substring(1, filePath.Length - 8));
            }

            return(handler);
        }
コード例 #15
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            // Use cases:
            //     ~/            -> ~/views/index.html
            //     ~/about       -> ~/views/about.html or ~/views/about/index.html
            //     ~/views/about -> ~/views/about.html
            //     ~/xxx         -> ~/views/404.html
            var filePath = requestContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath;

            if (filePath == "~/")
            {
                filePath = "~/views/index.html";
            }
            else
            {
                if (!filePath.StartsWith("~/views/", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath.Insert(2, "views/");
                }

                if (!filePath.EndsWith(".html", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath += ".html";
                }
            }

            var handler = WebPageHttpHandler.CreateFromVirtualPath(filePath); // returns NULL if .html file wasn't found

            if (handler == null)
            {
                // case a página requisitada não exista (partial html ou 404), retornar o index e deixar a SPA tratar roteamento
                requestContext.RouteData.DataTokens.Add("templateUrl", "/views/index");
                handler = WebPageHttpHandler.CreateFromVirtualPath("~/views/index.html");
            }
            else
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", filePath.Substring(2));
            }

            return(handler);
        }
コード例 #16
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var substitutedVirtualPath = GetSubstitutedVirtualPath(requestContext);
            int index = substitutedVirtualPath.IndexOf('?');

            if (index != -1)
            {
                substitutedVirtualPath = substitutedVirtualPath.Substring(0, index);
            }

            var handler = WebPageHttpHandler.CreateFromVirtualPath(substitutedVirtualPath);

            Trace.TraceInformation("Routing {0} = {1}", this._virtualPath, handler);

            if (handler != null)
            {
                return(handler);
            }

            return(new MvcHandler(requestContext));

            //return WebPageHttpHandler.CreateFromVirtualPath("~/db/Error.cshtml");
        }
コード例 #17
0
 public IHttpHandler GetHttpHandler(RequestContext requestContext)
 {
     return(WebPageHttpHandler.CreateFromVirtualPath("~/views/inventory/inventory.cshtml"));
 }
コード例 #18
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var filePath = requestContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath;

            if (filePath == "~/")
            {
                filePath = "~/views/SCPages/SCIndex.cshtml";
            }
            else
            {
                if (!filePath.StartsWith("~/views/SCPages/", StringComparison.OrdinalIgnoreCase))
                {
                    if (filePath.StartsWith("~/portfolio-detail/ShaligramInfotechAPI/api/PortfolioApi/", StringComparison.OrdinalIgnoreCase))
                    {
                        filePath = filePath.Replace("portfolio-detail/", "");
                    }
                    else
                    {
                        if (filePath.StartsWith("~/portfolio-detail", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/GetParticularPortfolioDetail";
                        }
                        else if (filePath.Equals("~/subscribe", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/Subscription";
                        }
                        else if (filePath.Equals("~/portfolio", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/portfolio-index";
                        }
                        else if (filePath.Equals("~/portfolio/true", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/portfolio-index";
                        }
                        else if (filePath.StartsWith("~/blog", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/BlogPost";
                        }
                        else if (filePath.Equals("~/pricing-comparison-uk", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/pricecomparisonuk";
                        }
                        else if (filePath.Equals("~/pricing-comparison-aus", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/pricecomparisonaus";
                        }
                        else if (filePath.Equals("~/pricing-comparison-us", StringComparison.OrdinalIgnoreCase))
                        {
                            filePath = "~/views/SCPages/pricecomparisonus";
                        }
                        else
                        {
                            filePath = filePath.Insert(2, "views/SCPages/");
                        }
                    }
                }

                if (!filePath.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
                {
                    filePath = filePath += ".cshtml";
                }
            }

            var handler = WebPageHttpHandler.CreateFromVirtualPath(filePath); // returns NULL if .cshtml file wasn't found

            if (handler == null)
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", "/views/404");
                handler = WebPageHttpHandler.CreateFromVirtualPath("~/views/404.cshtml");
            }
            else
            {
                requestContext.RouteData.DataTokens.Add("templateUrl", filePath.Substring(1, filePath.Length - 8));
            }

            return(handler);
        }