版本 .Net Framework 4.6
在Vs2017程序包管理控制台中运行以下命令
安装 Microsoft.Owin.SelfHost
Install-Package Microsoft.Owin.SelfHost -Version 4.0.0
安装 Microsoft.AspNet.WebApi.Owin
Install-Package Microsoft.AspNet.WebApi.Owin -Version 5.2.6
如果需要静态文件支持需安装 Microsoft.Owin.StaticFiles
Install-Package Microsoft.Owin.StaticFiles -Version 4.0.0
如果Microsoft.Owin.Host.HttpListener不在启动项目里面,需要手动安装到启动项目里面
Install-Package Microsoft.Owin.Host.HttpListener -Version 4.0.0
在App.Config中添加如下代码(如果没有的话)
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="5.2.6.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime />
<pages controlRenderingCompatibilityVersion="4.0" />
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
添加Startup.cs
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseErrorPage();
var config = GetWebApiConfig();
app.UseWebApi(config);
app.UseFileServer(new FileServerOptions()
{
RequestPath = PathString.Empty,
FileSystem = new PhysicalFileSystem(@".\www")
});
app.UseWelcomePage("");
}
private HttpConfiguration GetWebApiConfig()
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "Default",
routeTemplate: "api/{controller}/{action}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "Request",
routeTemplate: "api/crawler/request",
defaults: new { controller = "CrawlerApi", action = "request" }
);
config.Routes.MapHttpRoute(
name: "ServerInfo",
routeTemplate: "api/crawler/server/info",
defaults: new { controller = "CrawlerApi", action = "serverinfo" }
);
//config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("datatype", "json", "application/json"));
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()
{
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
};
return config;
}
}
在启动项目下添加www文件夹 并在其中建立 index.html 内容随意
将index.html 复制到输出目录属性设置为 如果较新则复制
调用如下代码启动Owin网站
WebApp.Start<Startup>(baseUrl);
————————————————
版权声明:本文为CSDN博主「极创意想」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42581666/article/details/106169298