【问题标题】:Serving default index.html page when using Angular HTML5mode and Servicestack on the backend在后端使用 Angular HTML5mode 和 Servicestack 时提供默认 index.html 页面
【发布时间】:2013-07-04 01:18:26
【问题描述】:

我是 ServiceStack 和 Angular 的新手。抱歉,如果这很冗长。

参考Html5 pushstate Urls on ServiceStack

我希望能够从根目录提供我的 api 服务。即http://mydomain.com/

如果用户浏览路线,我想提供一个默认的 html 页面来引导我的 Angular 应用程序。

如果 Angular 调用 mydomain.com/customer/{id} 应该在应用程序本身中提供,但如果直接浏览它应该提供默认的 html 页面并保留 url,但服务方法中的路由不会需要调用。因为这将由 angular 解决,它将调用客户服务本身以获得 json 结果。

【问题讨论】:

  • 与链接问题基本相同,但我希望角度应用程序和服务 api 从根开始。

标签: angularjs servicestack


【解决方案1】:

只要您不需要支持 html5mode url,可能有几种不同的方法可以完成这项工作。我希望我能够利用 this code 最终支持 html5mode,但目前,我已经放弃了基于哈希的 URL。

给定 URL,例如:http://example.com/http://example.com/#/customer/{id},以下是如何从域的根目录在自托管的 servicestack 项目上启动 angularjs 单页应用程序。

要启用 markdown razor 引擎,请将其添加到您的 AppHost 配置中(不是绝对必要,但我更喜欢默认 razor 引擎):

   Plugins.Add(new RazorFormat());

将文件 default.md 放在项目的根目录中,并确保其属性为“更新时的内容/复制”。把你想要的任何内容放在那里。重要的是分配模板文件。 (如果您使用的是默认剃须刀引擎,等效的 default.cshtml 文件也应该可以工作,但我从未尝试过。)模板文件将引导您的 angularjs 应用程序。这是我的 default.md 中的内容:

    @template  "Views\Shared\_Layout.shtml"

    # This file only exists in order to trigger the load of the template file, 
    # which bootstraps the angular.js app
    # The content of this file is not rendered by the template.

我的 _Layout.shtml 文件看起来像这样(省略不相关的细节)。注意 html 标签中的 ng-app 和 body 中的 ng-view div。另请注意,我没有在文件中包含<!--@Body-->。我没有为这个项目使用服务器端模板。

    <!DOCTYPE html>
    <html lang="en"  ng-app="MyApp">
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="Content/css/app-specific.css"/>        
        <!--[if lt IE 9]>
          <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
    </head>
    <body>
        <!-- Navbar goes here -->
        <div class="container">
            <header id="header">
            <!-- Header goes here -->
            </header>
            <div ng-view></div>
            <hr>
            <footer id="footer">
            <!-- Footer goes here -->
            </footer>
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
        <script src="/Scripts/app-specific/app.js?3ba152" type="text/javascript"></script>
        <script src="/Scripts/app-specific/app-services.js?3ba152" type="text/javascript"></script>
        <script src="/Scripts/app-specific/app-controllers.js?3ba152" type="text/javascript"></script>
    </body>
    </html>

【讨论】:

猜你喜欢
  • 2012-10-23
  • 2015-02-19
  • 2011-12-05
  • 2016-11-30
  • 1970-01-01
  • 2012-01-02
  • 2014-05-04
  • 2023-04-01
  • 1970-01-01
相关资源
最近更新 更多