具有自定义控制器的角度MVC路由

时间:2016-01-01 06:48:25

标签: angularjs asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing angularjs-routing

我有一个Angular MVC应用程序,它有几个控制器。默认和我添加的另一个自定义控制器。

http://example.com/home/

http://example.com/ManageSummaryInfo/

我的所有业务逻辑都在于ManageSummaryInfo控制器。

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

在Angular Routing中,我就是这样,

$routeProvider.when('/showsummary',
                        {
                            templateUrl: 'ManageSummaryInfo/ShowSummary',
                            controller: 'ShowSummaryController'
                        });
    $routeProvider.when('/showerror',
                        {                            
                            templateUrl: 'ManageSummaryInfo/ShowError',
                            controller: 'ShowErrorController'
                        });   
    $routeProvider.when('/showplatform',
                        {
                            templateUrl: 'ManageSummaryInfo/ShowPlatform',
                            controller: 'ShowPlatformController'
                        });

我的视图也围绕ManageSummaryInfo控制器配置,但是当我运行时,我进入主页,之后我点击其中一个元素应该带我到下一页。但是,我没有被路由,我得到404 - The resource cannot be found.错误。

这就是我的观点,

Views
--ManageSummaryInfo
----HomePage.cshtml
----Index.cshtml
----ShowSummary.cshtml
----ShowError.cshtml
----ShowPlatform.cshtml

我的问题是,当我们的路线中有控制器时(即http://example.com/ManageSummaryInfo/-,Angular如何处理事情以及为什么我的文件未找到错误。

我是C#MVC框架的新手。我错过了与ASP.NEt路由相关的东西吗?任何帮助将不胜感激。我曾尝试过精心设计,但如果您需要更多信息,我很乐意提供更多代码(Pastebin或其他内容。

提前致谢!

编辑:

根据请求添加控制器类

public class ManageSummaryInfoController : Controller
    {
        // GET: ManageSummaryInfo
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult HomePage()
        {
            return View();
        }

        public ActionResult ShowPlatform()
        {
            return PartialView("ShowPlatform");
        }

        public ActionResult ShowSummary()
        {
            return PartialView("ShowSummary");
        }

        public ActionResult ShowError()
        {
            return PartialView("ShowError");
        }
    }

1 个答案:

答案 0 :(得分:0)

1)在链接(一个元素)的视图(html代码)中,使用href showsummary(以及另一个)添加'#'之前' showsummary'。

2)检查你有

<script src=https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-route.min.js></script>

3)检查你是否依赖于ng-route:

angular.module('myModule', ['ngRoute']);

Angular路由只在html文件上工作(不在chtml上),所以改为创建html文件。

var adminApp = angular.module('adminApp', ['ngRoute']);

adminApp.config(['$routeProvider',
  function ($routeProvider) {
      $routeProvider.
        when('/ar', {
            templateUrl: 'auto_responder.html',
            controller: 'arCtrl'
        }).
        when('/broadcast', {
            templateUrl: 'broadcast.html',
            controller: 'broadcastCtrl'
        }).
        otherwise({

            redirectTo: '/ar'
        });
  }]);