开发者

How to create a barebones HttpApplication for ASP.Net (without Webforms or MVC)

Ok I am wanting to learn more about how ASP.Net works under the hood. I mean beneath MVC or Webforms and other such frameworks.

Basically I want to know how those frameworks are wired onto ASP.Net开发者_运维百科 so that they work with IIS. What would be the bare minimum for creating a simple HttpApplication which worked with IIS and used neither MVC or Webforms? What is the bare minimum required to be in the Web.config? What would be added to Global.asax?


Write a class that inherits from IHttpHandler. The interface definition is:

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

HttpContext is all you need to execute an application. It acts as a facade to everything involved in the interaction. The Server property gives you information about the server. The Request property gives you information about the HttpRequest, and the Response property provides a means to render output to the client.

My suggestion is to use Reflector on HttpContext and get a feel for what it contains and how each of its components work.

Here's a basic app example:

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 does not have to contain anything. It is probably better practice to handle global events using a class derived from IHttpModule.

web.config has to be handled differently depending on whether you are using IIS 7 or something else. Either way, there is an HttpHandler section where you have to register your custom handler to handle all requests.

You can make the web.config very minimal, but the amount of included configuration sections depend on what features you want. In addition, some of the things that are handled by web.config can be directly managed with IIS. View http://msdn.microsoft.com/en-us/library/b5ysx397(v=VS.85).aspx for more on this.

I hope this helps. We can give you better information if you are more specific with what you are looking for.


I actually meant to answer this question myself as I've done it. smartcaveman provides part of the solution.

What I did for 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>

and then in 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();
}

and then you can use ASP.Net handlers for content(as shown) or you can of course write your own replacement and write to Response yourself.

For reference, my working framework I made with a custom routing engine (and view engine) is in subversion here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜