[英]How to create a barebones HttpApplication for ASP.Net (without Webforms or MVC)
好的,我想了解更多有關ASP.Net如何在幕后工作的信息。 我的意思是MVC或Webforms以及其他類似的框架。
基本上我想知道這些框架如何連接到ASP.Net,以便它們可以與IIS一起使用。 創建一個簡單的HttpApplication,與IIS一起工作並且既不使用MVC也不使用Webforms,最低限度是什么? 在Web.config中需要的最低要求是什么? 什么會被添加到Global.asax?
編寫一個繼承自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
派生的類處理全局事件可能是更好的做法。
必須以不同的方式處理web.config,具體取決於您使用的是IIS 7還是其他。 無論哪種方式,都有一個HttpHandler部分,您必須注冊自定義處理程序來處理所有請求。
您可以使web.config非常小,但包含的配置部分的數量取決於您想要的功能。 此外,可以使用IIS直接管理web.config處理的一些內容。 有關詳細信息,請查看http://msdn.microsoft.com/en-us/library/b5ysx397(v=VS.85).aspx 。
我希望這有幫助。 如果您對所需內容更具體,我們可以為您提供更好的信息。
我實際上是想在我做完之后自己回答這個問題。 smartcaveman提供了部分解決方案。
<?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處理程序獲取內容(如圖所示),或者你當然可以編寫自己的替換程序並自己寫入Response。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.