我有一个场景,我想在同一个网址上托管 ASP.NET MVC Web API 应用程序和单页网站项目。
基本上,在http://localhost/上托管网站,在http://localhost/api/上托管API。
在IIS中,这就像设置应用程序子文件夹并将其指向磁盘上的文件一样简单。在OWIN中,我不确定如何实现这种情况。
我有一个控制台应用程序,它将作为Web主机,并且它目前使用此代码中的方法托管WebAPI应用程序: SelfHostWebApiWithOwinAndUnity
有人可以帮忙吗?
答案 0 :(得分:2)
If I'm understanding you correctly, you want to host some static html file(s) as well as the web api. This is fairly simple to do with OWIN. I'm going to assume you already have the nuget packages for Web API, so go ahead and grab the StaticFiles package. Then in Startup.Configuration, you can add the following code:
var options = new FileServerOptions();
options.EnableDefaultFiles = true;
options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" };
options.DefaultFilesOptions.FileSystem = new PhysicalFileSystem("Web");
options.DefaultFilesOptions.RequestPath = PathString.Empty;
app.UseFileServer(options);
This will serve files from the folder "Web". The default file that is served will be "index.html", and any other files in the Web folder can be accessed with their file name (ie. localhost/otherpage.html). It won't allow access to subfolders of Web.
Then simply make sure your WebAPI routes all start with "api/"