404未发现 - 消费由angularjs网络API [英] 404 Not found - consume the Web API by angularjs

查看:104
本文介绍了404未发现 - 消费由angularjs网络API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的控制器code:

I have my controller code:

 [HttpGet]
    [ActionName("Email")]
    public HttpResponseMessage GetByEmail(string email)
    {
        Users user = db.Users.Find(email);
        if (user == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }
        return Request.CreateResponse(HttpStatusCode.OK, user);
    }

和我WebApiConfig:

and my WebApiConfig:

    public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();

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

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

终于我的js code

and finally my js code

$http.get('/api/users/Email/' + NewUser.Email)      
         .success(function (response) {
             console.log(response);                  // prints 404 not found 
         });

问题是,我得到响应(404未找​​到)与搜索电子邮件尽管它在用户表中的电子邮件领域的存在。

The problem is that I get response (404 not found ) for the searched email although it exists in the email field in the Users table.

推荐答案

我已经解决了它:

    public Users Get(string Email)
    {
        Users user = db.Users.Find(Email);
        if (user == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }
        return user;
    }

无需溃败

这篇关于404未发现 - 消费由angularjs网络API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆