.NET MVC和角度与UI路由器路由器、角度、NET、MVC

2023-09-13 03:24:54 作者:做自己的英雄

我在那里我使用.NET MVC来提供初始页index.cshtml,然后提供所有运行单个页面的应用程序所需要的角度成分的情况。对于路由,我现在用的角UI团队出色的UI路由器组成部分,在大多数情况下它工作得很好。

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. For routing, I am using the excellent ui-router component by the Angular-UI team, and for the most part it works well.

我的问题是在第一页的负荷。当浏览的网站,它不会去的第一国家/ URL。它只是加载了我的外壳,没有内容。我的应用程序配置code是这样的:

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. 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": {}
            },
        });

如果我点击周围的导航(通过UI-SREF属性生成的)一切工作正常。这是仅在第一页的负荷。我首先想到的是,这是在.NET MVC路由以某种方式borking了第一个要求,所以我想我已经禁用它通过更改默认路由以下内容:

If I click around the navigation (generated by ui-sref attributes) everything works fine. 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:

        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.

任何想法?

推荐答案

您角code对我来说很好。我有我的项目完全相同的code,当我打 / 我重定向到 /家居

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.

这可能是模板网址是因为ASP.Net MVC路由器无法访问。

It might be that the template url is not accessible because of ASP.Net MVC router.

其他提示:

确保当你刷新 /家页它正常工作确保您添加的 DashboardController 为y我们的应用模块。手动点击应用程序/仪表板/ dashboard.html 来检查,如果你能得到的模板。 Make sure that when you refresh the page on /home it works correctly Make sure you are adding the DashboardController to y our app module. Manually hit app/dashboard/dashboard.html to check if you can get the template.