【问题标题】:HttpContext.Current.Request.Url.IsFile Returns False When Url is ASPX Page当 Url 为 ASPX 页面时,HttpContext.Current.Request.Url.IsFile 返回 False
【发布时间】:2014-12-04 23:00:19
【问题描述】:

我正在点击表单的本地 URL:http://localhost/example.dev/eu/default.aspx.

我的目标是确定请求何时是 global.asax 文件中的 aspx 文件,如果它是 aspx 文件(并且仅是 aspx 文件),则使用以下方法进行处理:

HttpContext.Current.Request.Url.IsFile

它始终解析为false,但我不确定为什么。我完整的global.asax 代码是:

if(HttpContext.Current.Request.Url.IsFile)
{
    if(File.Exists(HttpContext.Current.Request.Url.LocalPath))
    {
        if(new FileInfo(HttpContext.Current.Request.Url.LocalPath).Extension.Equals("aspx"))
        {
            DoSomethingWithThePagesURL();
        }
    }
}

【问题讨论】:

标签: c# asp.net http


【解决方案1】:

你看过Documentation for IsFile Property?。从文档中可以看出Http: 不是File:

当 Scheme 属性等于 UriSchemeFile 时,IsFile 属性为真。

DotNetFiddle Example

using System;
                
public class Program
{
  public static void Main()
  {
    Uri uriAddress2 =  new Uri("file://server/filename.ext");
    Console.WriteLine(uriAddress2.LocalPath);
    Console.WriteLine("Uri {0} a UNC path", uriAddress2.IsUnc ? "is" : "is not");
    Console.WriteLine("Uri {0} a local host", uriAddress2.IsLoopback ? "is" : "is not");
    Console.WriteLine("Uri {0} a file", uriAddress2.IsFile ? "is" : "is not");
  }
}

结果:

\服务器\文件名.ext

Uri 是 UNC 路径

Uri 不是本地主机

Uri 是一个文件

【讨论】:

    【解决方案2】:

    我使用 Nuget 包 walter.web.firewall 在每个请求中注入一个 IPageRequest,这包含对请求底层资源的访问,并将通过 IPageRequest.LocalFile 提供访问

    但是,如果您确实需要防火墙,并且自从提出这个问题以来已经有一段时间了,并且自从提出这个问题以来已经发生了很多框架更改,所以让我尝试以不使用框架类的方式回答它希望它将适用于所有尝试并在未来实施它的人。

    代码如下:

    public enum FileLocation
    {
        NotSet,
        Disk,
        Resource,
    }
    
    private static readonly string[] FileExtenstions = new[] {
        ".js"
        ,".ts"
        ,".vue"
        ,".css"
        ,".jpg"
        ,".png"
        ,".gif"
        ,".ico"
        ,".svg"
        ,".ttf"
        ,".eot"
        ,".ttf"
        ,".woff"
        ,".woff2"
        ,".mp4"
        ,".mp3"
        ,".emf"
    };
    
    public FileLocation IsMappedTo(Uri uri)
    {
        if (uri is null)
        {
            throw new ArgumentNullException(nameof(uri));
        }
        //make sure we support .net default URI contract
        if (uri.IsFile)
            return FileLocation.Disk;
    
        //now assume you are looking in a web application
        var path = uri.AbsolutePath;
        if (path.Length == 0 || path.Equals("/",StringComparison.Ordinal) || path.Length< FileExtenstions.Min(s=>s.Length))
            return FileLocation.NotSet;
    
        //get the directory normally one would use IWebHostEnvironment.ContentRootPath different versions .net will have other methods
        var dir = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
    
        //get all resources names from the assembly hosting this class out side if the loop from this assembly you can also use
        //you can also use GetManifestResourceNames() to use the web application's assembly
        var resourceNames = new HashSet<string>(this.GetType().Assembly.GetManifestResourceNames());
        var entryAssembly = Assembly.GetEntryAssembly();
        if (entryAssembly != null && entryAssembly != this.GetType().Assembly)
        {
            foreach (var entry in entryAssembly.GetManifestResourceNames())
            {
                if (string.IsNullOrEmpty(entry))
                    resourceNames.Add(entry);
            }
        }
    
        for (var i = 0; i < FileExtenstions.Length; i++)
        {
            if (FileExtenstions[i].Equals(path[FileExtenstions[i].Length..], StringComparison.OrdinalIgnoreCase) || path.Contains(FileExtenstions[i], StringComparison.OrdinalIgnoreCase))
            {
                //exists on disk
                if (File.Exists(Path.Combine(dir, path.Replace("/", @"\"))))
                    return FileLocation.Disk;
    
                //has a file as an embedded resource with the same name (ignores the path) so you might have duplicates names
                if (resourceNames.Any(a => a.EndsWith(path.Split('/')[^1], StringComparison.OrdinalIgnoreCase)))
                    return FileLocation.Resource;
            }
        }
    
        return FileLocation.NotSet;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 2018-06-02
      • 2018-03-20
      • 2016-03-11
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多