ASP.NET MVC 6: view components in a separate assembly

前端 未结 3 1075
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 10:13

I\'d like to define view components (which are new in ASP.NET MVC 6) in a separate assembly from the MVC 6 web startup project so that I can reuse them in multiple web proje

3条回答
  •  时光取名叫无心
    2020-12-13 10:56

    I have done some researching on Github and found that PhysicalFileProvider (link) IFileInfo GetFileInfo(string subpath) method is used by Razor engine (link) for getting real files to compile.

    Current implementation of this method

    public IFileInfo GetFileInfo(string subpath)
    {
         if (string.IsNullOrEmpty(subpath))
         {
             return new NotFoundFileInfo(subpath);
         }
    
         // Relative paths starting with a leading slash okay
         if (subpath.StartsWith("/", StringComparison.Ordinal))
         {
             subpath = subpath.Substring(1);
         }
    
         // Absolute paths not permitted.
         if (Path.IsPathRooted(subpath))
         {
             return new NotFoundFileInfo(subpath);
         }
    
         var fullPath = GetFullPath(subpath);
         if (fullPath == null)
         {
             return new NotFoundFileInfo(subpath);
         }
    
         var fileInfo = new FileInfo(fullPath);
         if (FileSystemInfoHelper.IsHiddenFile(fileInfo))
         {
             return new NotFoundFileInfo(subpath);
         }
    
         return new PhysicalFileInfo(_filesWatcher, fileInfo);
    }
    
    private string GetFullPath(string path)
    {
        var fullPath = Path.GetFullPath(Path.Combine(Root, path));
        if (!IsUnderneathRoot(fullPath))
        {
            return null;
        }
        return fullPath;
    }
    

    We can see here that absolute paths nor permitted and the GetFullPath method combines path with Root which is your main web application root path.

    So I assume that u can't open ViewComponent from other folder than the current one.

提交回复
热议问题