浏览器地址将无法在ui-route中正确显示

时间:2014-07-28 00:40:06

标签: asp.net-mvc angularjs angular-ui-router

我使用以下示例:

http://angular-ui.github.io/ui-router/sample/#/

上例中的默认页面网址:

 http://angular-ui.github.io/ui-router/sample/#/

但对我来说就是这样:

 http://localhost:25768/Admin#/

为什么?

应该是这样的:

 http://localhost:25768/Admin/#/

注意:Admin是MVC中的Controller。

我的代码:

app.js:

angular.module('uiRouterApp', [
        'uiRouterApp.home',
        'ui.router',
        'ngAnimate'
    ])
    .run(
        [
            '$rootScope', '$state', '$stateParams',
            function($rootScope, $state, $stateParams) {
                $rootScope.$state = $state;
                $rootScope.$stateParams = $stateParams;
            }
        ]
    )
    .config(
        [
            '$stateProvider', '$urlRouterProvider',
            function($stateProvider, $urlRouterProvider) {
                $urlRouterProvider
                    .when('/c?id', '/contacts/:id')
                    .when('/user/:id', '/contacts/:id')
                    .otherwise('/');
            }
        ]
    );

home.js:

angular.module('uiRouterApp.home', [
        'ui.router'
    ])
    .config(
        [
            '$stateProvider', '$urlRouterProvider',
            function($stateProvider, $urlRouterProvider) {
                $stateProvider
                    .state('home', {
                        url: '/',
                        templateUrl: 'Scripts/ui-route/home/home.html',
                    });
            }
        ]
    );

布局:

<!DOCTYPE html>

<html ng-app="uiRouterApp">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

    <title ng-bind="$state.current.name + ' - ui-router'">@ViewBag.Title</title>

</head>
<body class="container adminBackground">
    <div class="min1170px">
        <div class="adminHeader">
            <img class="adminLeftLogoHeader" src="/Images/juventusLogo.png" />
            <img class="adminRightLogoHeader" src="/Images/apkAndroid.png" />
            <div class="adminTitleHeader">

            </div>
        </div>
        <nav class="navbar navbar-inverse" role="navigation">
            <div class="navbar-header">
                <a class="navbar-brand" ui-sref="home">Main</a>
            </div>
            <div id="navbarMenuHeader">
                <ul class="nav navbar-nav">
                    <li><a href="#">Exit</a></li>
                </ul>
                <ul class="nav navbar-nav navbar-left">
                    <li><a href="#"> @User.Identity.Name</a></li>
                </ul>
            </div>
        </nav>
        <div id="body">
            @RenderSection("featured", required: false)
            <section>
                @RenderBody()
            </section>
        </div>
    </div>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @Scripts.Render("~/bundles/angularJs")
    @RenderSection("scripts", required: false)
</body>
</html>

管理控制器中的索引页面:

@{
    ViewBag.Title = "Manager";
    Layout = "../Shared/_AdminLayout.cshtml";
}

更新:

当网址为:http://localhost:25768/Admin

enter image description here

和网址更改为:

http://localhost:25768/Admin#/

没有错误而且有效。但网址是http://localhost:25768/Admin#/ !!!!不好

当网址为:localhost:25768/Admin/

enter image description here

所以网址改为:

http://localhost:25768/Admin/#/

角度误差:

GET http://localhost:25768/Admin/Scripts/ui-route/home/home.html 404 (Not Found) 

2 个答案:

答案 0 :(得分:1)

问题出在ASP.NET MVC方面......我们必须确定,

  • 浏览器将使用尾部斜杠
  • 或重定向,如果不是......

这应该是您的索引操作的内容

public ActionResult Index()
{
    var root = VirtualPathUtility.ToAbsolute("~/");
    var applicationPath = Request.ApplicationPath;
    var path = Request.Path;

    var hasTraillingSlash = 
          root.Equals(applicationPath, StringComparison.InvariantCultureIgnoreCase)
       || !applicationPath.Equals(path, StringComparison.InvariantCultureIgnoreCase);

    if (!hasTraillingSlash)
    {
        return Redirect(root + "#");
    }

    return View();
}

检查此Q&amp;答:

答案 1 :(得分:1)

首先,我将templateUrl更改为

 templateUrl: '/Scripts/ui-route/home/home.html',

然后,我按如下方式更改了代码:

      public ActionResult Index()
        {
            var path = Request.Path;

            if (path == "/Admin")
            {
                return Redirect("/Admin/");
            }

            return View();
        }

有效。