问题描述
我在写一个CMS系统的中间和阅读并努力通过几个例子后,我在的执行我所需要的。
关键点是我们的网站一般是复印件和登记过程的组合。所以我现在需要使用默认的HttpHandler的ASPX渲染物理注册页面,直到我可以工作的方式来过的内容管理它们。
创建处理程序类后,我增加了以下到我的网站的web配置
<添加动词=*路径= TYPE =Web.Helpers.HttpCMSHandlerFactory,Web.Helpers/&GT* ASPX。
由于上述路径处理物理和CMS驱动页,在code,我能够看到如果页面实际存在,然后就可以呈现所需的页面的小检查。
公开的IHttpHandler GetHandler(HttpContext的背景下,串请求类型,URL字符串,字符串pathTranslated)
{
字符串页面名= Path.GetFileNameWithoutExtension(context.Request.PhysicalPath);
context.Items.Add(页面名称,页面名);
// DirectoryInfo的DI =新DirectoryInfo的(context.Request.MapPath(context.Request.ApplicationPath));
FileInfo的网络连接=新的FileInfo(context.Request.MapPath(context.Request.CurrentExecutionFilePath));
// var文件= fi.Where(X => string.Equals(x.Name,string.Concat(页面名称的.aspx),StringComparison.InvariantCultureIgnoreCase))的SingleOrDefault()。
如果(fi.Exists == FALSE)
{
//想我有这个错误的方式四周,URL应该先与渲染第二页
返回PageParser.GetCompiledPageInstance(URL,context.Server.MapPath(〜/ CMSPage.aspx),上下文);
}
其他
{
返回PageParser.GetCompiledPageInstance(context.Request.CurrentExecutionFilePath,fi.FullName,背景);
}
}
我已经是我应该用其他的东西比 PageParser.GetCompiledPageInstance
时,有一个物理页?这个问题
更新:由于上述我已经在开发和HttpHandler的图像,而这又对工作,如果图像存在使用其他数据库,从服务于同样的原则。有一点与PNG文件,但下面的过程中问题适用于所示的文件格式。
字节[]图像= NULL;
如果(File.Exists(context.Request.PhysicalPath))
{
的FileStream FS =新的FileStream(context.Request.PhysicalPath,FileMode.Open,FileAccess.Read);
BinaryReader BR =新BinaryReader(FS); 图像= br.ReadBytes((INT)fs.Length);
}
其他
{
的iKernel内核=新StandardKernel(新ServiceModule());
VAR cmsImageService = kernel.Get< IContentManagementService>();
VAR框架= FrameworkSetup.GetSetFrameworkSettings();
图像= cmsImageService.GetImage(Path.GetFileName(context.Request.PhysicalPath),framework.EventId);
} VAR contextType =图像/ JPG
VAR格式= ImageFormat.Jpeg; 开关(Path.GetExtension(context.Request.PhysicalPath).ToLower())
{
案例符.gif:
contextType =图像/ GIF;
格式= ImageFormat.Gif;
转到默认;
案.JPEG:
案.JPG:
contextType =图像/ JPEG;
格式= ImageFormat.Jpeg;
转到默认;
案巴纽:
contextType =图像/ PNG
格式= ImageFormat.Png;
转到默认;
默认:
context.Cache.Insert(context.Request.PhysicalPath,图像);
context.Response.ContentType = contextType;
context.Response.BinaryWrite(图片);
context.Response.Flush();
打破;
}
我不知道这完全回答你的问题......我还建立了一个ASP.NET的CMS被HttpHandler的驱动,这也让物理.aspx页。正如我只有少量的物理.aspx文件和位置来管理执行的最简单的方法是通过web.config中。
首先,我配置网站(笼统)用我的处理程序 - 除了登录页面(作为一个例子):
<添加动词=*路径=login.aspx的TYPE =System.Web.UI.PageHandlerFactory/>
<添加动词=*路径=Register.aspxTYPE =System.Web.UI.PageHandlerFactory/>
<添加动词=*路径=* ASPX。TYPE =Morphfolia.PublishingSystem.HttpHandlers.DefaultHandler,Morphfolia.PublishingSystem/>
您可以做的另一件事是由位置隔离
,因此该网站的这一部分,我选择使用外现成的ASP。 NET处理程序通常处理经典ASP.NET请求:
<位置路径=Morphfolia / _publishing>
<&的System.Web GT;
<&HttpHandlers的GT;
<添加动词=*路径=*。ASPXTYPE =System.Web.UI.PageHandlerFactory/>
< / HttpHandlers的>
< /system.web>
< /地点>
I am in the middle of writing a CMS system and after reading and working through a few examples, I have settled on HttpHandlerFactory to perform what I need.
the key point is our sites are generally a mix of copy and registration processes. So I currently need to use the default HttpHandler for aspx to render the physical registration pages until I can work a way to content manage them too.
after creating the handler class I added the following to my website's web config
<add verb="*" path="*.aspx" type="Web.Helpers.HttpCMSHandlerFactory, Web.Helpers"/>
As the above path handles physical and cms driven pages, with a small check in the code I am able to see if the page physically exists and can then render the desired page.
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
string pageName = Path.GetFileNameWithoutExtension(context.Request.PhysicalPath);
context.Items.Add("PageName", pageName);
//DirectoryInfo di = new DirectoryInfo(context.Request.MapPath(context.Request.ApplicationPath));
FileInfo fi = new FileInfo(context.Request.MapPath(context.Request.CurrentExecutionFilePath));
//var file = fi.Where(x => string.Equals(x.Name, string.Concat(pageName, ".aspx"), StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
if (fi.Exists == false)
{
// think I had this the wrong way around, the url should come first with the renderer page second
return PageParser.GetCompiledPageInstance(url, context.Server.MapPath("~/CMSPage.aspx"), context);
}
else
{
return PageParser.GetCompiledPageInstance(context.Request.CurrentExecutionFilePath, fi.FullName, context);
}
}
The question I have is should I be using something other than PageParser.GetCompiledPageInstance
when there is a physical page?
Update: since the above I have gone on to develop and HttpHandler for images, which again works on the same principle of if the image exists use it else serve from database. Had a bit of problem with png files but the below process works for the file formats shown.
byte[] image = null;
if (File.Exists(context.Request.PhysicalPath))
{
FileStream fs = new FileStream(context.Request.PhysicalPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
image = br.ReadBytes((int)fs.Length);
}
else
{
IKernel kernel = new StandardKernel(new ServiceModule());
var cmsImageService = kernel.Get<IContentManagementService>();
var framework = FrameworkSetup.GetSetFrameworkSettings();
image = cmsImageService.GetImage(Path.GetFileName(context.Request.PhysicalPath), framework.EventId);
}
var contextType = "image/jpg";
var format = ImageFormat.Jpeg;
switch (Path.GetExtension(context.Request.PhysicalPath).ToLower())
{
case ".gif":
contextType = "image/gif";
format = ImageFormat.Gif;
goto default;
case ".jpeg":
case ".jpg":
contextType = "image/jpeg";
format = ImageFormat.Jpeg;
goto default;
case ".png":
contextType = "image/png";
format = ImageFormat.Png;
goto default;
default:
context.Cache.Insert(context.Request.PhysicalPath, image);
context.Response.ContentType = contextType;
context.Response.BinaryWrite(image);
context.Response.Flush();
break;
}
I'm not sure if this completely answers your question... I've also built an ASP.NET CMS that was HttpHandler driven, and which also allows for physical .aspx pages. As I only had a small number of physical .aspx files and locations the easiest way to manage execution was via web.config.
Firstly, I configure the website (in general terms) to use my handler - except for the login page (as an example):
<add verb="*" path="login.aspx" type="System.Web.UI.PageHandlerFactory"/>
<add verb="*" path="Register.aspx" type="System.Web.UI.PageHandlerFactory"/>
<add verb="*" path="*.aspx" type="Morphfolia.PublishingSystem.HttpHandlers.DefaultHandler, Morphfolia.PublishingSystem"/>
The other thing you can do is isolate by location
, so for this part of the site I'm opting to use the out-of-the-box ASP.NET handler which normally processes "classic" ASP.NET requests:
<location path="Morphfolia/_publishing">
<system.web>
<httpHandlers>
<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory"/>
</httpHandlers>
</system.web>
</location>
这篇关于使用HttpHandlerFactory渲染CMS和物理页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!