OWIN是Open Web Server Interface for .Net 的首字母缩写,他的定义如下:

OWIN在.NET Web Server 与Web Application之间定义了一套标准接口,OWIN的目标是用于解耦Web Server和Web Application.基于此标准,鼓励开发者开发简单,灵活的模块,从而推进.Net Web Development开源生态系统的发展.之前.net开发的所有webSite和Web Application不得不和IIS绑定到一起部署,对于部署,相当笨重.而OWIN正好是为了解决这个问题!

1.新建一个Console项目

2.添加Owin self host 引用,打开NuGet 程序包管理平台,输入如下代码:

>install-package Microsoft.Aspnet.WebApi.OwinSelfHost

3.新建一个名字为StartUp.cs的类,添加如下代码:

using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http; namespace TestProgram
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
//模仿站点配置
HttpConfiguration config = new HttpConfiguration();
//添加站点路由
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}", //这里为了类MVC特别加入了{action}
defaults: new { id = RouteParameter.Optional });
app.UseWebApi(config);
}
}
}

4.添加Controller,新建文件夹Controller,添加IndexController.cs,添加如下代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http; namespace TestProgram.Controller
{
public class IndexController:ApiController
{
public string Get(string name)
{
return name;
}
}
}

5.将Owin站点启动,修改Program.cs,如下:

using Microsoft.Owin.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks; namespace TestProgram
{
class Program
{
static void Main(string[] args)
{
//此入口是Owin站点的宿主
string baseAddress = "http://localhost:9009/";//站点启动地址
using (WebApp.Start<Startup>(baseAddress))
{
HttpClient client = new HttpClient();
//var response = client.GetAsync(baseAddress + "api/index").Result;
//Console.WriteLine(response);
//Console.WriteLine(response.Content.ReadAsStreamAsync().Result);
Console.ReadLine();//不让宿主程序结束
}
}
}
}

6.运行程序,打开浏览器输入 http://localhost:9009/index/index?name="RemiHoston"

由于没有设置返回的数据类型,所以默认返回XMl格式,但是,只是返回字符串并不能满足要求;但是Owin self hosted 程序宿主于Console没有提供文件下载访问,所以我们要自己提供接口支持文件访问

7.添加静态文件访问的接口

修改IndexController.cs添加如下两个方法:

 [HttpGet]
public HttpResponseMessage Index()
{
//构造文件路径,代码重构的时候可以把这一块代码封装成公用方法
var currentRunPath = AppDomain.CurrentDomain.BaseDirectory;
var substringBin = currentRunPath.IndexOf("bin");
//返回Index.html文件
var path = currentRunPath.Substring(, substringBin) + "Index.html";
var httpResponseMessage = new HttpResponseMessage();
httpResponseMessage.Content = new StringContent(File.ReadAllText(path), Encoding.UTF8);
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
//把文件内容读出来,以HttpResponseMessage的形式返回客户端
return httpResponseMessage;
}
/// <summary>
/// 获取静态文件,比如css,javascript
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
[HttpGet]
public HttpResponseMessage GetResource(string fileName)
{
//构造文件路径,代码重构的时候可以把这一块代码封装成公用方法
var currentRunPath = AppDomain.CurrentDomain.BaseDirectory;
var substringBin = currentRunPath.IndexOf("bin");
var path = currentRunPath.Substring(, substringBin) + fileName; var httpResponseMessage = new HttpResponseMessage();
if (!File.Exists(path))
{
httpResponseMessage.StatusCode = System.Net.HttpStatusCode.NotFound;
return httpResponseMessage;
}
httpResponseMessage.Content = new StringContent(File.ReadAllText(path), Encoding.UTF8);
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return httpResponseMessage;
}

8.新建站点页面

站点根目录下新建Script文件夹,下载添加query-1.6.4.min.js,新建Index.html,添加如下代码:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Owin默认页</title>
<!--通过站点接口,下载Jquery-->
<script src="getresource?filename=scripts/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
$(function () {$('body').append("<h1>Hello Owin!</h1>") })
</script>
</head>
<body>
</body>
</html>

运行重新刷新,页面

至此,解决了Owin Self Hosted Console 程序加载页面的问题,下一篇将在此基础上新建SignalR程序!

新建一个self hosted Owin+ SignalR Project(1)的更多相关文章

  1. 新建一个self hosted Owin+ SignalR Project(2)

    ASPNET SignalR是为ASP.NET开发人员提供的一个库,可以简化开发人员将实时Web功能添加到应用程序的过程.实时Web功能是指这样一种功能:当所连接的客户端变得可用时服务器代码可以立即向 ...

  2. Eclipse中在android项目中出现新建一个Activity后,出现整个project的报错以及包导入以后无法执行等等情况分析。

    今天用Eclipse去写android项目,然后后面须要建一个Blank  Activity后,非常正常的建立的.然后那个Activity是基于ActionBarAtivity,要导入v7,结果由于这 ...

  3. 18 12 30 新建一个 django project

    1. 新建一个 django project 1 2 django-admin.py startproject project_name 特别是在 windows 上,如果报错,尝试用 django- ...

  4. android studio 导入一个已有的android studio project作为lib使用

    android studio 导入一个已有的android studio project作为lib使用 新项目来了. 需要搭建框架. android studio对我来说还是很陌生,之前一个项目在同事 ...

  5. zynq学习01 新建一个Helloworld工程

    1,好早买了块FPGA板,zynq 7010 .终极目标是完成相机图像采集及处理.一个Window C++程序猿才开始学FPGA,一个小菜鸟,准备转行. 2,关于这块板,卖家的官方资料学起来没劲.推荐 ...

  6. Android学习笔记(一)——新建一个项目

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 1.打开Android Studio时出现以下界面,点击”start a new Android Studio ...

  7. 第一次使用Android Studio时你应该知道的一切配置(二):新建一个属于自己的工程并安装Genymotion模拟器

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  8. 新建一个mybatis HelloWorld

    1.下载mybatis https://github.com/mybatis/mybatis-3/ 没有梯子好像打不开 下载一个最新版本,我这里下载的是mybatis-3.4.1.zip 里面有myb ...

  9. Intellij IDEA 新建一个EJB工程(三)

    之前都是用IDEA启动JBoss服务器,并在启动的同时将EJB项目部署上去.在构建 artifacts 时遇到很多问题,明明是EJB项目却不能用EJB导出,真是奇怪~~ 后来用Web Applicat ...

随机推荐

  1. L1-049. 天梯赛座位分配

    天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所学校参赛,第 i 所学校有 M[i] 支队伍,每队 10 位 ...

  2. Docker Compose 一键部署LNMP

    Docker Compose 一键部署LNMP 目录结构 [root@localhost ~]# tree compose_lnmp/ compose_lnmp/ ├── docker-compose ...

  3. 自制操作系统Antz(2)——进入保护模式 (上) jmp到保护模式

    Antz系统更新地址: https://www.cnblogs.com/LexMoon/category/1262287.htm Linux内核源码分析地址:https://www.cnblogs.c ...

  4. Linux统计文件中单词出现的次数

    grep -E "\b[[:alpha:]]+\b"  /etc/fstab  -o | sort | uniq -c 或 awk '{for(i=1;i<NF;i++){c ...

  5. 浅析vue实例的生命周期(生命周期钩子)

    “每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要设置数据监听.编译模板.将实例挂载到 DOM 并在数据变化时更新 DOM 等” ,在不同的生命周期内会经历不同的钩子函数(生命周期 ...

  6. 20175312 2018-2019-2 《Java程序设计》第5周学习总结

    20175312 2018-2019-2 <Java程序设计>第5周学习总结 教材学习内容总结 已依照蓝墨云班课的要求完成了第六章的学习,主要的学习渠道是PPT,和书的课后习题. 总结如下 ...

  7. vue+vux scrollTop无法实现定位到具体dom

    先看一下最终的运行效果. 项目背景介绍:技术栈: vue+vux+nodejs需求:对汽车品牌列表可以按照字母进行索引定位 在开发中实现这种需求,心想还不是小菜一碟,作为一个饱经bug的程序员,别的我 ...

  8. MapReduce编程:平均成绩

    问题描述 现在有三个文件分别代表学生的各科成绩,编程求各位同学的平均成绩.                     编程思想 map函数将姓名作为key,成绩作为value输出,reduce根据key ...

  9. ssh hibernate修改数据库

     org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in rea ...

  10. Bioconductor软件安装与升级

    1 安装工具Bioc的软件包不能使用直接install.packages函数,它有自己的安装工具,使用下面的代码: source("https://bioconductor.org/bioc ...