简体   繁体   English

angularjs Web API发布作品,但找不到404

[英]angularjs web api post works but put 404 not found

I am beginner in Angularjs i have post method to register its works fine and have put method for login retutn 404 not found. 我是Angularjs的初学者,我有post方法可以很好地注册其工作,并且没有找到用于登录retutn 404的put方法。

This is my web Api Controller, having Post and Put method 这是我的Web Api控制器,具有Post和Put方法

// POST api/StudentsAPI 
    //[EnableCors(origins: "*", headers: "*", methods: "*")]
    // [ActionName("register")]
    // [HttpPost]
    public HttpResponseMessage PostRegister(Users Student)
    {
        if (ModelState.IsValid)
        {
            Random rnd = new Random();
            int card = rnd.Next(52);
            Student.user_id = card;
            // _usertManager.AddUser(Student);
            var activateToken = WebSecurity.CreateUserAndAccount(Student.user_mail, Student.password, Student);
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, Student);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = Student.user_id }));
            //  _mailManager.Sendmail("salah.rzzaz90@gmail.com", "salahsayedrzzaz@gmail.com","dd","dd");
            return response;
        }
        else
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }
    ////[EnableCors(origins: "*", headers: "*", methods: "*")]
    //[ActionName("Login")]
    [HttpPut]
    public HttpResponseMessage PutLogin(string userMaill, string passwordd)
    {
        if (ModelState.IsValid)
        {
            _usertManager.Login(userMaill, passwordd);
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, userMaill);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { userMaill = userMaill }));
            return response;
        }
        else
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }

Edit : before this error other error apear that put method not allowed i fix this by adding this code in web.config 编辑:在此错误之前出现其他错误,不允许放置方法,我通过在web.config中添加此代码来解决此问题

  <system.webServer>
<modules>
  <remove name="WebDAVModule" />
</modules>
<handlers>
  <remove name="WebDAV" />
</handlers>
<security>
  <requestFiltering>
    <verbs allowUnlisted="false">
      <add verb="GET" allowed="true" />
      <add verb="POST" allowed="true" />
      <add verb="DELETE" allowed="true" />
      <add verb="PUT" allowed="true" />
    </verbs>
  </requestFiltering>
</security>

Controller.js Controller.js

 $scope.login = function () {
    var LoginDto = {
        user_maill: $scope.user_maill,
        passwordd: $scope.passwordd,


    };


    var promisePost = CRUD_OperService.put(LoginDto);
    promisePost.then(function (pl) {

        GetAllRecords();
        $scope.Message = "done";
        ClearModels();
    }, function (err) {
        console.log("Err" + err);
    });

};

}); });

service.js service.js

 this.put = function (LoginDto) {
    var request = $http({
        method: "put",
        url: "/api/HomeApi",
        data: LoginDto
    });
    return request;
}

The issue is with API controller and not in Angular. 问题出在API控制器,而不是Angular。 Try to do something like this: 尝试做这样的事情:

public class LoginDto
{
 public string UserMail { get; set; }
 public string Password { get; set; }
}  


[HttpPut]
public HttpResponseMessage PutLogin(LoginDto loginDto)
{
  // use loginDto
}

You also can use [FromBody] attribute, but DTO is better way to do that. 您也可以使用[FromBody]属性,但是DTO是更好的方法。

I believe that Put and Delete are not enabled verbs in MVC by default. 我相信默认情况下,MVC中未启用“放置”和“删除”动词。 To enable follow this post or this one 要启用此帖子这篇 帖子

Is work no by adding this in web.config 通过在web.config中添加它来解决问题

<system.webServer>
<modules runAllManagedModulesForAllRequests="false">
  <remove name="UrlRoutingModule" />
  <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" preCondition="" />

</modules>
<handlers>
  <remove name="WebDAV" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,POST,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<security>
  <requestFiltering>
    <verbs allowUnlisted="false">
      <add verb="GET" allowed="true" />
      <add verb="POST" allowed="true" />
      <add verb="DELETE" allowed="true" />
      <add verb="PUT" allowed="true" />
    </verbs>
  </requestFiltering>
</security>

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

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