如何ASPX被编译? [英] How aspx is being compiled?

查看:208
本文介绍了如何ASPX被编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何的.aspx和aspx.cs文件被编译?会是两个部件或这两个文件合并之一,并将作为一个组合件产生的呢?它是否遵循任何等级/继承?的.aspx被创建为相应的C#code文件(只有这样,我们可以正确的创建装配?纠正我,如果我错了!)为一体,在windows窗体designer.cs文件?

How .aspx and aspx.cs file is being compiled? will it be two assemblies or both these files combined one and will be created as one assembly? Does any hierarchy/inheritance it follows? .aspx is being created as an corresponding C# code file (only then we could create an assembly right? correct me if i am wrong!) as one in windows form designer.cs file?

推荐答案

这取决于项目类型。随着ASP .net中,您可以创建两个样的项目: Web应用程序项目和网站项目。 。

It depends on the project type. With ASP .Net you can create two kind of projects: Web Application Project and Web Site Project..

被如何的.aspx和aspx.cs文件编译的?

使用 Web应用程序项目您编译 ASPX 文件,用户控制和其他后面的code code在这个项目中发现到一个单一的DLL和服务器上部署它。

With Web Application project you compile the code behind of the aspx files, user controls and other code found in this project into one single dll and deploy it on server.

使用网​​站项目您只需复制源$ C ​​$ c中的服务器和的ASP.NET 将办理编译为您服务。网站项目持有APP_ code文件夹中的自定义类的源$ C ​​$ C(你必须阅读更多有关这些的链接)

With Web Site Project you simply copy the source code on the server and ASP .Net will handle the compilation for you. The Web Site Project holds the source code of custom classes in App_Code folder (you'll have to read more about these on that link)

会不会是两个部件或这两个文件合并之一,并将作为一个组合件创建?

在所有这些情况下,code在 ASPX发现,ASCX 文件不是由您编译(使用Visual Studio等)。 的ASP.NET 分析这些文件,并创建存储在其临时文件夹中的DLL。在ASPX,ASCX动态链接库(它可以比单多了一个虽然)是不同的文件比您在Visual Studio中创建的(我认为这是比APP_ code文件夹中创建一个不同的,因为这code不能访问的网页中找到:code)。

In none of these cases, the code found in the aspx, ascx files is not compiled by you (using Visual Studio, etc). ASP .Net parses these files and creates a dll stored in its temp folder. The "aspx, ascx" dlls (it can be more than a single one though) are different files than the one created by you with Visual Studio (and I believe is different than the one created from App_Code folder, since that code can't access code found in the pages).

是否有层次/继承它遵循?

是的。当一个页面被解析和编译,生成类将继承一个在命名为继承属性,在@Page指令中。

Yes. When a page is parsed and compiled, that generated class will inherit the one named at "Inherits" attribute, found at @Page directive.

<%@ Page Language="C#" CodeBehind="Default.aspx.cs" 
         Inherits="WebApplication1._Default" %>

ASP.NET的解析Default.aspx文件并产生继承的类 WebApplication1._Default

public class default_aspx : global::WebApplication1._Default
{

}

捐赠,从标记生成的类继承我们写一个类的事实(通常是一个在code后面的 aspx.cs 文件)我们可以自由使用它的成员或方法。

Giving the fact that the generated class from markup inherits a class written by us (usually the one in the code behind, the aspx.cs file), we're free to use its members or methods.

下面的方法是_Default类的方法:

The following method is a method of the _Default class:

 protected string ToUpper(string source)
        {
            return source.ToUpper();
        }

然后在标记我们可以称之为:

Then in markup we can call:

<form id="form1" runat="server">
  <%= ToUpper("Microsoft") %>
</form>

我们甚至可以在标记像写:

We can even write in markup something like:

<% SomeValue = 1; %>
<%= SomeValue %>

在哪里someValue中它至少_Default类的受保护属性。

Where SomeValue it's at least a protected property of the _Default class.

很自由地声明成员和标记还编写服务器code:

We're free to declare members and write server code in the markup also:

<head runat="server">

    <script runat="server" language="C#">
        private int someCounter = 10;
    </script>

</head>
<body>
    <% for (var i = 0; i < someCounter; i++)
       { %>
        <p>
            Paragraph number:<%= i %>
       </p>
    <% } %>
</body>
</html>

在这里我声明了一个 someCounter 字段,并用它来写10段。当然,这并非是推荐的方式做这样的事情。由于 someCounter 是生成的类的成员,这是不是在code后面的访问。

Here I declare a someCounter field and use it to write 10 paragraphs. Of course this is not the recommended way to do such things. Since someCounter is a member of the generated class, this is not accessible in code behind.

有就是这种架构的另一个巨大的(和更真实)的优势。假设在网站上的某些页面是类的静态(about.aspx,privacy.aspx等),它们使用相同的母版页。在这个页面背后的code不会改变。这意味着我们可以创建其他页面,没有做code的另一个编译后部署它们(这方面适用于Web应用程序项目)。另外,要活的,我们可能只允许一个人看到这些网页。因此,要实现这一点,我们创建一个previewPage类

There is another huge (and more real) advantage of this architecture. Suppose some pages in the web site are kind static (about.aspx, privacy.aspx, etc), they use same Master page. The code behind in this pages doesn't change. This means we can create other pages and deploy them without doing another compilation of the code behind (this aspect applies to Web Application Projects). Also, before going live, we might allow only one person to see these pages. So to achieve this we create a PreviewPage class

public PreviewPage: System.Web.Page
{
     public  PreviewPage()
     {
          this.Load += (o, e) => {
               // code here to see if the authenticated user has the right to see the page
              // if not, redirect the user to another page 
          }
     }
}

和修改继承值:

<%@ Page Language="C#" Inherits="WebApplication1.PreviewPage" %>

ASPX被创建为相应的C#code文件

@Page 指令中的语言属性决定了用什么语言来编译 ASPX / ASCX 文件。所以,实际上你可以在ASPX文件中使用VB.Net,并编译C#编译器的网站。

The Language attribute in the @Page directive dictates what language is used to compile the aspx/ascx file. So you can actually use VB.Net in the aspx file and to compile the website with C# compiler.

这是另一个编译,不同的一个比Visual Studio中呢,它与不同的选择的。这就是为什么在web.config中有选项可以设置 compilationMode 调试/发布,也可指示编译以使用其他可用的选项。

This being another compilation, different than the one Visual Studio does, it is made with different options. This is why in web.config there are options to set the compilationMode to Debug/Release and also to instruct the compiler to use the other available options.

这篇关于如何ASPX被编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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