有没有办法可以动态注册IHttpHandlerC#代码,而不必手动将其添加到system.web/httpHandlersweb.config中的部分.
这可能听起来很疯狂,但我有充分的理由这样做.我正在构建一个WidgetLibrary,网站所有者只需将.dll文件放入他们的bin目录就可以使用,并希望以最少的配置支持web.config.
我从来没有完全理解IHttpHandler的这个属性.它是您在实现接口时必须设置的属性.我认为将其设置为true会更好地表现,但我不确定负面影响可能是什么.我应该回复真假吗?
通常,当我查看ASP.Net MVC应用程序时,Route表会在启动时配置,并且不会在之后触及.
我有几个问题,但它们彼此密切相关:
背景:评论垃圾邮件发送者通常会从网站上获取发布网址,然后再也无需通过网站来进行自动垃圾邮件发送.如果我经常将我的帖子URL修改为一些随机的URL,垃圾邮件发送者必须返回该站点并找到正确的帖子URL以尝试发送垃圾邮件.如果该URL不断变化,我认为这可能会使垃圾邮件发送者的工作更加繁琐,这通常意味着他们放弃了受影响的URL.
我注意到.NET IHttpAsyncHandler(以及IHttpHandler,在较小程度上)在受到并发Web请求时泄漏内存.
在我的测试中,Visual Studio Web服务器(Cassini)从6MB内存跳到100MB以上,一旦测试完成,它们都没有被回收.
问题可以很容易地重现.使用两个项目创建一个新的解决方案(LeakyHandler):
在LeakyHandler.WebApp中:
在LeakyHandler.ConsoleApp中:
随着HttpWebRequests(sampleSize)的数量增加,内存泄漏变得越来越明显.
LeakyHandler.WebApp> TestHandler.cs
namespace LeakyHandler.WebApp
{
public class TestHandler : IHttpAsyncHandler
{
#region IHttpAsyncHandler Members
private ProcessRequestDelegate Delegate { get; set; }
public delegate void ProcessRequestDelegate(HttpContext context);
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
Delegate = ProcessRequest;
return Delegate.BeginInvoke(context, cb, extraData);
}
public void EndProcessRequest(IAsyncResult result)
{
Delegate.EndInvoke(result);
}
#endregion
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
} …Run Code Online (Sandbox Code Playgroud) 前几天我在堆栈溢出的一些精彩帮助之后整理了一个下载脚本.但是我现在发现在下载文件之后我需要重新加载页面以摆脱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));
}
public bool IsReusable { get { return true; } }
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助,您可以提供!
我有一个自定义的IHttpHandler调用MvcHttpHandler,如本答案中所述.
它在asp.net MVC2中运行良好,但在我使用IISExpress 7.5将代码迁移到MVC4后,我开始在行上获得InvalidOperationException:
httpHandler.ProcessRequest(HttpContext.Current);
Run Code Online (Sandbox Code Playgroud)
有消息:
'HttpContext.SetSessionStateBehavior'只能在引发'HttpApplication.AcquireRequestState'事件之前调用.
ASP.NET Development Server不会出现任何问题.
有谁知道这里发生了什么,以及如何解决它?
我陷入了异步死锁,我无法找出正确的语法来解决它.我已经看了几个不同的解决方案,但似乎无法弄清楚导致问题的原因.
我使用Parse作为后端并尝试使用处理程序写入表.我的处理程序看起来像:
public class VisitorSignupHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//Get the user's name and email address
var UserFullName = context.Request.QueryString["name"].UrlDecode();
var UserEmailAddress = context.Request.QueryString["email"].UrlDecode();
//Save the user's information
var TaskToken = UserSignup.SaveUserSignup(UserFullName, UserEmailAddress);
TaskToken.Wait();
....
}
public bool IsReusable { get { return false; } }
}
Run Code Online (Sandbox Code Playgroud)
然后它调用我的中间层:
public static class UserSignup
{
public static async Task SaveUserSignup(string fullName, string emailAddress)
{
//Initialize the Parse client with the Application ID and the Windows key …Run Code Online (Sandbox Code Playgroud) 我试图从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();
}
Run Code Online (Sandbox Code Playgroud)
发生的事情是小文件,如果我调试代码,它将播放视频,但直到它到达context.Respond.End()行.但是对于大文件,这不起作用,因为它将整个文件存储在内存中会带来问题.
使用库AttributeRouting,我能够配置属性路由以使用自定义路由处理程序(继承MvcRouteHandler):
routes.MapAttributeRoutes(cfg =>
{
cfg.UseRouteHandler(() => new MultiCultureMvcRouteHandler());
}
);
Run Code Online (Sandbox Code Playgroud)
此外,在 MVC5 之前,可以更改任何现有路由的路由处理程序:
(routes["myroute"] as Route).RouteHandler = new MyCustomRouteHandler();
Run Code Online (Sandbox Code Playgroud)
对于使用属性路由的 MVC5,路由集合包含内部类(RouteCollectionRoute例如)并且似乎不可能更改路由的RouteHandler属性。
如何更改在 MVC5.1 中使用属性路由时使用的默认路由处理程序?
asp.net-mvc mvcroutehandler ihttphandler attributerouting asp.net-mvc-5.1
我有一个自动完成文本框,通过用C#编写的IIS7请求IHttphandler.
但是到达网络服务器的请求似乎无法解决.
这是我输入'guidolin'后从IHttpHandler获取的日志示例
406302 2010-11-24 12:33:58,448 [8256] DEBUG Services.jQueryHandler - VALID jQueryHandler request data:guidoli RequestTime:24/11/2010 12:33:58(396)
406418 2010-11-24 12:33:58,564 [8256] DEBUG Services.jQueryHandler - VALID jQueryHandler request data:guidolin RequestTime:24/11/2010 12:33:58(507)
407751 2010-11-24 12:33:59,897 [8256] DEBUG Services.jQueryHandler - VALID jQueryHandler request data:gu RequestTime:24/11/2010 12:33:58(685)
408008 2010-11-24 12:34:00,154 [8256] DEBUG Services.jQueryHandler - VALID jQueryHandler request data:guid RequestTime:24/11/2010 12:34:00(56)
408167 2010-11-24 12:34:00,313 [8000] DEBUG Services.jQueryHandler - VALID jQueryHandler request data:guido RequestTime:24/11/2010 12:34:00(244)
408562 2010-11-24 12:34:00,708 [5912] DEBUG Services.jQueryHandler - VALID jQueryHandler request data:gui RequestTime:24/11/2010 …Run Code Online (Sandbox Code Playgroud) ihttphandler ×10
c# ×5
asp.net ×4
.net ×3
asp.net-mvc ×2
.net-4.0 ×1
async-await ×1
httphandler ×1
jquery ×1
jquery-ui ×1
memory-leaks ×1
url-routing ×1
web-config ×1
webforms ×1