2015-11-02 14 views
0

我喜欢Owin基于轻量级代码的配置注册。这是在Owin中创建端点的正确方法吗?如何使用OwinMiddleware做IHttpHandler?

现在下面的代码被打破,它执行(在如果块运行的代码),但后来我得到一个404

public class HelloOkEndpoint : OwinMiddleware 
{ 
    public HelloOkEndpoint(OwinMiddleware next) 
     : base(next) 
    { 
    } 
    public override Task Invoke(IOwinContext context) 
    { 
     IOwinRequest request = context.Request; 
     IOwinResponse response = context.Response; 
     if (request.Path.Value.ToLower().Contains("hello.ashx")) 
     { 
      response.Body = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("Ok!")); 
      response.StatusCode = 200; 
     } 
     return Next.Invoke(context); 
    } 
} 

我通常会实现这个作为一个ASHX

回答

0

好的,我已经解决了,自从google使用google以来,作为参考,hander和owin几乎完全处理中间件,而不是“终端软件”

public class HelloOkEndpoint : OwinMiddleware 
{ 
    public HelloOkEndpoint(OwinMiddleware next) 
     : base(next) 
    { 
    } 
    public override Task Invoke(IOwinContext context) 
    { 
     IOwinRequest request = context.Request; 
     IOwinResponse response = context.Response; 
     if (request.Path.Value.ToLower().Contains("hello.ashx")) 
     { 
      response.StatusCode = 200; 
      //This is the magic, using response.WriteAsync will end the Respose. 
      return response.WriteAsync(System.Text.Encoding.UTF8.GetBytes("Ok!")); 
     } 
     return Next.Invoke(context); 
    } 
}