【问题标题】:MVC and angularJs routing conflictMVC 和 angularJs 路由冲突
【发布时间】:2015-12-11 12:36:41
【问题描述】:

在我的 MVC 应用程序中,我有一个家庭控制器和一个索引方法。我已将索引方法设置为 RouteConfig 文件中的默认路由。

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

我在我的应用程序中使用 Angular JS。我已经使用 $http 在家庭控制器中定义了一个服务并调用了一个操作“集群”。

app.service('homeService', function ($http) {

    this.getClusters = function () {
        var promise = $http.get("Clusters");
        return promise;
    };
});

当我运行项目并转到默认 url [http://localhost:56698/] 然后http://localhost:56698/Clusters 404 not found 发生异常。但如果 url 是http://localhost:56698/Home/Index,它就可以工作。所以我的 Angular js 服务在默认 url 中不起作用。如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    我认为您的 javascript 参考有问题。 你应该像这样使用相对引用:

    <script src="@Url.Content("~/Scripts/angular.js")" type="text/javascript"></script>
    

    或者您应该更改您的集群调用:(我假设您的 javascript 代码位于您的视图中)

     this.getClusters = function () {
        var promise = $http.get('@Url.action(Clusters,Your controller name)');
        return promise;
    };
    

    【讨论】:

    • 感谢您的回答。但它不起作用。对角度脚本的引用在两个 url 中都有效。但在默认 url [localhost:56698/]. 中找不到 url 路径“Cluster”
    • @NOBLEM.O.:欢迎您。我根据您的评论编辑了我的答案。如果新答案不起作用,请通知我。
    • 谢谢pooria taghizadeh。该脚本不在视图文件中。所以我不能使用@Url 助手。我按照您的建议在脚本中指定了控制器名称/操作名称。有效。谢谢。
    • @NOBLEM.O.你欢迎我的朋友。很高兴为您提供帮助
    猜你喜欢
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多