System.Web.HttpContext.RewritePath C# (CSharp) Method

RewritePath() public method

public RewritePath ( string filePath, string pathInfo, string queryString, bool setClientFilePath ) : void
filePath string
pathInfo string
queryString string
setClientFilePath bool
return void
		public void RewritePath (string filePath, string pathInfo, string queryString, bool setClientFilePath)
		{
			if (filePath == null)
				throw new ArgumentNullException ("filePath");
			if (!VirtualPathUtility.IsValidVirtualPath (filePath))
				throw new HttpException ("'" + HttpUtility.HtmlEncode (filePath) + "' is not a valid virtual path.");

			filePath = VirtualPathUtility.Canonize (filePath);
			bool pathRelative = VirtualPathUtility.IsAppRelative (filePath);
			bool pathAbsolute = pathRelative ? false : VirtualPathUtility.IsAbsolute (filePath);
			HttpRequest req = Request;
			if (req == null)
				return;
			
			if (pathRelative || pathAbsolute) {
				if (pathRelative)
					filePath = VirtualPathUtility.ToAbsolute (filePath);
			} else
				filePath = VirtualPathUtility.AppendTrailingSlash (req.BaseVirtualDir) + filePath;
			
			if (!StrUtils.StartsWith (filePath, HttpRuntime.AppDomainAppVirtualPath))
				throw new HttpException (404, "The virtual path '" + HttpUtility.HtmlEncode (filePath) + "' maps to another application.", filePath);

			req.SetCurrentExePath (filePath);
			req.SetFilePath (filePath);

			if (setClientFilePath)
				req.ClientFilePath = filePath;
			
			// A null pathInfo or queryString is ignored and previous values remain untouched
			if (pathInfo != null)
				req.SetPathInfo (pathInfo);

			if (queryString != null)
				req.QueryStringRaw = queryString;
		}

Same methods

HttpContext::RewritePath ( string path ) : void
HttpContext::RewritePath ( string path, bool rebaseClientPath ) : void
HttpContext::RewritePath ( string filePath, string pathInfo, string queryString ) : void

Usage Example

Beispiel #1
0
        /// <summary>
        /// Handles the current request.
        /// </summary>
        /// <param name="context">The current context</param>
        /// <param name="draft">Weather to view the draft</param>
        /// <param name="args">Optional url arguments passed to the handler</param>
        protected virtual void HandleRequest(HttpContext context, bool draft, params string[] args)
        {
            if (args != null && args.Length > 0) {
                Permalink perm = Permalink.GetByName(args[0]) ;

                if (perm != null) {
                    if (perm.Type == Permalink.PermalinkType.PAGE) {
                        Page page = Page.GetSingle(perm.ParentId, draft) ;

                        if (!String.IsNullOrEmpty(page.Controller)) {
                            context.RewritePath("~/templates/" + page.Controller + "/" + perm.Name +
                                (draft ? "?draft=true" : ""), false) ;
                        } else {
                            context.RewritePath("~/page/" + perm.Name + (draft ? "?draft=true" : "")) ;
                        }
                    } else {
                        context.RewritePath("~/post/" + perm.Name + (draft ? "?draft=true" : "")) ;
                    }
                }
            } else {
                //
                // Rewrite to current startpage
                //
                Page page = Page.GetStartpage() ;

                if (!String.IsNullOrEmpty(page.Controller))
                    context.RewritePath("~/templates/" + page.Controller, false) ;
                else context.RewritePath("~/page") ;
            }
        }
All Usage Examples Of System.Web.HttpContext::RewritePath