IIS如何知道它是服务于一个网站还是一个Web应用程序项目? [英] How does IIS know if it's serving a Web Site or a Web Application project?

查看:110
本文介绍了IIS如何知道它是服务于一个网站还是一个Web应用程序项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道网站项目在实时编译源代码,而Web应用程序项目将源代码预编译为DLL(很像ASP.Net 1.x)。



但是在IIS中指定的区别是什么?



我知道Visual Studio知道 - 每个都有不同的项目等等。但是运行的实例IIS +框架)知道使用哪个编译模型,对吧?因为它怎么知道是否要在运行时编译?



一个请求进来,命中一个ASPX文件...这个过程如何知道是否需要编译相关的CS文件(网站),或者如果它在部署之前已经完成(Web应用程序)?



指定。在web.config某处?

解决方案

.aspx文件中有一个微妙的差别,你会发现在这些项目类型。



如果你看一个网站项目,你应该看到这样的...

 <%@ Page Language =C#AutoEventWireup =true
CodeFile =Default.aspx.csInherits =_ Default%&

...其中,Web应用程序项目将具有类似这样的.aspx文件...

 <%@ Page Language =C#AutoEventWireup =true
CodeBehind =Default.aspx。 csInherits =WebApplication2._Default%>

请注意,第一个具有CodeFile属性,第二个具有CodeBehind属性。这是做出区分的地方。



CodeBehind属性在运行时不使用 - 它可以告诉VS.NET代码的存在位置,Inherits属性告诉运行时哪个类要搜索



CodeFile属性在运行时使用,并由aspnet_compiler.exe用于生成代码,然后使用Inherits属性。 p>

有关这些属性的更多信息,请在这里查看...



http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx



但是,为了回答你的问题IIS如何知道?答案是它不。 ASP.NET知道。



您可以通过执行以下操作来证明是这种情况:


  1. 创建新的Web应用程序。这将包括Default.aspx和Default.aspx.cs。

  2. 将以下代码添加到Default.aspx.cs中:

      protected void Page_Load(object sender,EventArgs e)
    {
    Response.Write(hello);
    }


  3. 编译项目,运行它,


  4. 现在,更改代码,使其看起来像
    ,并保存.cs文件:

      protected void Page_Load(object sender,EventArgs e)
    {
    Response.Write(goodbye) ;
    }


  5. 不要编译。刷新浏览器。


  6. 现在,将Default.aspx中的attrib从CodeBehind更改为CodeFile。保存此文件。


  7. 刷新浏览器。


  8. 将代码中的goodbye更改为I believe!。保存.aspx.cs但不编译。


  9. 刷新浏览器,查看我相信!



I understand that Web Site Projects compile source on-the-fly, and Web Application Projects pre-compile source into a DLL (much like ASP.Net 1.x).

But how is the difference specified in IIS?

I know that Visual Studio knows -- there are different projects for each, etc. But the running instance (IIS + Framework) has to know which compilation model is being used, right? Because how else does it know whether or not to compile on-the-fly?

A request comes in, hits an ASPX file...and how does the process know whether the associated CS file needs to be compiled (Web Site), or if it was already done before deployment (Web Application)?

I'm just curious where this difference is specified. In the web.config somewhere?

解决方案

There is a subtle difference in the .aspx file that you'll find in these project types.

If you look at a Web Site Project you should see something like this...

<%@ Page Language="C#" AutoEventWireup="true"  
CodeFile="Default.aspx.cs" Inherits="_Default" %>

... where as the Web Application project will have .aspx files with something like this...

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

Notice that the first has a CodeFile attribute, and the second as a CodeBehind attribute. This is where the distinction is made.

The CodeBehind attribute is NOT used at runtime - it's there to tell VS.NET where the code lives, and the Inherits attribute tells the runtime which class to go searching for in the binaries.

The CodeFile attribute IS used at runtime, and is used by the aspnet_compiler.exe to generate code, and then the Inherits attribute is used as above.

For more info on these attributes, look here...

http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx

But to answer your question "how does IIS know?" the answer is "it doesn't." ASP.NET knows.

You can prove that this is the case by doing the following:

  1. Create a new web application. This will include a Default.aspx and a Default.aspx.cs.
  2. Add the following code into Default.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("hello");
    }
    

  3. Compile the project, run it, see the text "hello" appear in a browser.

  4. Now, change the code so it looks like this, and save the .cs file:

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("goodbye");
    }
    

  5. DO NOT COMPILE. Refresh your browser. You'll still see "hello" because the compiled code still uses this string.

  6. Now, change the attrib in Default.aspx from CodeBehind to CodeFile. Save this file.

  7. Refresh your browser. You'll see "goodbye" displayed.

  8. Change "goodbye" in your code to "I believe!". Save the .aspx.cs but don't compile.

  9. Refresh your browser, see "I believe!", and dance around the room enlightend :-)

这篇关于IIS如何知道它是服务于一个网站还是一个Web应用程序项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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