简体   繁体   中英

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.

This is my web Api Controller, having Post and Put method

// 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

  <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

 $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

 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. 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.

I believe that Put and Delete are not enabled verbs in MVC by default. To enable follow this post or this one

Is work no by adding this in 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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