[英]how does System.Web.HttpRequest::PathInfo work?
I am trying to understand the PathInfo property of System.Web.HttpRequest and how it is set.我试图了解 System.Web.HttpRequest 的 PathInfo 属性及其设置方式。
Why would it be empty in the following example?为什么在下面的例子中它是空的?
var p = new System.Web.HttpRequest("file.txt","http://file.com/files/file.txt","");
//PathInfo is always empty
return string.IsNullOrEmpty(p.PathInfo)
I am trying to pipe the Elmah interface through Nancyfx by invoking the Elmah.ErrorLogPageFactory::ProcessRequest(HttpContext context).我试图通过调用 Elmah.ErrorLogPageFactory::ProcessRequest(HttpContext 上下文) 来通过 Nancyfx 管道 Elmah 接口。
but it does not work because Elmah.ErrorLogPageFactory depends on HttpRequest::PathInfo to resolve the correct IHttpHandler and PathInfo is always empty.但它不起作用,因为 Elmah.ErrorLogPageFactory 依赖于 HttpRequest::PathInfo 来解析正确的 IHttpHandler 并且 PathInfo 始终为空。
If someone would take the time explaining how PathInfo works I would be very grateful!如果有人愿意花时间解释 PathInfo 的工作原理,我将不胜感激!
The PathInfo
property is calculated basing on HttpContext
private variable of the HttpRequest
class. PathInfo
属性是基于HttpRequest
类的HttpContext
私有变量计算的。 There's no official way to set this HttpContext
instance.没有官方的方法来设置这个
HttpContext
实例。 That's why whenever you create the HttpRequest
manually, the HttpContext
is always null, therefore the PathInfo
use empty also.这就是为什么每当您手动创建
HttpRequest
, HttpContext
始终为空,因此PathInfo
也使用空。
To get something different from empty string you need to use a real instance, which is created by .NET framework to you, and not to instantiate it by yourself.要获得与空字符串不同的东西,您需要使用由 .NET 框架为您创建的真实实例,而不是自己实例化它。
I have just been trying to do a similar thing with Elmah.我一直在尝试对 Elmah 做类似的事情。
My route was set up as follows:我的路线设置如下:
var url = "admin/elmah.axd/{*pathInfo}";
var defaults = new RouteValueDictionary {{"pathInfo", string.Empty}};
var routeHandler = new ElmahHandler();
var route = new Route(url, defaults, routeHandler);
RouteTable.Routes.Add(route);
But I too found that the pathinfo is always empty, so the stylesheet and additional paths didn't work.但我也发现 pathinfo 总是空的,所以样式表和附加路径不起作用。 I managed to achieve my goal using reflection to call the underlying methods in the ErrorLogFactory.
我设法使用反射调用 ErrorLogFactory 中的底层方法来实现我的目标。
private object InvokeMethod(string name, params object[] args)
{
var dynMethod = typeof(ErrorLogPageFactory).GetMethod(name, BindingFlags.Static | BindingFlags.NonPublic);
return dynMethod.Invoke(null, args );
}
Then my handler looked like this然后我的处理程序看起来像这样
public class ElmahHandler : ErrorLogPageFactory, IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var handler = InvokeMethod("FindHandler", requestContext.RouteData.Values["pathInfo"]) as IHttpHandler;
if (handler == null)
throw new HttpException(404, "Resource not found.");
var num = (int)InvokeMethod("IsAuthorized", context);
if (num != 0 && (num >= 0 || HttpRequestSecurity.IsLocal(context.Request) /*|| SecurityConfiguration.Default.AllowRemoteAccess*/))
{
return handler;
}
//new ManifestResourceHandler("RemoteAccessError.htm", "text/html").ProcessRequest(context);
HttpResponse response = context.Response;
response.Status = "403 Forbidden";
response.End();
return null;
}
}
I had no need to get the ManifestResourceHandler working, so just commented it out, likewise for the allowRemoteAccess setting我不需要让 ManifestResourceHandler 工作,所以只需将其注释掉,对于 allowRemoteAccess 设置也是如此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.