5

好的,我想了解更多关于 ASP.Net 如何在后台工作的信息。我的意思是在 MVC 或 Webforms 和其他此类框架之下。

基本上我想知道这些框架是如何连接到 ASP.Net 上的,以便它们与 IIS 一起工作。创建一个与 IIS 一起工作且既不使用 MVC 也不使用 Webforms 的简单 HttpApplication 的最低要求是什么?Web.config 中的最低要求是什么?Global.asax 中会添加什么?

4

2 回答 2

7

编写一个继承自IHttpHandler. 接口定义为:

public interface IHttpHandler
{
    void ProcessRequest(HttpContext context);
    bool IsReusable { get; }
}

HttpContext是执行应用程序所需的全部内容。它充当交互中涉及的所有内容的外观。该Server属性为您提供有关服务器的信息。该Request属性为您提供有关 HttpRequest 的信息,并且该Response属性提供了一种将输出呈现给客户端的方法。

我的建议是在 HttpContext 上使用 Reflector 并了解它包含的内容以及它的每个组件如何工作。

这是一个基本的应用程序示例:

public class HelloWorldHandler: IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.Write("Hello World");
        context.Response.End();
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

Global.asax不必包含任何内容。使用从IHttpModule.

根据您使用的是 IIS 7 还是其他方式,必须对 web.config 进行不同的处理。无论哪种方式,都有一个 HttpHandler 部分,您必须在其中注册自定义处理程序以处理所有请求。

您可以使 web.config 非常小,但包含的配置部分的数量取决于您想要的功能。另外,web.config处理的一些事情可以直接用IIS来管理。查看http://msdn.microsoft.com/en-us/library/b5ysx397(v=VS.85).aspx了解更多信息。

我希望这有帮助。如果您更具体地了解您正在寻找的内容,我们可以为您提供更好的信息。

于 2011-01-13T19:21:26.477 回答
2

我实际上是想自己回答这个问题,因为我已经这样做了。smartcaveman 提供了部分解决方案。

为 web.config 做了什么:

<?xml version="1.0"?>
<configuration>
    <system.web>
       <compilation debug="true">
       </compilation>
    </system.web>
    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <providerOption name="CompilerVersion" value="v3.5"/>
                <providerOption name="WarnAsError" value="false"/>
            </compiler>
        </compilers>
    </system.codedom>
    <!--
    The system.webServer section is required for running ASP.NET AJAX under Internet
    Information Services 7.0. It is not necessary for previous version of IIS.
    -->
    <system.webServer>
    </system.webServer>
    <runtime>
    </runtime>
</configuration>

然后在 global.asax 中:

protected virtual void Application_BeginRequest (Object sender, EventArgs e)
{
    if (Request.Url.AbsolutePath == "/test") 
    {
        var h=new Test1(); //make our Test1.ashx handler
        h.ProcessRequest(Context);
    }
    else
    {
        Response.ContentType = "text/plain";
        Response.Write("Hi world!");
    }
    CompleteRequest();
}

然后您可以对内容使用 ASP.Net 处理程序(如图所示),或者您当然可以编写自己的替换并自己写入响应。

作为参考,我使用自定义路由引擎(和视图引擎)制作的工作框架在此处处于颠覆状态

于 2011-01-14T01:58:15.273 回答