Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

asp.net mvc - Browser address will not be displayed correctly in ui-route

I am using the following example :

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

Default Page URL in the example above :

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

But for me it is like this:

 http://localhost:25768/Admin#/

why ?

Should be like this:

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

note : Admin is Controller in MVC.

my codes :

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',
                    });
            }
        ]
    );

layout :

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

Index page in Admin Controller :

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

Update :

when url is : http://localhost:25768/Admin

enter image description here

and url change to :

http://localhost:25768/Admin#/

no error and it works. but url is http://localhost:25768/Admin#/ !!!! not good

when url is : localhost:25768/Admin/

enter image description here

so url change to :

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

error in angular :

GET http://localhost:25768/Admin/Scripts/ui-route/home/home.html 404 (Not Found) 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue is on the ASP.NET MVC side... we have to be sure, that

  • browser will be using the trailing slash
  • or redirect it, if not...

This should be the content of your Index action

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();
}

Check this Q & A:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...