简体   繁体   English

System.Web.HttpRequest::PathInfo 如何工作?

[英]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.这就是为什么每当您手动创建HttpRequestHttpContext始终为空,因此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.

相关问题 如何从 System.Web.HttpRequest 获取协议版本? - How do I get protocol version from System.Web.HttpRequest? 从System.Web.HttpRequest获取带有方括号符号的“ RawUrl” - Getting “RawUrl” with square bracket notation from System.Web.HttpRequest 提供System.Web.HttpRequest作为ActionResult方法的参数 - Provide System.Web.HttpRequest as a parameter to an ActionResult method 有没有办法知道System.Web.HttpRequest是否是来自Global.asax的XMLHttpRequest? - Is there a way to know if a System.Web.HttpRequest is a XMLHttpRequest from Global.asax? System.Web.HttpRequest.FillInFormCollection()和System.Web.HttpRequest.GetEntireRawContent()非常慢 - System.Web.HttpRequest.FillInFormCollection() and System.Web.HttpRequest.GetEntireRawContent() very slow 为什么HttpRequest不能在Windows上运行但在Mac上运行? - Why does HttpRequest not work on Windows but works on Mac? 使HttpRequest缓存工作 - Make HttpRequest cache work 如何将HttpRequest复制到另一个Web服务? - How do I copy a HttpRequest to another web service? 网页-代码隐藏连接如何工作? - How does the web page - codebehind connection work? 从客户端(&lt;)System.Web.HttpRequest.ValidateInputIfRequiredByConfig()中检测到潜在危险的Request.Path值 - A potentially dangerous Request.Path value was detected from the client (<) System.Web.HttpRequest.ValidateInputIfRequiredByConfig()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM