C# .aspx 和 .aspx.cs 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13182757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between .aspx and .aspx.cs?
提问by Kalina
I'm not new to programming but am new to Visual Studio, MVC, C#, ASP.NET, and EXT.NET, i.e. all of the tools I am trying to use.
我对编程并不陌生,但对 Visual Studio、MVC、C#、ASP.NET 和 EXT.NET 并不陌生,即我尝试使用的所有工具。
I am trying to create an Ext.Net 2.0 MVC4 project and was given a similar (in functionality) non-MVC project for reference.
我正在尝试创建一个 Ext.Net 2.0 MVC4 项目,并获得了一个类似的(在功能上)非 MVC 项目以供参考。
I see that this non-MVC project has .aspx and .aspx.cs files. It seems like the .aspx file basically maps to the "View" in MVC that I want to make... And the .aspx.cs file has the functions that relate to the .aspx file - so is that like the "Controller"? Though the .aspx file also has some functions that seem to not be entirely view-related...
我看到这个非 MVC 项目有 .aspx 和 .aspx.cs 文件。似乎 .aspx 文件基本上映射到我想要制作的 MVC 中的“视图”......而 .aspx.cs 文件具有与 .aspx 文件相关的功能 - 所以就像“控制器” ? 虽然 .aspx 文件也有一些似乎不完全与视图相关的功能......
Could someone give me a quick overview or a place to start with this?
有人可以给我一个快速概述或一个开始的地方吗?
采纳答案by Shyju
ASPX files usually will have the UI and will which is usually HTML tags, some ASP.NET server control embed code (which ultimately produce some HTML markups). ASPX.CS file (usually called the codebehind) will have server-side coding in C#.
ASPX 文件通常具有 UI,并且通常是 HTML 标记、一些 ASP.NET 服务器控件嵌入代码(最终生成一些 HTML 标记)。ASPX.CS 文件(通常称为代码隐藏)将使用 C# 进行服务器端编码。
If needed, I would relate ASPX page to View and ASPX.CS to Controller action methods.
如果需要,我会将 ASPX 页面与 View 和 ASPX.CS 与 Controller 操作方法相关联。
You have to remember that in webforms, there are ASP.NET controls we will be using in the ASPX file to render some HTML. Examples are TextBox, DataGrid, etc. In MVC, there is nothing called Server control. The View will be pure, handwritten HTML.
您必须记住,在 webforms 中,我们将在 ASPX 文件中使用 ASP.NET 控件来呈现一些 HTML。示例有 TextBox、DataGrid 等。在 MVC 中,没有所谓的服务器控件。视图将是纯手写的 HTML。
If needed, you can create a Hybrid project which is a combination of MVC and webforms. Scott has a post explaining about it here.
如果需要,您可以创建一个混合项目,它是 MVC 和 webforms 的组合。斯科特有一个文章,解释一下这里。
No ViewState in MVC :)
MVC 中没有 ViewState :)
When switching from ASP.NET Webforms to MVC, One important thing you have to understand is that MVC architecture tries to stick with the truth that HTTP is stateless. There is no viewstateavailable in MVC. So you need to make sure that you are repopulating the data in every HTTP Request, as needed. Folks usually run into problems in loading DropDownlist in MVC. There are a lot of answershere in SO about how to handle dropdown lists on postback (when form is posted).
当从 ASP.NET Webforms 切换到 MVC 时,您必须了解的一件重要事情是 MVC 架构试图坚持HTTP 是无状态的事实。MVC 中没有可用的视图状态。因此,您需要确保根据需要重新填充每个 HTTP 请求中的数据。人们通常会在 MVC 中加载 DropDownlist 时遇到问题。这里有很多关于如何处理回发下拉列表的答案(当表单发布时)。
I suggest that you look into some beginner-level tutorialson ASP.NET MVC and start building your app step-by-step, and if you run into any issues, post a (new) question with relevant details.
我建议您查看一些有关 ASP.NET MVC 的初级教程并开始逐步构建您的应用程序,如果您遇到任何问题,请发布一个(新)问题并提供相关详细信息。
Good luck, and welcome to the wonderful world of MVC. :)
祝你好运,欢迎来到 MVC 的精彩世界。:)
回答by Justin Niessner
It sounds like you haven't created an MVC project, but rather a WebForms project.
听起来您还没有创建 MVC 项目,而是创建了一个 WebForms 项目。
The *.aspx files are the markup and the *.aspx.cs files are the code-behind files. Code-behind files handle the .NET code for any server-side controls in the *.aspx files.
*.aspx 文件是标记,*.aspx.cs 文件是代码隐藏文件。代码隐藏文件处理 *.aspx 文件中任何服务器端控件的 .NET 代码。
回答by Antarr Byrd
.aspx is your markup file. Contains things such as HTML, CSS, JavaScript, and ASP markup. this .cs file is referred to as a codebehind file. This is where you do thing that may not be available or u are not comfortable doing in scripting languages. Generally aspx is run on the client side while the code behind is executed on the server.
.aspx 是您的标记文件。包含 HTML、CSS、JavaScript 和 ASP 标记等内容。此 .cs 文件称为代码隐藏文件。这是你做一些可能不可用或者你不习惯用脚本语言做的事情的地方。通常 aspx 在客户端运行,而后面的代码在服务器上执行。
回答by Kundan Singh Chouhan
The aspx file contains your page markup. It's automatically converted into code by ASP.NET.
aspx 文件包含您的页面标记。它由 ASP.NET 自动转换为代码。
The cs file contains the code behind your page (initialization, event handlers, etc.). You have to write that code yourself.
cs 文件包含页面背后的代码(初始化、事件处理程序等)。您必须自己编写该代码。
These two files are related with the inheritance and he Inherits attribute of the @Page directive associates the page markup to the code behind
这两个文件与继承有关,@Page 指令的 Inherits 属性将页面标记与后面的代码相关联
回答by Ben Hoffman
Checkout Wikipedia's document on ASP.NET, http://en.wikipedia.org/wiki/ASP.NET.
在 ASP.NET 上查看维基百科的文档,http://en.wikipedia.org/wiki/ASP.NET。
It states:
它指出:
Web forms are contained in files with a ".aspx" extension; these files typically contain static (X)HTML markup, as well as markup defining server-side Web Controls and User Controls where the developers place all the rc content for the Web page. Additionally, dynamic code which runs on the server can be placed in a page within a block <% -- dynamic code -- %>, which is similar to other Web development technologies such as PHP, JSP, and ASP. With ASP.NET Framework 2.0, Microsoft introduced a new code-behind model which allows static text to remain on the .aspx page, while dynamic code remains in an .aspx.vb or .aspx.cs or .aspx.fs file (depending on the programming language used).
Web 表单包含在扩展名为“.aspx”的文件中;这些文件通常包含静态 (X)HTML 标记,以及定义服务器端 Web 控件和用户控件的标记,开发人员在其中放置 Web 页面的所有 rc 内容。另外,在服务器上运行的动态代码可以放在一个页面中<%--动态代码--%>块内,这类似于其他Web开发技术,如PHP、JSP和ASP。在 ASP.NET Framework 2.0 中,Microsoft 引入了一种新的代码隐藏模型,该模型允许静态文本保留在 .aspx 页面上,而动态代码保留在 .aspx.vb 或 .aspx.cs 或 .aspx.fs 文件中(取决于使用的编程语言)。
The .cs file names .aspx.cs is the code behind that goes with .aspx, which generally holds the html, css, javascript and other client side controls.
.cs 文件名 .aspx.cs 是与 .aspx 一起使用的代码,它通常包含 html、css、javascript 和其他客户端控件。
Generally, dynamic code (C# in this case because of the .cs on the file name) goes in the .cs file as a "good practice" to keep dynamic code and static html separated. Another reason for this abstraction is that the .aspx.cs code is run server side, while the .aspx file is compiled on the server and is then served to the web client requesting it.
通常,动态代码(在本例中为 C#,因为文件名中带有 .cs)作为“良好做法”放入 .cs 文件中,以保持动态代码和静态 html 分开。这种抽象的另一个原因是 .aspx.cs 代码在服务器端运行,而 .aspx 文件在服务器上编译,然后提供给请求它的 Web 客户端。
Additionally, for MVC, I would suggest using a different view model, specifically Razor, which uses .cshtml files instead of the .aspx.cs and .aspx because they are easier to follow. The reason for the change in MVC is that MVC uses the MVC pattern to abstract layers of code so that .aspx and .aspxcs are not as needed. From a personal experience, I have used both Razor and Webforms (.aspx/.aspx.cs) view models with MVC and I find Razor to be much easier to code/maintain and use.
此外,对于 MVC,我建议使用不同的视图模型,特别是 Razor,它使用 .cshtml 文件而不是 .aspx.cs 和 .aspx,因为它们更容易理解。MVC变化的原因是MVC使用MVC模式来抽象代码层,这样.aspx和.aspxcs就不需要了。根据个人经验,我在 MVC 中使用了 Razor 和 Webforms (.aspx/.aspx.cs) 视图模型,我发现 Razor 更易于编码/维护和使用。

