owin搭载静态网页

最近做的一个项目需要使用owin搭载静态页面,做了个简单的功能,就是启动一个winform窗体,打开浏览器就可以访问指定目录下的页面,本来是mvc的,网上没有找到owin和mvc结合的例子,就只能使用owin搭载静态页面了

主要参照的是这个网页http://www.mamicode.com/info-detail-2198711.html

nuget安装下面这个东东

Install-Package Beginor.Owin.StaticFile
创建StartUp文件

//静态文件托管
appBuilder.Map("/Web", map =>
{
// 允许跨域
//map.UseCors(CorsOptions.AllowAll);
map.UseStaticFile(new StaticFileMiddlewareOptions
{
RootDirectory = @".\Web",
DefaultFile = "index.html",
EnableETag = true,
EnableHtml5LocationMode = true,
MimeTypeProvider = new MimeTypeProvider(new Dictionary<string, string>
{
{ ".html", "text/html" },
{ ".htm", "text/html" },
{ ".dtd", "text/xml" },
{ ".xml", "text/xml" },
{ ".ico", "image/x-icon" },
{ ".css", "text/css" },
{ ".js", "application/javascript" },
{ ".json", "application/json" },
{ ".jpg", "image/jpeg" },
{ ".png", "image/png" },
{ ".gif", "image/gif" },
{ ".config", "text/xml" },
{ ".woff2", "application/font-woff2"},
{ ".eot", "application/vnd.ms-fontobject" },
{ ".svg", "image/svg+xml" },
{ ".woff", "font/x-woff" },
{ ".txt", "text/plain" },
{ ".log", "text/plain" }
})
});
});
}

在窗体的load方法中添加下面代码

url是ip加端口,可以自行制定,比如:http://localhost:8080

var startOpts = new StartOptions(url);
//如果绑定的是ip地址,请使用管理员运行,否则会出错
WebApp.Start<Startup>(startOpts);

在winform根目录下创建web文件夹,放image,css,js等文件

在编码之前,还需要将文件的属性设置为始终复制,这样生成后,文件会自动复制到bin/debug的目录中了

生成后,就可以访问了,使用http://localhost:8080/web/index.html

猜你喜欢

转载自www.cnblogs.com/sharestone/p/9585001.html