将单个文件aspx转换为后面的代码
我正在VS 2008 .Net 3.5中的网站(不是Web应用程序)上工作,它使用单个文件.aspx模型,其中服务器代码包含在html的头部,而不是使用。
I'm working on a web site (not a web application) in VS 2008 .Net 3.5 and it uses the single file .aspx model where the server code is included in the head portion of the html instead of using a .aspx.cs code behind page.
我想快速将文件转换为使用代码隐藏模型,但是到目前为止,我唯一的方法是通过删除文件,创建一个新的具有相同名称的代码隐藏式aspx页面,然后将与aspx相关的代码手动复制到.aspx页面,并将服务器代码手动复制到.aspx.cs页面。
I'd like to quickly convert the files to use the code-behind model, but so far the only way I can do this is by removing the file, creating a new, code-behind aspx page of the same name, then manually copying in the aspx related code to the .aspx page and the server code to the .aspx.cs page.
有更快的方法吗?
我看过两篇文章,它们似乎都可以回答这个问题。问题,但不幸的是不要这样做:
在Visual Studio .NET中使用单文件Web表单页面和
如何将aspx或母版页面文件转换为页面并在其后进行编码?
I have seen two article that seem to answer this question, but unfortunately don't: Working with Single-File Web Forms Pages in Visual Studio .NET and How do you convert an aspx or master page file to page and code behind?
两者都提供了一种简单的解决方案,VS可以完成腿部工作,您只需将其指向文件并进行拍摄即可。无论出于何种原因,他们都无法正常工作。第一篇文章似乎指的是VS 2002,第二篇文章似乎指的是Web应用程序。
Both offer a simple solution whereby VS does the leg work, you just point it to a file and shoot. For whatever reason, they aren't working. The first article seems to refer to VS 2002 and the second seems to refer to a web application.
网站是否有希望?
另外,也许我看到的是错误的方式,单页模型是否有优势?我确实计划尽快将整个网站转换为Web应用程序,单页模型是否可以在Web应用程序中很好地工作?
Also, maybe I'm seeing this the wrong way, is there an advantage to the single page model? I do plan on converting the whole web site to a web application soon, does the single page model work well in web applications?
如果手动转换太耗时,并且自动转换无法正常工作,那么我认为您唯一的选择就是构建自己的转换器。您可以编写一个简单的控制台应用程序,该应用程序在命令行上采用目录路径并处理该目录中的每个文件。这不太难-在这里,我将帮助您入门:
If manual conversion is too time-intensive, and the automatic conversion isn't working, I think your only other option would be to build your own converter. You could write a simple console app which takes a directory path on the command line and processes every file in that directory. This isn't too hard - here, I'll get you started:
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();
}
}
}
}
}
30分钟编码,30分钟调试。有一些明显的错误-例如,如果您的代码在内部的任意位置包含关闭脚本标签,则将无法正确导出。结果不会很漂亮,但这应该可以处理90%的代码,而且您应该能够手动清除所有问题结果。在那里,有帮助吗?
30 minutes coding, 30 minutes debugging. There are some obvious bugs - like, if your code contains a closing script tag anywhere inside, then it won't get exported correctly. The results won't be pretty, but this should take care of 90% of your code, and you should be able to clean up any problem results manually. There, does that help?