Jam*_*rgy 11 asp.net routing httphandler httpcontext
我正在将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处理程序",但不识别类或给出任何可能如何使用它的指示.
有关如何做到这一点的任何想法?可能吗?
坚持不懈!这实际上是有效的,因为这些信息似乎几乎无处可用,我以为我会回答我自己的问题.感谢Robert关于使用内部构造函数实例化的这篇文章,这是关键.
http://www.rvenables.com/2009/08/instantiating-classes-with-internal-constructors/
public void ProcessRequest(HttpContext context) {
// the internal constructor doesn't do anything but prevent you from instantiating
// the factory, so we can skip it.
PageHandlerFactory factory =
(PageHandlerFactory)System.Runtime.Serialization.FormatterServices
.GetUninitializedObject(typeof(System.Web.UI.PageHandlerFactory));
string newTarget = "default.aspx";
string newQueryString = // whatever you want
string oldQueryString = context.Request.QueryString.ToString();
string queryString = newQueryString + oldQueryString!="" ?
"&" + newQueryString :
"";
// the 3rd parameter must be just the file name.
// the 4th parameter should be the physical path to the file, though it also
// works fine if you pass an empty string - perhaps that's only to override
// the usual presentation based on the path?
var handler = factory.GetHandler(context, "GET",newTarget,
context.Request.MapPath(context,newTarget));
// Update the context object as it should appear to your page/app, and
// assign your new handler.
context.RewritePath(newTarget , "", queryString);
context.Handler = handler;
// .. and done
handler.ProcessRequest(context);
}
Run Code Online (Sandbox Code Playgroud)
...就像一些小奇迹一样,aspx页面完全处理并呈现,无需重定向.
我希望这只适用于IIS7.
| 归档时间: |
|
| 查看次数: |
6343 次 |
| 最近记录: |