Asp.net mvc 浏览器地址将无法在ui路由中正确显示
我使用以下示例: 上面示例中的默认页面URL:Asp.net mvc 浏览器地址将无法在ui路由中正确显示,asp.net-mvc,angularjs,angular-ui-router,Asp.net Mvc,Angularjs,Angular Ui Router,我使用以下示例: 上面示例中的默认页面URL: http://angular-ui.github.io/ui-router/sample/#/ 但对我来说是这样的: http://localhost:25768/Admin#/ http://localhost:25768/Admin/#/ 为什么? 应该是这样的: http://localhost:25768/Admin#/ http://localhost:25768/Admin/#/ 注意:管理员是MVC中的控制器 我
http://angular-ui.github.io/ui-router/sample/#/
但对我来说是这样的:
http://localhost:25768/Admin#/
http://localhost:25768/Admin/#/
为什么?
应该是这样的:
http://localhost:25768/Admin#/
http://localhost:25768/Admin/#/
注意:管理员是MVC中的控制器
我的代码:
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>
更新:
当url为:http://localhost:25768/Admin
和url更改为:
http://localhost:25768/Admin#/
http://localhost:25768/Admin/#/
没有错误,它工作正常。但是url是http://localhost:25768/Admin#/
!!!!不好
当url为:localhost:25768/Admin/
因此,url更改为:
http://localhost:25768/Admin#/
http://localhost:25768/Admin/#/
角度误差:
GET http://localhost:25768/Admin/Scripts/ui-route/home/home.html 404 (Not Found)
问题出在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();
}
检查此问答:
templateUrl: '/Scripts/ui-route/home/home.html',
然后,我将代码更改如下:
public ActionResult Index()
{
var path = Request.Path;
if (path == "/Admin")
{
return Redirect("/Admin/");
}
return View();
}
有效。我一直在使用你的代码。但它没有起作用。当url=''=>root=“/”和applicationPath=“/”和path=“/Admin”时。因此hasTraillingSlash=true。那么,url是
localhost:25768/Admin
或localhost:25768/Admin/
结尾没有哈希。。。浏览器从不将散列后的部分发送到服务器。因此,您的url(带或不带尾随斜杠)将到达服务器。。。此代码适用于我(我们)。。。换句话说:我是说,如果您正确地应用我的代码片段,它将正确地重定向到/Admin/。。。一直以来,问题都是另一种类型的。。。我一直错误地认为管理员是虚拟应用程序。。。不是控制器。如果是这样的话,以上的一切就足够了。但由于Admin是控制器,它带来了url的另一个深度。。不需要。对于索引操作,您应该使用localhost:25768/
。也就是说,它应该是主控制器及其动作索引。。。或者只需更改url路由设置(默认为Admin not Home)。。。这两个thinkg(MVC的相对路径和url路由)不能一起工作……这个答案对我来说很有用。它在本地主机上运行良好,但当推送到我们的服务器时,它停止了工作。现在用这个答案很好。非常感谢!!