简体   繁体   English

.NET MVC和Angular与ui-router

[英].NET MVC and Angular with ui-router

I have a scenario where I am using .NET MVC to serve up an initial page 'index.cshtml' which then delivers all the Angular components required to run a single page app. 我有一个场景,我使用.NET MVC来提供初始页面'index.cshtml',然后提供运行单页面应用程序所需的所有Angular组件。 For routing, I am using the excellent ui-router component by the Angular-UI team, and for the most part it works well. 对于路由,我使用Angular-UI团队的优秀ui-router组件,并且在大多数情况下它运行良好。

My issue is with the very first page load. 我的问题是第一页加载。 When browsing to the site, it doesn't go to the very first state/url. 浏览网站时,它不会转到第一个状态/网址。 It just loads up my 'shell' with no content. 它只是加载我的'shell'没有任何内容。 My app config code looks like this: 我的应用配置代码如下所示:

app.config(function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/home');
    $stateProvider
        .state('home', {
            url: "/home",
            views: {
                "content": {
                    templateUrl: "app/dashboard/dashboard.html",
                    controller: 'DashboardController'
                },
                "subNav": {}
            },
        });

If I click around the navigation (generated by ui-sref attributes) everything works fine. 如果我点击导航(由ui-sref属性生成),一切正常。 It's only the first page load. 它只是第一页加载。 My first thought was that it was the .NET MVC routing that is somehow borking up the first request, so I think I've disabled it by changing the default route to the following: 我的第一个想法是,.NET MVC路由以某种方式引发了第一个请求,所以我想我已经通过将默认路由更改为以下来禁用它:

        routes.MapRoute(
            name: "Default",
            url: "{*url}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

But I can't be sure this has done what I wanted it to do. 但我无法确定这是否符合我的要求。 It certainly didn't fix my issue. 它当然没有解决我的问题。

Any ideas? 有任何想法吗?

Your angular code looks good to me. 你的角度代码对我来说很好看。 I have the exact same code on my project and when I hit / I get redirected to /home . 我在我的项目中有完全相同的代码,当我点击/我被重定向到/home

It might be that the template url is not accessible because of ASP.Net MVC router. 由于ASP.Net MVC路由器,可能无法访问模板URL。

Other tips: 其他提示:

  • Make sure that when you refresh the page on /home it works correctly 确保在/home上刷新页面时它可以正常工作
  • Make sure you are adding the DashboardController to y our app module. 确保将DashboardController添加到我们的app模块中。
  • Manually hit app/dashboard/dashboard.html to check if you can get the template. 手动点击app/dashboard/dashboard.html以检查是否可以获取模板。

I think you have to actively set a route for otherwise to be used. 我认为你必须积极设置一条路线以供其他方式使用。

You can solve the problem by setting the state in the run block and handle the $stateChangeError. 您可以通过在运行块中设置状态并处理$ stateChangeError来解决问题。

app.run(['$rootScope','$state',function($rootScope,$state) {
    $state.transitionTo('home');

    $rootScope.$on('$stateChangeError', 
      function(event, toState, toParams, fromState, fromParams, error){ 
      $state.transitionTo('home');
    });
}]);

A more radical alternative is to remove your reliance on .net MVC to serve up any pages. 更激进的替代方案是消除您对.net MVC的依赖以提供任何页面。 You could then move all your server side code into a .net webApi for a reuseable api including all security etc. (although you should replicate your security in your front end also). 然后,您可以将所有服务器端代码移动到.net webApi中,以获得包含所有安全性等的可重复使用的api(尽管您也应该在前端复制安全性)。 Your Angular app with ui-router would then be responsible for all navigation and routing. 然后,使用ui-router的Angular应用程序将负责所有导航和路由。 Makes it less complex in my opinion. 在我看来,它不那么复杂。 No need to mix an angular SPA and MVC. 无需混合角度SPA和MVC。 All the features in MVC such as bundling/minification/routing etc can be done in your front end angular app using bower/grunt/gulp packages and configuration. MVC中的所有功能(如捆绑/缩小/路由等)都可以使用bower / grunt / gulp软件包和配置在您的前端角度应用程序中完成。 Just requires you to do a bit more reading...leaves you with a cleaner less complex solution and only and index.html and associated assets to deploy (alongside your Webapi which you can re-use). 只需要你多做一些阅读......只需要一个更简洁的解决方案,只需要部署index.html和相关资产(与你可以重复使用的Webapi一起)。 To do this you can create a web project (non MVC) add an index.html and off you go. 要做到这一点,你可以创建一个web项目(非MVC)添加index.html然后离开。 If you want to add in Modern Web development techniques you can use one of the .net vNext web application templates which allows you to integrate with npm/gulp/grunt/bower. 如果要添加现代Web开发技术,可以使用.net vNext Web应用程序模板之一,它允许您与npm / gulp / grunt / bower集成。 Microsoft have already built this in. Deployment can ignore the vNext project and just work on the html/js/assets. Microsoft已经构建了这个。部署可以忽略vNext项目,只需处理html / js / assets。 Productivity enhancements from the vNext project, yet simplicity from losing MVC. vNext项目的生产力增强,但失去MVC的简单性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM