Angularjs html5Mode页面刷新打破了asp.net的MVC路由 [英] Angularjs html5Mode page refresh breaks the routing in asp.net mvc

查看:235
本文介绍了Angularjs html5Mode页面刷新打破了asp.net的MVC路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的asp.net application.I执行angularjs路由有一个文件夹命名模板,包括3个html页面首页,关于,错误。
这里是我的app.js文件

I am performing angularjs routing in my asp.net application.I have a folder name template which consist of 3 html pages Home,About,Error. Here is my app.js file

 var app = angular.module('myApp', ['ngRoute','ngAnimate']);

app.controller('HomeController', function ($scope) {
    $scope.Message = "We are in home page";
});
app.controller('AboutController', function ($scope) {
    $scope.Message = "We are in about page";
});
app.controller('ErrorController', function ($scope) {
    $scope.Message = "404 page not found";
});

app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
    $routeProvider.
         when('/', {
             redirectTo: function () {
                 return '/home';
             }
         }).
        when('/home', {
            templateUrl: '/template/Home.html',
            controller: 'HomeController'
        }).
        when('/about', {
            templateUrl: '/template/About.html',
            controller: 'AboutController'
        }).
         when('/error', {
             templateUrl: '/template/Error.html',
             controller: 'ErrorController'
         }).
    otherwise({
        redirectTo: '/error'
    });
    $locationProvider.html5Mode(true);
}]);

Home.html中包括

Home.html consist of

<div ng-controller="HomeController">
<h1>Home Page</h1>
<h3>{{Message}}</h3>


在我Layout.cshtml文件我已经包括模块对myApp和基本属性。

In my Layout.cshtml file I have included the module myApp and base attribute.

   <!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <base href="/" />

机身拥有定义一些链接

Body has define some links

<body>
<div>
    <ul>
        <li><a href="/home">Home</a></li>
        <li><a href="/about">About</a></li>
        <li><a href="/erro">Invalid Url</a></li>
    </ul>
</div>
<div class="container">
    <div data-ng-view="" id="ng-view" class="slide-animation"></div>
</div>

路由工作正常,当我从家里浏览他人。但是,当我刷新页面左右它给人的资源没有发现error.For这个我也包括服务器端改写像这样在我的web.config文件。

Routing working fine when I navigate from home to others . But when I refreshes about page it gives resource not found error.For this I have also included server side rewrite like this in my web.config file.

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

不过问题仍然需要帮助解决它。谢谢你。

Still the problem remains need helping in solving it . Thanks.

推荐答案

我已经在Route.Config更改文件解决了这个问题。

I have solved it by making changes in Route.Config file

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

用此代替

 routes.MapRoute(
       name: "Application",
       url: "{*url}",
       defaults: new { controller = "Home", action = "Index" });

这篇关于Angularjs html5Mode页面刷新打破了asp.net的MVC路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆