Serving default index.html page when using Angular HTML5mode and Servicestack on the backend

心已入冬 提交于 2020-01-04 23:21:34

问题


I am new to ServiceStack and Angular. Apologies if this is verbose.

with reference to Html5 pushstate Urls on ServiceStack

I would like to be able to have my api service served up from the root. ie http://mydomain.com/

If a user browses the route, I would like to serve up a default html page that bootstraps my angular app.

In the the app itself if angular calls mydomain.com/customer/{id} json should be served but if this is browsed directly it should serve the default html page and keep the url but the route in the service method does not need to be called. as this will be resolved by angular which will call the customer service itself for a json result.


回答1:


There are probably a few different ways to make this work, as long as you don't need to support html5mode urls. I have hope that I'll be able to leverage this code to eventually support html5mode, but at the moment, I've resigned myself to hash based URLs.

Given urls like: http://example.com/ or http://example.com/#/customer/{id}, here's how to bootstap an angularjs single page app on a self-hosted servicestack project from the root of the domain.

To enable the markdown razor engine, add this to your AppHost config (not absolutely necessary, but my preference over the default razor engine):

   Plugins.Add(new RazorFormat());

Place a file, default.md, in the root of your project, and ensure it's properties are "content/copy when newer". Put whatever content you want in there. The important thing is to assign the template file. (If you're using the default razor engine, an equivalent default.cshtml file should also work, but I've never tried it. ) The template file is what will bootstrap your angularjs app. This is what I have in my 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.

My _Layout.shtml file looks like this (omitting irrelevant details). Note ng-app in the html tag, and the ng-view div in the body. Also note that I don't include <!--@Body--> in the file. I'm not using server side templates for this project.

    <!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>


来源:https://stackoverflow.com/questions/17070835/serving-default-index-html-page-when-using-angular-html5mode-and-servicestack-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!