IHttpHandler接口初步实现

写了很长时间的Java,c#的很多内容都已经比较陌生了。这次使用c#中的IHttpHandler初步实现一个Web接口。
首先在VS中创建新的web应用程序。IHttpHandler接口初步实现_第1张图片
接口类代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CSharpWebDemo
{
    /// 
    /// HandlerDemo 的摘要说明
    /// 
    public class HandlerDemo : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }

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

之后需要在Web.config文件中添加访问节点信息,全文如下:




  
    
    
  
  
    
      
      
    
  
  
    
    
      
    
  

主要是配置为configuration→system.webServer→handlers下的add节点,如需要多个接口则可以添加多个add节点,其中path属性为url请求地址;verb属性为请求类型,*是Get,Post都可以,也可以直接写Post这样就只接收Post提交了;type属性为处理类,填入指定的处理类即可。

除此之外c#还有其它种类的是实现方式,后续会再进行更新。

你可能感兴趣的:(c#)