如何将Angular应用程序部署到IIS以及在url中写什么(对于Windows服务器)

问题描述 投票:0回答:1

我正在尝试在wwwroot文件中部署angular应用程序我不知道在下面的url中写什么,我想写的别名也没有。

  [action type="Rewrite" url="here" /]
angular iis windows-server-2012-r2
1个回答
0
投票

您可以尝试以下步骤在iis中创建和托管角度站点:

首先,通过运行以下命令确保已在计算机上安装了节点:

node --version

使用npm安装Angular CLI

npm install -g @angular/cli

运行以下命令并进入wwwroot文件夹:

cd C:\inetpub\wwwroot

创建一个新的Angular项目

ng new hello-world

现在基本应用程序已准备就绪,我们将使用ng serve…从控制台托管该应用程序。

ng serve

然后构建应用程序

ng build

enter image description here

enter image description here

构建成功完成后,它将所有构建工件发布到dist文件夹。

在iis中创建站点并使用dist文件夹设置文件夹路径:

enter image description here

enter image description here

URL重写路由规则:

此步骤需要支持深层链接。深度链接是用户通过在地址栏中键入路由而不是使用Angular路由来直接导航到页面的功能。深链接给IIS带来了问题,因为服务器不知道用户尝试访问的URL,因此用户收到404响应。解决方案是,即使用户请求应用程序内的路径,服务器也始终返回应用程序的根。

我们现在将添加一个web.config文件,其中包含URL重写规则。对该Web应用程序的所有非文件或文件夹请求都应重定向到该应用程序的根目录。对于默认网站下的Web应用程序或虚拟目录,URL应设置为别名(例如/ MyApp /)。对于位于服务器根目录的网站,URL应该设置为/。

在我们的例子中,因为我们正在使用网络应用程序HelloWorld,所以我们将设置URL / HelloWorld /

<configuration>
<system.webServer>
  <rewrite>
    <rules>
      <rule name="Redirect all requests" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="/HelloWorld/" />
        <!--<action type="Rewrite" url="/" />-->
        </rule>
    </rules>
  </rewrite>
</system.webServer>
</configuration>
© www.soinside.com 2019 - 2024. All rights reserved.