Mic*_*oie 7 asp.net code-behind visual-studio-2008
我正在使用VS 2008 .Net 3.5中的网站(不是Web应用程序),它使用单个文件.aspx模型,其中服务器代码包含在html的头部而不是使用.aspx.cs代码页面背后.
我想快速转换文件以使用代码隐藏模型,但到目前为止,我能做到这一点的唯一方法是删除文件,创建一个新的,代码隐藏的同名aspx页面,然后手动复制在.aspx页面的aspx相关代码和.aspx.cs页面的服务器代码中.
有更快的方法吗?
我看过两篇似乎回答这个问题的文章,但不幸的是没有: 在Visual Studio .NET中使用单文件Web窗体页面以及 如何将aspx或母版页文件转换为页面和代码?
两者都提供了一个简单的解决方案,VS使腿部工作,你只需将它指向文件并拍摄.无论出于何种原因,他们都没有工作.第一篇文章似乎是指VS 2002,第二篇似乎是指Web应用程序.
网站有什么希望吗?
另外,也许我看错的方式,单页模型有优势吗?我计划很快将整个网站转换为Web应用程序,单页模型在Web应用程序中是否运行良好?
The*_*beg 19
如果手动转换过于耗时,并且自动转换不起作用,我认为您唯一的另一种选择是构建自己的转换器.您可以编写一个简单的控制台应用程序,它在命令行上获取目录路径并处理该目录中的每个文件.这不是太难 - 在这里,我会让你开始:
using System;
using System.IO;
class Program
{
const string ScriptStartTag = "<script language=\"CS\" runat=\"server\">";
const string ScriptEndTag = "</script>";
static void Main(string[] args)
{
DirectoryInfo inPath = new DirectoryInfo(args[0]);
DirectoryInfo outPath = new DirectoryInfo(args[0] + "\\codebehind");
if (!outPath.Exists) inPath.CreateSubdirectory("codebehind");
foreach (FileInfo f in inPath.GetFiles())
{
if (f.FullName.EndsWith(".aspx"))
{
// READ SOURCE FILE
string fileContents;
using (TextReader tr = new StreamReader(f.FullName))
{
fileContents = tr.ReadToEnd();
}
int scriptStart = fileContents.IndexOf(ScriptStartTag);
int scriptEnd = fileContents.IndexOf(ScriptEndTag, scriptStart);
string className = f.FullName.Remove(f.FullName.Length-5).Replace("\\", "_").Replace(":", "_");
// GENERATE NEW SCRIPT FILE
string scriptContents = fileContents.Substring(
scriptStart + ScriptStartTag.Length,
scriptEnd-(scriptStart + ScriptStartTag.Length)-1);
scriptContents =
"using System;\n\n" +
"public partial class " + className + " : System.Web.UI.Page\n" +
"{\n" +
" " + scriptContents.Trim() +
"\n}";
using (TextWriter tw = new StreamWriter(outPath.FullName + "\\" + f.Name + ".cs"))
{
tw.Write(scriptContents);
tw.Flush();
}
// GENERATE NEW MARKUP FILE
fileContents = fileContents.Remove(
scriptStart,
scriptEnd - scriptStart + ScriptEndTag.Length);
int pageTagEnd = fileContents.IndexOf("%>");
fileContents = fileContents.Insert(PageTagEnd,
"AutoEventWireup=\"true\" CodeBehind=\"" + f.Name + ".cs\" Inherits=\"" + className + "\" ");
using (TextWriter tw = new StreamWriter(outPath.FullName + "\\" + f.Name))
{
tw.Write(fileContents);
tw.Flush();
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
30分钟编码,30分钟调试.有一些明显的错误 - 比如,如果你的代码在里面的任何地方包含一个结束脚本标记,那么它将无法正确导出.结果不会很好,但这应该照顾90%的代码,并且您应该能够手动清理任何问题结果.那有帮助吗?
| 归档时间: |
|
| 查看次数: |
9769 次 |
| 最近记录: |