asp.net-mvc - AngularJS - 用户界面路由器 - MVC 5 - html5mode

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

我正在尝试使用 AngularJSMVC 5 创建一个迷你 SPA。除此之外,我想为 AngularJS 使用 ui-router 插件而不是 ng-route,并希望启用 html5mode

我的问题是,每当我单击 anchor 元素时,它都会刷新页面并向 ASP.NET MVC Controller 发送请求并将所选 View 放在正确的位置,我不希望它重新加载。

如果我将 AngularJS 路由机制更改为 ng-route,那么它会按我的意愿工作,不会刷新页面,并会路由到选定的 View 。

在 MVC 5 RouteConfig.cs 文件中,

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "SpaUI",
            url: "{SpaUI}/{*catchall}",
            defaults: new { controller = "SpaUI", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "SpaUI", action = "Index", id = UrlParameter.Optional }
        );

在 AngularJS 的 app.js 文件中,

app.config(['$stateProvider', '$provide', '$locationProvider', '$urlRouterProvider', function ($stateProvider, $provide, $locationProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/Dashboard');

    $stateProvider
    .state("dashboard", {
        url: "/Dashboard",
        templateUrl: "AngularViews/dashboard.html"
    })

    .state("test", {
        url: "/Test",
        templateUrl: "AngularViews/test.html"
    });

    $locationProvider.html5Mode(true);
 }]);

在 anchor 的_Layout.cshtml文件中;

 <a data-ui-sref="dashboard" title="Dashboard">

在相同的 _Layout.cshtml 文件中用于 View 占位符

 <div id="content" data-ui-view="" class="view-animate"></div>

如何在不重新加载页面的情况下让所有这些东西一起玩? 任何想法表示赞赏:)

最佳答案

可能对你有帮助!!

这样做:

  • 移除“app”路由的复杂性,并使用标准路由。
  • 添加重写规则。
  • 从布局中删除基本 href。

例如看起来像这样。

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.LowercaseUrls = true;
        routes.MapRoute("Default", "{controller}/{action}/{id}", new
        {
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        }).RouteHandler = new DashRouteHandler();
    }
}

public class DashRouteHandler : MvcRouteHandler
{
    /// <summary>
    ///     Custom route handler that removes any dashes from the route before handling it.
    /// </summary>
    /// <param name="requestContext">The context of the given (current) request.</param>
    /// <returns></returns>
    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var routeValues = requestContext.RouteData.Values;

        routeValues["action"] = routeValues["action"].UnDash();
        routeValues["controller"] = routeValues["controller"].UnDash();

        return base.GetHttpHandler(requestContext);
    }
}

等等

关于asp.net-mvc - AngularJS - 用户界面路由器 - MVC 5 - html5mode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26574631/

相关文章:

asp.net-mvc - ASP.NET MVC 在重写的 VirtualPathProvider 中禁用 View 缓存

c# - 获取asp.net mvc网站中的本地文件路径

html - 在 CSS 中将 Div 行转换为列

html - 如何将 anchor 链接到具有确切位置的新页面

angularjs - Firebase - limitToLast(5),然后加载以前的数据

JQuery - 在下拉菜单更改时查找最接近的文本框

asp.net-mvc - 在asp .net mvc 5中显示外部网站页面

html - 使用自定义 Bootstrap 导航栏,如何使移动版本看起来与桌面版本不同

AngularJS:如何使用多个实例定位特定指令

javascript - 如何从 $cookies 获取对象?