Can I use HttpHandler to fake the existence of aspx pages?

I am building a website with ASP.NET 3.5 and most of the site structure is static enough to create aspx folder and page structure. However, site administrators want to add new pages to different sections of the site through the web interface and use the WYSIWYG editor. I use nested master pages to give different sections of the site my own menus. What I would like to do is create a shared page under each section of the site that uses the corresponding main page, and has room for content that can be downloaded from the database. I would also like these “fake” pages to have a URL, for example, any other aspx page, as if they had the corresponding files on the server. So instead of my url:

http://mysite.com/subsection/gerenicconent.aspx?contentid=1234

it will be something like:

http://mysite.com/subsection/somethingmeaningful.aspx

The problem is that somethingmeaningful.aspx does not exist, because the administrator created it through the web interface, and the contents are stored in the database. I think I am implementing an HTTP handler that handles requests for aspx files. In this handler, I will check if the requested URL is an actual file or one of my "fake pages". If this is a request for a fake page, I redirect the request to the general content page for the corresponding section, change the request line to request the corresponding data from the database and rewrite the URL so that it looks like the user has a fake page. The problem I'm currently facing is that I cannot figure out how to redirect the request to the default handler for aspx pages.I tried to create an instance of PageHandlerFactory, but constuctor is protected internally. Is there any way for me to tell my HttpHandler about a call to HttpHandler that will normally be used to process the request? Currently, my handler code is as follows:

using System.Web;
using System.Web.UI;

namespace HandlerTest
{
    public class FakePageHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if(RequestIsForFakedPage(context))
            {
                // reroute the request to the generic page and rewrite the URL
                PageHandlerFactory factory = new PageHandlerFactory(); // this won't compile because the constructor is protected internal
                factory.GetHandler(context, context.Request.RequestType, GetGenericContentPath(context), GetPhysicalApplicationPath(context)).ProcessRequest(context);
            }
            else
            {
                // route the request to the default handler for aspx pages
                PageHandlerFactory factory = new PageHandlerFactory();
                factory.GetHandler(context, context.Request.RequestType, context.Request.Path, context.Request.PhysicalPath).ProcessRequest(context);
            }
        }

        public string RequestForPageIsFaked(HttpContext context)
        {
            // TODO
        }

        public string GetGenericContentPath(HttpContext context)
        {
            // TODO
        }

        public string GetPhysicalApplicationPath(HttpContext context)
        {
            // TODO
        }
    }
}

, , , - URL-, - ? PageHandlerFactory, ? "" HttpHandler aspx? : " ASPX, ."

+3
3

http- , RewritePath , .

, IIS 7.0, .

+2

I just removed this similar system that we just recorded.

This method takes care of physical pages and "fake" pages. I am sure that you can determine how this fits your fake page layout.

public class AspxHttpHandler : IHttpHandlerFactory
{
        #region ~ from IHttpHandlerFactory ~

        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
                    string url=context.Request.Url.AbsolutePath;
            string[] portions = url.Split(new char[] { '/', '\\' });
                    // gives you the path, i presume this will help you identify the section and page
                    string serverSidePage=Path.Combine(context.Server.MapPath("~"),url);
                    if (File.Exists(serverSidePage))
                    {
                             // page is real
                            string virtualPath = context.Request.Url.AbsolutePath;
                string inputFile = context.Server.MapPath(virtualPath);

                try
                {
                                    // if it real, send in the details to the ASPX compiler
                    return PageParser.GetCompiledPageInstance(virtualPath, inputFile, context);
                }
                catch (Exception ex)
                {
                        throw new ApplicationException("Failed to render physical page", ex);
                }
                      }
                      else
                      {
                            // page is fake
                            // need to identify a page that exists which you can use to compile against
                            // here, it is CMSTaregtPage - it can use a Master
                            string inputFile = context.Server.MapPath("~/CMSTargetPage.aspx");
                string virtualPath = "~/CMSTargetPage.aspx";
                            // you can also add things that the page can access vai the Context.Items collection
                            context.Items.Add("DataItem","123");
                            return PageParser.GetCompiledPageInstance(virtualPath, inputFile, context);
}

public void ReleaseHandler(IHttpHandler handler)
{

}
+1
source

Source: https://habr.com/ru/post/1705183/


All Articles