Tag: ihttphandler

使用HttpHandlerFactory呈现CMS和物理页面

我正在编写CMS系统,在阅读并完成一些示例后,我已经确定了HttpHandlerFactory以执行我需要的操作。 关键点是我们的网站通常是复制和注册流程的混合。 因此,我目前需要使用aspx的默认HttpHandler来呈现物理注册页面,直到我可以使用内容管理它们的方式。 在创建处理程序类之后,我将以下内容添加到我网站的Web配置中 由于上面的路径处理物理和cms驱动的页面,通过对代码的小检查,我能够看到页面是否物理存在,然后可以呈现所需的页面。 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, […]

动态更改ASP.NET MVC路由

通常,当我查看ASP.Net MVC应用程序时,Route表会在启动时配置,并且不会在以后触及。 我有几个问题,但它们彼此密切相关: 是否可以在运行时更改路由表? 我应该/应该如何避免线程问题? 是否有更好的方法来提供动态URL? 我知道ID等可以出现在URL中,但无法看到它如何适用于我想要实现的目标。 即使我定义了默认控制器/操作路由,我怎么能避免这种情况,默认路由不适用于特定组合,例如“注释”控制器上的“发布”操作无法通过默认路由获得? 背景:评论垃圾邮件发送者通常会从网站上获取发布url,然后再也无需通过网站来进行自动垃圾邮件发送。 如果我经常将我的postURL修改为一些随机的URL,垃圾邮件发送者必须返回该站点并找到正确的postURL以尝试发送垃圾邮件。 如果该URL不断变化,我认为这可能会使垃圾邮件发送者的工作更加乏味,这通常意味着他们放弃了受影响的URL。

IHttpHandler图像类型文件C#ASP.Net所需的示例

任何人都可以提供一个很好的IHttpHnalder示例来处理图像类型。 我想调整托管在我服务器上的图像的大小

流式传输大型video文件.net

我试图从HttpHandler流式传输webforms中的大文件。 它似乎不起作用,因为它不流式传输文件。 相反,它将文件读入内存然后将其发送回客户端。 我全神贯注地寻找解决方案,解决方案告诉我他们在做同样的事情时会流式传输文件。 我的解决方案就是这样: using (Stream fileStream = File.OpenRead(path)) { context.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(360.0)); context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.AppendHeader(“Content-Type”, “video/mp4”); context.Response.AppendHeader(“content-length”, file.Length); byte[] buffer = new byte[1024]; while (true) { if (context.Response.IsClientConnected) { int bytesRead = fileStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) break; context.Response.OutputStream.Write(buffer, 0, bytesRead); context.Response.Flush(); } else { break; } } context.Response.End(); } 发生的事情是小文件,如果我调试代码,它将播放video,但直到它到达context.Respond.End()行。 但对于大文件,这将无法工作,因为它将整个文件存储在内存中会带来问题。

来自HttpHandler的图片不会在浏览器中缓存

我正在使用IHttpHandler从数据库提供图像。 相关代码在这里: public void ProcessRequest(HttpContext context) { context.Response.ContentType = “image/jpeg”; int imageID; if (int.TryParse(context.Request.QueryString[“id”], out imageID)) { var photo = new CoasterPhoto(imageID); if (photo.CoasterPhotoID == 0) context.Response.StatusCode = 404; else { byte[] imageData = GetImageData(photo); context.Response.OutputStream.Write(imageData, 0, imageData.Length); context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(5)); context.Response.Cache.SetLastModified(photo.SubmitDate); } } else context.Response.StatusCode = 404; } 问题是浏览器不会缓存图像,可能是因为我没有在响应头中指出正确的东西。 我认为HttpCachePolicy属性上的部分调用方法会强制浏览器保留图像,但事实并非如此。 我认为“正确”的事情是让处理程序返回没有图像的304状态代码,对吧? 如何使用IHttpHandler实现这一目标? 编辑: 根据最佳答案,我运行了此代码,它完全解决了问题。 是的,它需要一些重构,但它通常会certificate我所追求的是什么。 […]

未从HttpHandler写入日志文件

我想捕获所有转到我服务器上*.jpg文件的请求。 为此,我创建了一个HttpHandler,其代码如下: using System; using System.Collections.Generic; using System.Text; using System.Web; using System.IO; using System.Globalization; namespace MyHandler { public class NewHandler : IHttpHandler { public NewHandler() {} public void ProcessRequest(System.Web.HttpContext ctx) { HttpRequest req = ctx.Request; string path = req.PhysicalPath; string extension = null; string contentType = null; extension = Path.GetExtension(path).ToLower(); switch (extension) { case “.gif”: […]

文件下载后更新页面

前几天我在堆栈溢出的一些精彩帮助后整理了一个下载脚本。 但是我现在发现文件下载后我需要重新加载页面以摆脱aspx页面上的进度模板。 在我下载代码之前,删除模板的代码工作正常。 删除进度模板的代码: upFinanceMasterScreen.Update(); 我已经尝试在重定向到IHttpHandler之前和之后调用它 Response.Redirect(“Download.ashx?ReportName=” + “RequestingTPNLeagueTable.pdf”); public class Download : IHttpHandler { public void ProcessRequest(HttpContext context) { StringBuilder sbSavePath = new StringBuilder(); sbSavePath.Append(DateTime.Now.Day); sbSavePath.Append(“-“); sbSavePath.Append(DateTime.Now.Month); sbSavePath.Append(“-“); sbSavePath.Append(DateTime.Now.Year); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ContentType = “application/pdf”; HttpResponse objResponce = context.Response; String test = HttpContext.Current.Request.QueryString[“ReportName”]; HttpContext.Current.Response.AppendHeader(“content-disposition”, “attachment; filename=” + test); objResponce.WriteFile(context.Server.MapPath(@”Reports\” + sbSavePath + @”\” + test)); } […]

Uploadify ashx文件Context.Session获取null

我在我的网站上有一个文件上传,这是使用uploadify完成的,它使用ashx页面将文件上传到数据库。它在IE中工作正常,但在Mozilla中,context.Session变为null。我也使用IReadOnlySessionState来读取会话。 我怎么能像IE一样在Mozilla中获得会话。 这是我做过的ashx代码 public class Upload : IHttpHandler, IReadOnlySessionState { HttpContext context; public void ProcessRequest(HttpContext context) { string UserID = context.Request[“UserID”]; context.Response.ContentType = “text/plain”; context.Response.Expires = -1; XmlDocument xDoc = new XmlDocument(); HttpPostedFile postedFile = context.Request.Files[“Filedata”]; try { if (context.Session[“User”] == null || context.Session[“User”].ToString() == “”) { context.Response.Write(“SessionExpired”); context.Response.StatusCode = 200; } else { // […]

asp.net中的Filehandler

我需要跟踪何时在我的网络应用程序中打开pdf。 现在我正在写一个数据库,当用户点击链接,然后使用后面的代码中的window.open是不理想的,因为Safari阻止弹出窗口和其他Web浏览器在运行时发出警告所以我在想Filehandler是我需要使用的。 我以前没有使用过Filehandler,所以这有用吗? pdf不是二进制forms,它只是一个位于目录中的静态文件。