AngularJs UI-Router - $stateProvider 无法读取我的抽象状态 [英] AngularJs UI-Router - $stateProvider can't read my abstract state
问题描述
我对 html5Mode 有问题.在我的 MVC 项目中,我只有一个控制器
I have problem with html5Mode. In my MVC project I have only one controller
public class HomeController : Controller
{
public ActionResult Index()
{
return this.File("index.html", "text/html");
}
}
在我的 Angular 项目中,我有两条路线和一个摘要.
In my angular project I have two routes and one abstract.
angular.module('app').config(function ($urlRouterProvider, $stateProvider, $locationProvider) {
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/articles');
$stateProvider
.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/blocks/layout.html",
controller: 'AppCtrl'
})
.state('app.articles', {
url: "/articles",
templateUrl: 'templates/articles.html',
controller: 'ArticlesCtrl'
})
.state('app.one-article', {
url: "/articles/:articleId",
templateUrl: 'templates/one-article.html',
controller: 'OneArticleCtrl'
});
// use the HTML5 History API
$locationProvider.html5Mode(true).hashPrefix('!');
});
这是在RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "articles",
url: "app/articles",
defaults: new { controller = "Home", action = "Index" });
routes.MapRoute(
name: "Default",
url: "{*url}",
defaults: new { controller = "Home", action = "Index" });
}
当我从根导航时一切正常.但是当我点击刷新按钮时抽象状态没有加载.请帮我解决这个问题.
Everything works fine when I'm navigating from the root. But when I will click the refresh button abstract state is not loaded. Please help me how to solve this.
谢谢
推荐答案
我解决了这个问题.
我的索引文件位于解决方案的根目录中.相反,我删除了那个 index.html 文件并创建了 MVC 视图 Views -> Home -> Index.cshtml.所有 HTML 与 index.html 中的相同.
My index file was in the root of the solution. Instead of that I removed that index.html file and created MVC view Views -> Home -> Index.cshtml. All HTML is the same as in index.html.
现在在我使用的控制器中
Now from in the controller I'm using
public ActionResult Index()
{
return this.View();
}
而不是这个:
public ActionResult Index()
{
return this.File("index.html", "text/html");
}
这是我的 RouteConfig.cs
Here is my RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "AngularCatchAllRoute",
url: "{*any}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
希望有人会觉得这篇文章有用.
Hope someone will find this article useful.
博格丹
这篇关于AngularJs UI-Router - $stateProvider 无法读取我的抽象状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!