有没有办法可以动态注册IHttpHandlerC#代码,而不必手动将其添加到system.web/httpHandlersweb.config中的部分.
这可能听起来很疯狂,但我有充分的理由这样做.我正在构建一个WidgetLibrary,网站所有者只需将.dll文件放入他们的bin目录就可以使用,并希望以最少的配置支持web.config.
我不知道在MVC网站上发生这种情况是否相关,但我认为无论如何我都会提到它.
在我的web.config中,我有以下几行:
<add verb="*" path="*.imu" type="Website.Handlers.ImageHandler, Website, Version=1.0.0.0, Culture=neutral" />
Run Code Online (Sandbox Code Playgroud)
在网站项目中,我有一个名为Handlers的文件夹,其中包含我的ImageHandler类.它看起来像这样(我已经删除了processrequest代码)
using System;
using System.Globalization;
using System.IO;
using System.Web;
namespace Website.Handlers
{
public class ImageHandler : IHttpHandler
{
public virtual void ProcessRequest(HttpContext context)
{
//the code here never gets fired
}
public virtual bool IsReusable
{
get { return true; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我运行我的网站并转到/something.imu它只会返回404错误.
我正在使用Visual Studio 2008并尝试在ASP.Net开发服务器上运行它.
我一直在寻找几个小时,并让它在一个单独的空网站上工作.所以我不明白为什么它不能在现有的网站内工作.没有其他引用*.imu路径顺便说一句.
我在我的web.config文件中配置了一个HttpHandler,如下所示:
<add verb="GET,HEAD,POST"
path="TinyMCE.ashx"
type="Moxiecode.TinyMCE.Web.HttpHandler,Moxiecode.TinyMCE" />
Run Code Online (Sandbox Code Playgroud)
当我部署到IIS 7时,处理程序停止工作(404).
为了让这个工作,我需要做什么?
只是不明白我做错了..我一直在寻找几十个类似的问题,但仍然有误解......当我从JS调用CallHandler函数时,我总是得到"请求失败"警报.请帮我.
JS/jQuery的:
function CallHandler() {
$.ajax({
url: "DemoHandler.ashx",
contentType: "application/json; charset=utf-8",
type: 'POST',
dataType: "json",
data: [{"id": "10000", "name": "bill"},{"id": "10005", "name": "paul"}],
success: OnComplete,
error: OnFail
});
return false;
}
function OnComplete(result) {
alert(result);
}
function OnFail(result) {
alert('Request Failed');
}
Run Code Online (Sandbox Code Playgroud)
asp.net c#代码背后:
public void ProcessRequest(HttpContext context)
{
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
string jsonString = HttpContext.Current.Request.Form["json"];
List<Employee> emplList = new List<Employee>();
emplList = jsonSerializer.Deserialize<List<Employee>>(jsonString);
string resp = "";
foreach (Employee emp in emplList){
resp += emp.name + " …Run Code Online (Sandbox Code Playgroud) 我已经做了很多研究,找到了一个.NET上传组件,我可以用来上传大文件,有一个进度条,可以恢复上传大文件.我遇到过一些组件,比如AjaxUploader,SlickUpload和PowUpload,仅举几例.这些选项中的每一个都需要花钱,只有PowUpload才能进行可恢复的上传,但它使用的是java applet.我愿意支付一个可以很好地完成这些工作的组件,但如果我能自己编写那些最好的组件.
我有两个问题:
谢谢
奥斯汀
[编辑]
我意识到我确实需要能够为我的项目进行可恢复的文件上传,对组件的任何建议都能做到吗?
我有一个HttpHandler,我用它来处理客户端网站上的某些图像.当我将图像流输出到响应对象并偶尔调用Flush时会抛出错误.这是一个代码块
var image = Image.FromStream(memStream);
if (size > -1) image = ImageResize.ResizeImage(image, size, size, false);
if (height > -1) image = ImageResize.Crop(image, size, height, ImageResize.AnchorPosition.Center);
context.Response.Clear();
context.Response.ContentType = contentType;
context.Response.BufferOutput = true;
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
context.Response.Flush();
context.Response.End();
Run Code Online (Sandbox Code Playgroud)
根据我的阅读,这个异常是由于客户端在进程完成之前断开连接并且没有要刷新的.
这是我的错误页面的输出
System.Web.HttpException: An error occurred while communicating with the remote host. The error code is 0x80070057.
Generated: Mon, 12 Oct 2009 03:18:24 GMT
System.Web.HttpException: An error occurred while communicating with the remote host. The error code is 0x80070057.
at System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte[] status, Byte[] header, Int32 keepConnected, …Run Code Online (Sandbox Code Playgroud) 我正在将ASP.NET路由添加到较旧的webforms应用程序中.我正在使用自定义HttpHandler来处理所有内容.在某些情况下,我想将特定路径映射回aspx文件,因此我需要将控制权传递回asp.net的默认HttpHandler.
我得到的最接近的是这个
public void ProcessRequest(HttpContext context) {
// .. when we decide to pass it on
var handler = new System.Web.UI.Page();
handler.ProcessRequest(context);
MemoryStream steam = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
handler.RenderControl(htmlWriter);
// write headers, etc. & send stream to Response
}
Run Code Online (Sandbox Code Playgroud)
它没有做任何事情,流没有任何输出.MS的System.Web.UI.Page文档(作为IHttpHandler)说"不要调用ProcessRequest方法.它是供内部使用的".
从环顾四周看来你可以用MVC做到这一点,例如:MvcHttpHandler似乎没有实现IHttpHandler
还有一件事System.Web.UI.PageHandlerFactory似乎只会为aspx文件生成一个Page处理程序,但它是内部的,我不能直接使用它.
此页面:http://msdn.microsoft.com/en-us/library/bb398986.aspx引用"默认的asp.net处理程序",但不识别类或给出任何可能如何使用它的指示.
有关如何做到这一点的任何想法?可能吗?
我一直在使用Ashx随jQuery.我读过msdn,我在谈论IHttpHandler.IsReusable房产.
获取一个值,该值指示另一个请求是否可以使用IHttpHandler实例.
他们的意思是什么the IHttpHandler instance.?" 他们是否试图让static每个人都能看到和使用它?它是由同一个可重复使用的是什么?(QueryString,cookies等?)
如果我写这个:
public class MyHttpHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Write(DateTime.Now.Ticks.ToString());
}
public bool IsReusable
{
get { return true; }
}
}
Run Code Online (Sandbox Code Playgroud)
似乎每个请求会得到其自己跟上时代的 - Datetime值.
我正在运行下面的代码来获取POST消息.如何获取文件(使用POST发送)以及如何使用HTTP状态代码进行响应200?
#!/usr/bin/env python
import ssl
import BaseHTTPServer, SimpleHTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler
class HttpHandler(BaseHTTPRequestHandler):
def do_POST(self):
print "got POST message"
# how do I get the file here?
print self.request.FILES
httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), HttpHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile='./server.pem', server_side=True)
httpd.serve_forever()
$curl -X POST -d @../some.file https://localhost:4443/resource --insecure
Run Code Online (Sandbox Code Playgroud)
AttributeError:'SSLSocket'对象没有属性'FILES'
我正在尝试使用标准 python 日志库的 HTTPHandler 类来发送日志。我需要使用基本凭据(用户名和密码)发出 https 发布请求。这就是我如何设置 HTTPHandler-
host = 'example.com'
url = '/path'
handler = logging.handlers.HTTPHandler(host, url, method='POST', secure=True, credentials=('username','password'), context=None)
logger.addHandler(handler)
Run Code Online (Sandbox Code Playgroud)
但问题是,我的远程服务器中没有任何日志。我什至没有看到处理程序有任何异常。我是否错误地设置了处理程序参数?我可以使用简单的 pythong http 请求发送类似的日志-
url = 'https://username:password@example.com/path'
headers = {'content-type': 'application/json'}
jsonLog = { 'id': '4444','level': 'info', 'message': 'python log' };
r = requests.post(url, data = json.dumps(jsonLog), headers=headers)
Run Code Online (Sandbox Code Playgroud)
由于 json 内容类型,我是否需要以某种方式设置标题?如果是,那么我如何在 httphandler 中设置它?
更新
我想我应该更新我最终做的事情。经过多次搜索,我发现我可以通过覆盖 logging.Handler 的 emit() 来创建自定义处理程序。
class CustomHandler(logging.Handler):
def emit(self, record):
log_entry = self.format(record)
# some code....
url = 'url'
# some code....
return …Run Code Online (Sandbox Code Playgroud) httphandler ×10
asp.net ×7
c# ×5
.net ×2
python ×2
.net-4.0 ×1
ajax ×1
asp.net-mvc ×1
file-upload ×1
http ×1
httpcontext ×1
https ×1
ihttphandler ×1
iis ×1
iis-7 ×1
image ×1
jquery ×1
json ×1
large-files ×1
logging ×1
post ×1
python-3.x ×1
routing ×1
web-config ×1