无法在 IIS 上部署 Angular 应用程序
Cannot deploy Angular application on IIS
我正在尝试在 IIS 上部署 Angular 应用程序。当我在 IIS 中执行 Sites -> RightClick -> Select Add Web Site as mywebapp 时它正在工作。但是,如果我在 IIS 中执行 默认网站 -> 右键单击 -> Select 添加应用程序 作为 mywebapp,它不起作用。
第一种情况,申请的Url为http://localhost。
在第二种情况下 Url 应用程序是 http://localhost/mywebapp
在第二种情况下,它在浏览器控制台中给出错误。
GET http://localhost/scripts.bundle.js 404 (Not Found)
所以错误被理解为我的应用程序在第二种情况下试图访问 http://localhost/scripts.bundle.js, but it's not present, it should access http://localhost/mywebapp/scripts.bundle.js。
Html 启动页 Index.html.
<!doctype html>
<html lang="en" dir="ltr" style="height:100%">
<head>
<meta charset="utf-8">
<title>mywebapp</title>
<base href="/">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<meta name="author" content="">
<meta name="description" content="">
<link rel="shortcut icon" type="image/x-icon" href="assets/images/favicon.png">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.dataTables.net/1.10.15/css/jquery.dataTables.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body class="theme-red">
<app-root></app-root>
<script type="text/javascript" src="inline.bundle.js"></script><script type="text/javascript" src="polyfills.bundle.js"></script><script type="text/javascript" src="styles.bundle.js"></script><script type="text/javascript" src="scripts.bundle.js"></script><script type="text/javascript" src="vendor.bundle.js"></script><script type="text/javascript" src="main.bundle.js"></script></body>
</html>
部署应用程序的步骤:
- 运行
ng build
来自命令提示符中的 Angular 文件夹。
- 将 dist 文件夹文件复制到某个 abc 文件夹。
- 将 abc 文件夹添加为虚拟目录。
当您的标记中有一个标记时,这是一条指示浏览器始终尝试加载相对于 相对于 中指定的绝对路径的相对路径引用的资源=10=] 元素的 <base>
元素,而不是将这些相对路径视为相对于当前 URL.
的默认行为
在您的情况下,您将基本路径设置为站点根目录 (/
),因此浏览器将尝试从 domain/inline.bundle.js
加载相对路径 inline.bundle.js
(http://localhost/inline.bundle.js
因为您在 IIS 中使用 localhost 主机名)。
如果您想在 IIS 的虚拟目录中托管应用程序,您需要将 <base>
元素设置为指向虚拟目录 - 在您的例子中,<base href="/mywebapp/">
在部署到虚拟目录(注意:结尾的正斜杠 重要 )。如果您使用的是 Angular CLI,则可以使用 --base-href
参数通过 ng build
执行此操作。
请注意,如果您以此方式设置基本路径,它只会影响以相对路径引用的资源,因此所有资源都必须以不以 /
开头的路径引用。对于 Angular 应用程序,这通常意味着路径应该是 CSS 和 JavaScript 的裸文件名,对于资产文件夹中发布的任何文件,路径应以 assets/
开头。
我正在尝试在 IIS 上部署 Angular 应用程序。当我在 IIS 中执行 Sites -> RightClick -> Select Add Web Site as mywebapp 时它正在工作。但是,如果我在 IIS 中执行 默认网站 -> 右键单击 -> Select 添加应用程序 作为 mywebapp,它不起作用。
第一种情况,申请的Url为http://localhost。
在第二种情况下 Url 应用程序是 http://localhost/mywebapp
在第二种情况下,它在浏览器控制台中给出错误。
GET http://localhost/scripts.bundle.js 404 (Not Found)
所以错误被理解为我的应用程序在第二种情况下试图访问 http://localhost/scripts.bundle.js, but it's not present, it should access http://localhost/mywebapp/scripts.bundle.js。
Html 启动页 Index.html.
<!doctype html>
<html lang="en" dir="ltr" style="height:100%">
<head>
<meta charset="utf-8">
<title>mywebapp</title>
<base href="/">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<meta name="author" content="">
<meta name="description" content="">
<link rel="shortcut icon" type="image/x-icon" href="assets/images/favicon.png">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.dataTables.net/1.10.15/css/jquery.dataTables.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body class="theme-red">
<app-root></app-root>
<script type="text/javascript" src="inline.bundle.js"></script><script type="text/javascript" src="polyfills.bundle.js"></script><script type="text/javascript" src="styles.bundle.js"></script><script type="text/javascript" src="scripts.bundle.js"></script><script type="text/javascript" src="vendor.bundle.js"></script><script type="text/javascript" src="main.bundle.js"></script></body>
</html>
部署应用程序的步骤:
- 运行
ng build
来自命令提示符中的 Angular 文件夹。 - 将 dist 文件夹文件复制到某个 abc 文件夹。
- 将 abc 文件夹添加为虚拟目录。
当您的标记中有一个标记时,这是一条指示浏览器始终尝试加载相对于 相对于 中指定的绝对路径的相对路径引用的资源=10=] 元素的 <base>
元素,而不是将这些相对路径视为相对于当前 URL.
在您的情况下,您将基本路径设置为站点根目录 (/
),因此浏览器将尝试从 domain/inline.bundle.js
加载相对路径 inline.bundle.js
(http://localhost/inline.bundle.js
因为您在 IIS 中使用 localhost 主机名)。
如果您想在 IIS 的虚拟目录中托管应用程序,您需要将 <base>
元素设置为指向虚拟目录 - 在您的例子中,<base href="/mywebapp/">
在部署到虚拟目录(注意:结尾的正斜杠 重要 )。如果您使用的是 Angular CLI,则可以使用 --base-href
参数通过 ng build
执行此操作。
请注意,如果您以此方式设置基本路径,它只会影响以相对路径引用的资源,因此所有资源都必须以不以 /
开头的路径引用。对于 Angular 应用程序,这通常意味着路径应该是 CSS 和 JavaScript 的裸文件名,对于资产文件夹中发布的任何文件,路径应以 assets/
开头。