c# - Angular 和 WebAPI 配置

标签 c# angular azure asp.net-web-api .net-4.6

我们有一个 Angular 4 应用程序和一个 WebAPI 应用程序 (.NET Framework)。我们设置了一个 Azure 应用程序,其中部署了两个应用程序(客户端和服务)。根路径指向客户端。

对我们的 API (/api/{controller}/) 的 HTTP 请求未到达我们的服务,看起来 Angular 路由正在获取它们并返回 index.html 作为响应内容。

我们如何设置 Azure,以便我们的 Angular 应用程序提供 HTML/CSS/JS 内容以及对“/api/”的任何调用都会路由到我们的服务?

我们尝试让根路径指向服务,但随后我们收到所有非 API 请求的 404。

最佳答案

为了回答我自己的问题,我需要提供本文中概述的静态文件:

https://mirkomaggioni.com/2016/09/11/owin-middleware-for-static-files/

简而言之,我将服务 (WebAPI) 部署到 Azure 根目录,并将客户端 (Angular 4) 部署为编译后的“dist”文件夹。

我添加了以下 NuGet 包:

<package id="Owin" version="1.0" targetFramework="net452" />
<package id="Microsoft.Owin" version="3.1.0" targetFramework="net462" />
<package id="Microsoft.Owin.FileSystems" version="3.1.0" targetFramework="net462" />
<package id="Microsoft.Owin.Host.SystemWeb" version="3.1.0" targetFramework="net462" />
<package id="Microsoft.Owin.StaticFiles" version="3.1.0" targetFramework="net462" />

然后创建一个 OWIN Startup 类,如下所示:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var physicalFileSystem = new PhysicalFileSystem(@"./dist");
        var options = new FileServerOptions
        {
            EnableDefaultFiles = true,
            FileSystem = physicalFileSystem
        };
        options.StaticFileOptions.FileSystem = physicalFileSystem;
        options.StaticFileOptions.ServeUnknownFileTypes = true;
        options.DefaultFilesOptions.DefaultFileNames = new[]
        {
            "index.html"
        };

        app.UseFileServer(options);
    }
}

关于c# - Angular 和 WebAPI 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45179678/

相关文章:

c# - 如何从 Azure DocumentDB 查询结果中选择第一个元素

html - display flex 不会像预期的那样打断长行

javascript - 如何将 AngularJS 1 服务转换为 Angular 2 服务?

azure - 无法在同一网络上访问 Cassandra

azure - Cordova/Ionic 框架与 ADAL 配合使用

Azure 指标 : dtu_used does not accept zero dimension case

c# - 无法将类型 'customtype' 隐式转换为 'othercustomtype'

c# - Xamarin - 找不到类(android 支持库)

c# - RejectChanges() 与 ViewModel 和 MVVM 设计模式和 UI 更新

javascript - Angular 图表.js "Cannot use import statement outside a module"