我可以得到“WAR文件”使用ASP.NET类型部署? [英] Can I get "WAR file" type deployment with ASP.NET?
问题描述
这不是ASP.NET的主流方法。人们做什么他们是否诉诸复制目录和所有无数文件?这对ASP.NET开发者来说这不是问题吗?
(这是一种欺骗,因为我要提出自己的答案)
尽管在ASP.NET中并不是主流方法,但是对于ASP.NET来说,这是非常有可能的,它使用一个称为$ code> VirtualPathProvider 的结构。使用它,您可以从不是文件系统的东西提供网站内容。例如,您可以直接从ZIP文件中提供ASP.NET网站,而无需首先将文件解压缩到磁盘。
这是一个下载,演示或说明了这个概念,使用DotNetZip库来协助ASP.NET从zip中拉出内容。
有趣的代码位:
使用Ionic.Zip;
命名空间Ionic.Zip.Web.VirtualPathProvider
{
public class ZipFileVirtualPathProvider:System.Web.Hosting.VirtualPathProvider
{
ZipFile _zipFile;
public ZipFileVirtualPathProvider(string zipFilename)
:base(){
_zipFile = ZipFile.Read(zipFilename);
}
〜ZipFileVirtualPathProvider(){
_zipFile.Dispose();
}
public override bool FileExists(string virtualPath)
{
string zipPath = Util.ConvertVirtualPathToZipPath(virtualPath,true);
ZipEntry zipEntry = _zipFile [zipPath];
if(zipEntry!= null)
{
return!zipEntry.IsDirectory;
}
else
{
//这里您可能希望返回Previous.FileExists(virtualPath)而不是false
//如果要给以前注册提供一个进程来提供文件
return false;
}
}
public override bool DirectoryExists(string virtualDir)
{
string zipPath = Util.ConvertVirtualPathToZipPath(virtualDir,false);
ZipEntry zipEntry = _zipFile [zipPath];
if(zipEntry!= null)
{
return zipEntry.IsDirectory;
}
else
{
//您可能希望返回Previous.DirectoryExists(virtualDir)而不是false
//如果要给予先前注册的提供者处理目录的机会
return false;
}
}
public override VirtualFile GetFile(string virtualPath){
return new ZipVirtualFile(virtualPath,_zipFile);
}
public override VirtualDirectory GetDirectory(string virtualDir)
{
返回新的ZipVirtualDirectory(virtualDir,_zipFile);
}
public override string GetFileHash(string virtualPath,System.Collections.IEnumerable virtualPathDependencies)
{
return null;
}
public override System.Web.Caching.CacheDependency GetCacheDependency(String virtualPath,System.Collections.IEnumerable virtualPathDependencies,DateTime utcStart)
{
return null;
}
}
}
VPP结构与ASP .NET 2.0或更高版本,可与任何网站一起使用。您当然可以将想法从数据库,CMS或...中获取源内容?
Sometimes J2EE-savvy people look at ASP.NET and wonder, where's the support for deploying an app as a single unit? JSP/Servlet apps can be deployed as WAR files, with all pages, content, metadata, and code in that single archive. The war file can be versioned, easily moved around. There's an assurance that the entire app is contained in a single unit.
That's not a mainstream approach for ASP.NET. What do people do? Do they resort to copying directories and all the myriad files? Is this just not a problem for ASP.NET developers?
(this is sort of a cheat, because I'm going to suggest my own answer)
Although not a mainstream approach in ASP.NET, this is very possible, using a construct called the VirtualPathProvider
for ASP.NET. With it, you can serve website content out of things that are not the filesystem. For one example, you could serve an ASP.NET website directly out of a ZIP file, without unzipping the files to disk, first.
Here's a download that demonstrates or illustrates the concept, using the DotNetZip library to assist ASP.NET in pulling content out of the zip.
The interesting code bits:
using Ionic.Zip;
namespace Ionic.Zip.Web.VirtualPathProvider
{
public class ZipFileVirtualPathProvider : System.Web.Hosting.VirtualPathProvider
{
ZipFile _zipFile;
public ZipFileVirtualPathProvider (string zipFilename)
: base () {
_zipFile = ZipFile.Read(zipFilename);
}
~ZipFileVirtualPathProvider () {
_zipFile.Dispose ();
}
public override bool FileExists (string virtualPath)
{
string zipPath = Util.ConvertVirtualPathToZipPath (virtualPath, true);
ZipEntry zipEntry = _zipFile[zipPath];
if (zipEntry != null)
{
return !zipEntry.IsDirectory;
}
else
{
// Here you may want to return Previous.FileExists(virtualPath) instead of false
// if you want to give the previously registered provider a process to serve the file
return false;
}
}
public override bool DirectoryExists (string virtualDir)
{
string zipPath = Util.ConvertVirtualPathToZipPath (virtualDir, false);
ZipEntry zipEntry = _zipFile[zipPath];
if (zipEntry != null)
{
return zipEntry.IsDirectory;
}
else
{
// Here you may want to return Previous.DirectoryExists(virtualDir) instead of false
// if you want to give the previously registered provider a chance to process the directory
return false;
}
}
public override VirtualFile GetFile (string virtualPath) {
return new ZipVirtualFile (virtualPath, _zipFile);
}
public override VirtualDirectory GetDirectory (string virtualDir)
{
return new ZipVirtualDirectory (virtualDir, _zipFile);
}
public override string GetFileHash(string virtualPath, System.Collections.IEnumerable virtualPathDependencies)
{
return null;
}
public override System.Web.Caching.CacheDependency GetCacheDependency(String virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
{
return null;
}
}
}
The VPP construct works with ASP.NET 2.0 or later, works with any website. You can of course adapt the idea to source content from a database, a CMS, or ... ??
这篇关于我可以得到“WAR文件”使用ASP.NET类型部署?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!