我目前正在使用 Angular 1.6.4 版。
为了解决hashbang
我遇到的问题,我所有的 URL 都是这种格式:
http://localhost:8885/#/hotels/holiday-inn
我app.js
包括路由器(使用ui-router
):
$urlRouterProvider.otherwise('/');
$locationProvider.hashPrefix('');
$locationProvider.html5Mode(true);
$stateProvider
.state('index', {
url: '/',
templateUrl: 'index.html',
controller: 'hotelController'
})
.state('login', {
url: '/hotel/:name',
templateUrl: 'views/hotel.html',
controller: 'hotelController'
});
我ui-view
的位于Index.cshtml
(我以前拥有所有东西,_Layout.cshtml
但将所有东西都移到了Index.cshtml
):
<!DOCTYPE html>
<html ng-app="hotelScheduler">
<head>
<meta charset="utf-8" />
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Application</title>
<link href="~/css/bootstrap.min.css" rel="stylesheet" />
<script src="~/lib/angular/angular.min.js"></script>
<script src="~/lib/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="~/js/app.js"></script>
<script src="~/js/hotelController.js"></script>
</head>
<body>
<div ui-view>
</div>
</body>
</html>
现在我有一个不同的问题。如果我访问http://localhost:8885/hotel/holiday-inn
,我会得到一个404
.
为什么会这样?
其他故障排除:
如果我访问http://localhost:8885/#/hotel/
,它会显示index.html
而不是views/hotel.html
将地址更改为http://localhost:8885/#%2Fhotel%2F
. (我知道这是otherwise
路由中的工作,但为什么它与一个以 hashbang 开头的 URL 一起工作?)
我一直在四处打听并在网上寻找,人们建议其余的工作是通过 C#MapRoute
或服务器端代码完成的。
我将如何通过任一选项执行此操作?如果我要执行此服务器端,我如何通过 ASP.Net Core 和 Visual Studio 执行此操作?
这是我目前的MapRoute
:
app.UseMvc(config =>
{
config.MapRoute(
name: "Default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "App", action = "Index" }
);
});
另一个编辑:基于评论的建议和https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode,我尝试将此代码添加到我的 web.config 以进行服务器重写:
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
但是现在,绝对一切都只是重定向到根/主页:localhost:8885/
.
有什么帮助吗?