AngularJs WebAPI未找到HTTP资源

时间:2017-12-04 16:02:47

标签: angularjs asp.net-web-api

我正在使用WebAPI开发Angular应用程序。当我调用任何方法时,我找不到HTTP资源。 角度呼叫

service.Login = function (email, password, callback) {
        var Url = Constants.apiDevUrl + 'api/login/GetLoginByEmailPassword';

        $http.post(Url, JSON.stringify({ email: email, password: password })).then(
            function (response) {
                var data = response.data
                callback(data);
            }).catch(function (error) {
                console.log('ERROR GetLoginByEmailPassword:');
                console.log(error.config.url);
                console.log(error.data.Message);
                console.log(error.data.MessageDetail);
                console.log(error.status);
                console.log(error.statusText);
            });
    };

Controller webAPI

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Esox_Elo_Calculator.API
{
    [RoutePrefix("api/login")]
    public class LoginController : ApiController
    {
        [Route("GetLoginByEmailPassword")]
        [HttpPost]
        public LoggedUser GetLoginByEmailPassword(string email, string password)
        {
            return new BOLogin().GetLoginByEmailPassword(email, password);
        }
    }
}

WebApiConfig

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace Esox_Elo_Calculator
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            config.Formatters.Remove(config.Formatters.XmlFormatter);

            config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
            config.Formatters.JsonFormatter.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
            config.Formatters.JsonFormatter.SerializerSettings.DateFormatString = "yyyy-MM-ddTHH:mm:ssK";

            config.Routes.Clear();
            config.Routes.MapHttpRoute(
                name: "api",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.MapHttpAttributeRoutes();
        }

    }
}

有什么问题?我认为控制器名称是问题,但我没有成功改变。仅供参考,web.config可在此处获取:https://pastebin.com/aGcEVgdS

1 个答案:

答案 0 :(得分:0)

看来,你错过了config.MapHttpAttributeRoutes(); WebApiConfig中的配置。如果您想尝试使用属性路由,则需要此配置。

在Web API中,post action参数从FormBody中检索数据,并且最多只允许一个参数。因此,如果您需要更多参数值,则应声明一个对象并将该对象用作参数。

因此,对于您的情况,您需要创建一个对象

  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for XDebug",
      "type": "php",
      "request": "launch",
      "port": 9000
    },
    {
      "name": "Launch currently open script",
      "type": "php",
      "request": "launch",
      "program": "${file}",
      "cwd": "${fileDirname}",
      "port": 9000
    },
    {
      "name": "debug FRAaap",
      "type": "php",
      "request": "launch",
      "program": "${file}",
      "cwd": "C:\\Users\\chadmeyers\\Sites\\aaAppDev\\public_html\\",
      "port": 9000
    }
  ]
}

然后您的控制器操作应为

public class UserModel
{
    public string email { get; set; }
    public string password { get; set; }
}

然后您的WebApiConfig配置应为

    [Route("GetLoginByEmailPassword")]
    [HttpPost]
    public LoggedUser GetLoginByEmailPassword(UserModel user)
    {
        return new BOLogin().GetLoginByEmailPassword(user.email, 
user.password);
    }

请确保config.MapHttpAttributeRoutes();将放在config.Routes.MapHttpRoute(......)

之前

然后你的帖子请求发送对象而不是JSON.stringify。

        // Web API configuration and services
        config.Formatters.Remove(config.Formatters.XmlFormatter);


config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = 
Newtonsoft.Json.DateTimeZoneHandling.Local;

       config.Formatters.JsonFormatter.SerializerSettings.DateFormatHandling 
= Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
        config.Formatters.JsonFormatter.SerializerSettings.DateFormatString 
= "yyyy-MM-ddTHH:mm:ssK";

        config.Routes.Clear();
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "api",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

希望它为你工作。