Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 388
Earlz Avatar asked Jul 22 '10 01:07

Earlz


People also ask

Can ASP.NET application run without web server?

Yes it is possible to run Web application with out web. config file. However, it will not allow user to debug, even if you are running it in debug mode. Yes, we can run Asp.Net application without using Web.

Is MVC better than Web forms?

Advantages of MVC Over WebformsBetter Control over Design: MVC has dropped concept of server controls and instead use HTML controls or HTML helpers to generate HTML controls. This gives developers better control over HTML and page design. Design time and run time variations are very few as compared to webforms.

What is the difference between MVC and Web forms?

The main difference between Webform and MVC is that the Webform follows a traditional event-driven development model while the MVC follows a Model, View, and, Controller pattern based development model. ASP.NET is a web framework developed by Microsoft.

What is single page application in ASP net MVC?

Single-Page Applications (SPAs) are Web apps that load a single HTML page and dynamically update that page as the user interacts with the app. SPAs use AJAX and HTML5 to create fluid and responsive Web apps, without constant page reloads. However, this means much of the work happens on the client side, in JavaScript.


2 Answers

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.

like image 96
smartcaveman Avatar answered Sep 22 '22 01:09

smartcaveman


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

like image 44
Earlz Avatar answered Sep 19 '22 01:09

Earlz