我试图在IIS上部署Angular应用程序 . 当我在IIS中使用 Sites -> RightClick -> Select Add Web Site 作为mywebapp时,它正在工作 . 但是如果我在IIS中使用 Default Web Site -> RightClick -> Select Add Application 作为mywebapp它不起作用 .
在第一种情况下,应用程序的URL是http://localhost .
在第二种情况下,应用程序的URL是http://localhost/mywebapp
在第二种情况下,它在浏览器控制台中出错 .
GET http://localhost/scripts.bundle.js 404(未找到)
因此,错误被理解为我的应用程序正在尝试访问http://localhost/scripts.bundle.js,但它不存在,它应该在第二种情况下访问http://localhost/mywebapp/scripts.bundle.js .
Html of startup page 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>
Steps to deploy app:
-
在命令提示符下从Angular文件夹运行
ng build
. -
将dist文件夹文件复制到某个abc文件夹 .
-
将abc文件夹添加为虚拟目录 .
1 回答
当您的标记中有标记时,这是一条指示浏览器始终尝试加载相对于
<base>
元素的href
元素中指定的绝对路径的相对路径引用的资源,而不是处理这些的默认行为 . 相对路径相对于当前URL .在您的情况下,您将基本路径设置为站点根目录(
/
),因此浏览器将尝试从domain/inline.bundle.js
(http://localhost/inline.bundle.js
)加载相对路径inline.bundle.js
,因为您在IIS中使用localhost主机名 .如果要在IIS中的虚拟目录中托管应用程序,则需要将
<base>
元素设置为指向虚拟目录 - 在您的情况下,<base href="/mywebapp/">
在部署到虚拟目录时(注意:尾部正斜杠为 important ) . 如果您正在使用Angular CLI,则可以使用--base-href
参数通过ng build
执行此操作 .请注意,如果以这种方式设置基本路径,它将仅影响使用相对路径引用的资源,因此必须使用不以
/
开头的路径引用所有资产 . 对于Angular应用程序,这通常意味着路径应该是CSS和JavaScript的裸文件名,对于在assets文件夹中发布的任何文件,以assets/
开头 .