简体   繁体   English

AngularJs WebAPI找不到HTTP资源

[英]AngularJs WebAPI No HTTP resource was found

I'm developing an Angular app with WebAPI. 我正在使用WebAPI开发Angular应用程序。 When I call any method, i have the HTTP resource not found. 当我调用任何方法时,都找不到HTTP资源。 Angular call 角度通话

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 控制器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 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();
        }

    }
}

What is wrong? 怎么了? I think that the controllers name were the problem, but I changed without success. 我认为控制器名称是问题所在,但我没有成功就进行了更改。 FYI, web.config is avaible here: https://pastebin.com/aGcEVgdS 仅供参考,可在此处使用web.config: https ://pastebin.com/aGcEVgdS

It seems, you miss the config.MapHttpAttributeRoutes(); 看来,您错过了config.MapHttpAttributeRoutes(); configuration in WebApiConfig. WebApiConfig中的配置。 This configuration is required when you would like to try with attribute routing. 当您想尝试属性路由时,此配置是必需的。

In Web API, post action parameter retrieve data from FormBody and there at most one parameter allowed. 在Web API中,后操作参数从FormBody检索数据,并且最多允许一个参数。 So if you need more parameter value, you should declare an object and use that object as parameter. 因此,如果需要更多的参数值,则应声明一个对象并将该对象用作参数。

So for your case, you need to create an object as 因此,对于您的情况,您需要创建一个对象

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

Then your controller action should be 然后您的控制器动作应该是

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

Then your WebApiConfig configuration should be 然后,您的WebApiConfig配置应为

        // 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 }
        );

Please make sure config.MapHttpAttributeRoutes(); 请确保config.MapHttpAttributeRoutes(); will be placed before config.Routes.MapHttpRoute(......) 将放置在config.Routes.MapHttpRoute(......)之前

Then your post request send object instead of JSON.stringify. 然后,您的发布请求发送对象而不是JSON.stringify。

    var data = { email: email, password: password };
    $http.post(Url, data).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);
        });

Hope its working for you. 希望它为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM