我在ASP.NET 4.0中創建了兩個Web解決方案,A和B.然後我將第二方的數據輸入Web表單(DataEntery.aspx)添加到解決方案A中用戶控件和表單身份驗證。如何在不同的Web應用程序中重新使用網頁
我想重新使用網頁DataEntery.aspx進入解決方案B.什麼是最好的方法呢?
我在ASP.NET 4.0中創建了兩個Web解決方案,A和B.然後我將第二方的數據輸入Web表單(DataEntery.aspx)添加到解決方案A中用戶控件和表單身份驗證。如何在不同的Web應用程序中重新使用網頁
我想重新使用網頁DataEntery.aspx進入解決方案B.什麼是最好的方法呢?
您可以創建第三個名爲Solution C的Web應用程序,該應用程序將使用共享功能來保存用戶控件。不是添加C液的解決方案A和B.
參考例子:HOW TO: Share ASP.NET Pages and User Controls Between Applications by Using Visual Basic .NET
一個做到這一點的方法是添加你DataEntry.aspx和代碼隱藏類如鏈接到您的項目。此外,您需要添加邏輯以將DataEntry.aspx頁面從其所在的共享目錄複製到添加到項目中的目錄。您可以通過編寫命令行做到這一點在你的項目的預生成事件命令或創建CopyLinkedContentFiles.targets與以下內容的文件:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildDependsOn>
CopyLinkedContentFiles;
$(BuildDependsOn);
</BuildDependsOn>
</PropertyGroup>
<Target Name="CopyLinkedContentFiles">
<!-- Remove any old copies of the files -->
<Delete Condition=" '%(Content.Link)' != '' AND Exists('$(WebProjectOutputDir)\%(Content.Link)') "
Files="$(WebProjectOutputDir)\%(Content.Link)" />
<!-- Copy linked content files recursively to the project folder -->
<Copy Condition=" '%(Content.Link)' != '' " SourceFiles="%(Content.Identity)"
DestinationFiles="$(WebProjectOutputDir)\%(Content.Link)" />
</Target>
</Project>
然後包括這個目標到你的項目,這將參照添加的鏈接頁面。您可以通過添加以下行到您的Web應用程序的csproj文件執行:
<Import Project="$(MSBuildProjectDirectory)\[RELATIVE PATH TO FILE]\CopyLinkedContentFiles.targets" />
此行應例如在您的csproj文件中加入這一行後:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
最好的方法是創建一個有你的網頁的DLL。
您可以參考此dll以獲取您的項目中包含的代碼。
您可以使用VirtualFile
類加載嵌入在dll中的資源。下面是一個例子
public class SVirtualFile : VirtualFile
{
private string m_path;
public SVirtualFile(string virtualPath)
: base(virtualPath)
{
m_path = VirtualPathUtility.ToAppRelative(virtualPath);
}
public override System.IO.Stream Open()
{
var parts = m_path.Split('/');
var assemblyName = parts[1];
var resourceName = parts[2];
assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName);
var assembly = System.Reflection.Assembly.LoadFile(assemblyName + ".dll");
if (assembly != null)
{
return assembly.GetManifestResourceStream(resourceName);
}
return null;
}
}
你需要掛接到應用程序生命週期和註冊VirtualPathProvider
讓asp.net知道你所服務的虛擬文件。
public static class AppStart
{
public static void AppInitialize()
{
SVirtualPathProvider sampleProvider = new SVirtualPathProvider();
HostingEnvironment.RegisterVirtualPathProvider(sampleProvider);
}
}
public class SVirtualPathProvider : VirtualPathProvider
{
public SVirtualPathProvider()
{
}
private bool IsEmbeddedResourcePath(string virtualPath)
{
var checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
return checkPath.StartsWith("~/Succeed.Web/", StringComparison.InvariantCultureIgnoreCase);
}
public override bool FileExists(string virtualPath)
{
return IsEmbeddedResourcePath(virtualPath) || base.FileExists(virtualPath);
}
public override VirtualFile GetFile(string virtualPath)
{
if (IsEmbeddedResourcePath(virtualPath))
{
return new SVirtualFile(virtualPath);
}
else
{
return base.GetFile(virtualPath);
}
}
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
{
if (IsEmbeddedResourcePath(virtualPath))
{
return null;
}
else
{
return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
}
}
參見: How to use virtual path providers to dynamically load and compile content from virtual paths