【问题标题】:Angular MVC Routing with Custom Controller带有自定义控制器的 Angular MVC 路由
【发布时间】:2016-04-05 21:10:19
【问题描述】:

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

http://example.com/home/

http://example.com/ManageSummaryInfo/

我所有的业务逻辑都依赖于ManageSummaryInfo Controller。

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

在 Angular 路由中,我有这样的,

$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.error。

这就是我的观点,

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

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

我是 C# MVC 框架的新手。我是否缺少与 ASP.NET Routing 相关的内容?任何帮助将不胜感激。我试图详细说明,但如果您需要更多信息,我很乐意提供更多代码(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");
        }
    }

【问题讨论】:

  • 你的 menageSummaryInfoController 是什么样子的?

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


【解决方案1】:

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'
        });
  }]); 

【讨论】:

  • 我已添加参考文献,先生。事实上,当我直接点击页面时,http://example.com/ManageSummaryInfo/HomePage 并从那里单击配置了 href 的 DOM 对象会将我带到http://example.com/ManageSummaryInfo/showsummary 页面。我如何使它与控制器无关,比如点击我的http://example.com/HomePage 并仍然对我的ManageSummaryInfoController 采取行动?
猜你喜欢
  • 2015-10-12
  • 2018-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
相关资源
最近更新 更多