渲染web文件而不使用路由+layout.cshtml



我想在ASP.NET 5(Visual Studio 2015)中启动一个新的AngularJS单页应用程序。我将只使用ASP.NET的WebAPI功能,而不是视图渲染(Razor)。

因此,如果我不需要HomeController_Layout.cshtml来引导我的Angular应用程序,我会更喜欢它。

有没有办法删除它,只让ASP.NET返回一个静态的初始页面?我将通过AngularJS 进行所有路由

更新

根据@Phills的建议,我已将index.html放入wwwroot。如果我导航到http://localhost:62583/index.html,它会起作用,但如果我导航至http://localhost:62583/ ,它就不会起作用

是的,你可以这样做。将文件放在wwwroot/(例如,称为index.html)中,然后确保Startup.cs配置方法中存在以下内容:

app.UseDefaultFiles();   // Allows index.html to be used automatically
app.UseStaticFiles();    // Must be AFTER UseDefaultFiles
app.UseMvc();            // Required for your API controllers

顺便说一句,即使你坚持使用HomeController,也不需要_Layout.chtml-你只需要从Views\Shared_ViewStart.chtml中删除对它的引用。如果你这样做,你会把所有内容放在Views\Home\Index.chtml.

您可以在应用程序的根目录中创建一个index.html页面,并删除HomeController,如果找不到路由,它将选择index.html页面。

可以。您的视图不一定要有布局页。您仍然会从控制器返回一个cshtml视图,它基本上是一个启动Angular应用程序的静态HTML。

不,尤其是当你要开发一个Angular JS应用程序时,你将能够使用AngularJS本身处理模板。

您可以使用index.html来代替布局页面,并且可以使用<ng视图/>标记来渲染局部视图。

你需要有一些类似MVC 的路由设置

  angular.module("myApp",["ngRoute"])
            .config(function($routeProvider){
                $routeProvider.when("/home",{
                    templateUrl:"/views/home.html"
                });
                $routeProvider.when("/placeorder",{
                    templateUrl:"/views/placeOrder.html"
                });

                $routeProvider.otherwise({
                    templateUrl:"/views/home.html"
                });
            });

您的视图将类似于此

<body>
  <ng-view/>
</body>

最新更新