Angular $ http.get为我的ASP.NET操作返回404?

时间:2014-09-28 17:14:29

标签: asp.net-mvc angularjs

我有以下angularjs代码:

  $http.get("/Home/GetEmails").then(function (response) {
            $scope.emails = response.data;
            $scope.init($scope.emails);
            $scope.totalEmails = $scope.emails.length;
        });

当我在本地开发时,它工作正常,但是当我发布到实时服务器时,它会显示以下错误消息:

Failed to load resource: the server responded with a status of 404 (Not Found)。它正在寻找http://xaisoft.com/Home/GetEmails,但它无法找到它。在ASP.NET MVC和/或Angular中我还需要做些什么才能使其工作。目前,我只是让GetEmails操作在我的HomeController中返回一个JSON对象。

HomeController

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult GetEmails()
    {
        return Json(new[]
        {
            new
            {
                from = "Jet Blue",
                to = "Me",
                subject = "Buy one seat, get one free!",
                body = "That and helping Data finish 'Pop Goes the Weasel'. Also counts as a CMOH.",
                date = "Dec 20th 12:22 PM",
                isImportant = false,
                isStarred = true,
                isChecked = false,
                isRead = true
            },
            new
            {
                from = "Publix",
                to = "Me",
                subject = "Check this weeks BOGO deals",
                body = "Hurry, ends Thursday!",
                date = "Mar 15th 8:15 AM",
                isImportant = false,
                isStarred = false,
                isChecked = false,
                isRead = false
            },
            new
            {
                from = "AJ Carpio",
                to = "Me",
                subject = "When Life Gives You Questions, Google has Answers",
                body = "Get more life quotes here",
                date = "Mar 15th 8:15 AM",
                isImportant = true,
                isStarred = false,
                isChecked = false,
                isRead = true
            }
        },JsonRequestBehavior.AllowGet);
    }

}

1 个答案:

答案 0 :(得分:2)

您不应该像在以下那样对ASP.NET MVC应用程序中的URL进行硬编码:

$http.get("/Home/GetEmails")

您应该始终使用网址助手。例如,在您的视图中,您可以设置变量:

<script type="text/javascript">
    var getEmailsUrl = @Url.Action("GetEmails", "Home");
</script>

您以后可以在您的ng脚本中使用

$http.get(getEmailsUrl)

诸如Url.Action之类的Url助手的目的是考虑托管环境虚拟目录和路由配置等内容。所有这些因素都可能在各种环境之间发生变化,如果您在javascript中对网址进行硬编码,那么您将获得大量的404.


这是一种更有棱角的方式,而不是注册一个全局的javascript变量。您可以使用$provide

<script type="text/javascript">
    angular.module('myApp').config(['$provide', function ($provide) {
        // Here you can also register a complex javascript object that will serve
        // as configuration for your app and can contain multiple urls

        $provide.value('getEmailsUrl', '@Url.Action("GetEmails", "Home")');
    }]);
</script>

然后您可以在控制器和服务中注入getEmailsUrl

app.controller('HomeController', ['$scope', 'getEmailsUrl', function ($scope, getEmailsUrl) {
    // You can use the getEmailsUrl variable here.
}]);