C#:如何将网站项目转换为Web项目 [英] C#: How to convert a Website project to a Web Project

查看:465
本文介绍了C#:如何将网站项目转换为Web项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:所有这些问题都是在Visual Studio中将网站应用程序转换为Web项目.挖完后,找到了解决方法,如下所示,我将其标记为答案.

UPDATE: All of these problems was for converting a Website application to a Web Project in Visual Studio. Once I dug in and I found the solution, as I marked as answer below.

基本上,如果您要将网站项目升级/移动到Web项目,则会遇到许多错误.简短的方法在下面的答案中.谢谢大家的评论和帮助.

Basically, if you are wanting to upgrade/move your Website project to a Web Project, you will run into a number of errors. The short how-to is in the answer below. Thank you all for comments and help.

旧问题和标题如下:

如何将命名空间xx {}包装到所有类文件中?

How to wrap namespace xx {} to all class files?

您如何将名称空间包装到Web项目(或任何项目)中的所有代码隐藏文件中?

How would you wrap namespace to all codebehind files in a webproject (or any project)?

namespace MyDomain
{
  // regular code here
}

长话短说...

我一直在转换一个外包给海外的项目.对于这样一个简单的网站(摇头),仅在"web"项目中就有超过20,000个文件.它是使用VB.NET和C#在vs2005中编写的(是的,两者都很复杂).它最初是一个网站应用程序.我将其转换为Web应用程序,主要是因为发布时会产生8,000个临时"程序集.

I have been converting a project that was outsourced overseas. Over 20,000 files in the 'web' project alone, for such a simple website (shakes head). It was written in vs2005 using VB.NET and C# (yes, both complilations). It was originally a Website applicaiton. I have converted it to a Web Application, mainly because of the 8,000 "temp" assemblies that are produced when you publish.

好的,我要转换为VS2008.我已经将所有代码转换为C#.看起来不错.我已经将所有特殊的App_Code/VB和C#代码带到了单独的程序集中.

Ok, I am converting to VS2008. I've converted all code to C#. Looks good. I've brought over all special App_Code/VB and C# code into seperate assemblies.

现在,当我尝试再次编译时,会收到大量错误的警报.我通过将代码隐藏文件包装在命名空间中并更新用户控件以使用新的命名空间来解决这些问题.

Now when I attempt to compile again, I am alerted to a huge number of errors. I fix these by wrapping the codebehind files in a Namespace, and updating the usercontrol to use the new namespace.

但是...走了5000多个错误...帮助!

But... 5000+ errors to go... Help!

替代文本http://eduncan911.com/blog/thumbnail/4 -15-2009.6-00-13.PM.png

也许还有另一种方法?这是来自代码隐藏的示例代码片段:

Maybe there is another way? Here is a sample snippet from the codehind:

public partial class MyHomepage_MyStuff_MyPosts : SecurePage
{ ... }

请注意类名MyHomepage_MyStuff_MyPosts.这是文件位置/MyHomepage/MyStuff/MyPosts.aspx的目录路径.

Notice the class name MyHomepage_MyStuff_MyPosts. This is the directory path to the location of the file at /MyHomepage/MyStuff/MyPosts.aspx.

错误是代码隐藏文件找不到对正在使用的控件的引用.如:

The error is that the codebehind file cannot find a reference to the control being used. Such as:

ctl_website_rpt.RowCount = _totalCount;

这是ASPX文件中名为"ctl_website_rpt"的控件(其命名约定). aspx页面的标题中包含:

This is a control (their naming convention) called "ctl_website_rpt" within the ASPX file. The aspx page has in the header:

<%@ Page Title="My Posts" Language="C#" 
  MasterPageFile="~/MasterPages/Main.master" AutoEventWireup="true" 
  CodeFile="MyPosts.aspx.cs" 
  Inherits="MyHomepage_MyStuff_MyPosts" %>

也许有一种方法可以在其他地方引用此页面?无需将其更改为的名称空间?

Maybe there is a way I can reference this page elsewhere? Without changing it to the namespace of?

namespace Website.MyHomepage.MyStuff
{
  public partial class MyPosts
  { ... }
}

<%@ Page Title="My Posts" Language="C#" 
  MasterPageFile="~/MasterPages/Main.master" AutoEventWireup="true" 
  CodeFile="MyPosts.aspx.cs" 
  Inherits="Website.MyHomepage.MyStuff.MyPosts" %>

推荐答案

好,我找到了解决方案.比您想象的要容易.

Fyi, I found the solution. It's easier than you think.

您只需右键单击新的Web项目,然后有一个转换为Web应用程序"选项.这做了一些有趣的事情.首先,它创建[file] .aspx.designer.cs自动生成的文件.这消除了我99%的错误,这是因为背后的代码找不到控件的引用(请参见上面我的问题的屏幕截图).

You simply right-click your new Web Project and there is an option for "Convert to Web Application." This does some interesting things. First, it creates the [file].aspx.designer.cs auto-generated file. This got rid of 99% of my errors, as it was because the codebehind could not find a reference to the control (see screenshot in my question above).

这还将在您的aspx文件中重写<%@ Page%>脱节,以继承其背后的新名称/命名空间中的代码.

This also re-writes the <%@ Page %> decleration in your aspx file to to inherit from the code behind's new name/namespace.

剩下的就是一些程序集参考问题并完成了!

All that was left was some assembly reference issues and done!

操作方法:

1)创建一个新的Web项目,将旧网站项目中的所有文件复制到新的Web项目中.

1) Create a new Web Project, copy all files from your old Website project to the new Web Project.

2)修正对您知道将需要的程序集的所有引用.

2) Fix all references to your assemblies you KNOW you will need.

3)右键单击Web应用程序,然后选择转换为Web应用程序".仔细检查错误和警告日志.修复问题后,即可随意进行转换.

3) Right-click on the Web Application and select Convert to Web Application. Closely inspect the error and warning log. Re-convert at will once you fix things.

如果任何aspx或ascx文件都没有designer.cs文件,则该文件的转换失败.修复引用,名称空间等,然后重新转换.

If any aspx or ascx files do not have a designer.cs file, then the conversion failed on that file. Fix the references, namespace, etc and re-convert.

提示#1 :在转换为Web应用程序之前,请确保已添加所有引用.有时可能会发生许多错误.

Tip #1: Make sure you have all references added before you Convert to Web Application. Sometimes a number of errors can happen.

提示#2 :即使看起来已经完成,也要在转换后查找错误.它将指向您无法转换的文件,因此您需要对其进行处理.

Tip #2: Even though it looks like it completed, look for errors after the convert. It will point you to files it was unable to convert, so you need to work on them.

提示#3 :您可以多次转换任何子目录或文件.假设您要修复的#2中存在多个错误,则可以多次重新转换该文件或目录,直到您对其进行修复为止.重新转换不会损害现有应用程序.

Tip #3: You can re-convert any sub-directory, or any file, any number of times. Say you had multiple errors in #2 you were working to fix, you can re-convert that file or directory multiple times until you fix it. Re-converting does not hurt the existing application.

这篇关于C#:如何将网站项目转换为Web项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆