0

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;
}
salah
  • 41
  • 2
  • 11

3 Answers3

1

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.

mironych
  • 2,938
  • 2
  • 28
  • 37
0

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

Community
  • 1
  • 1
sarin
  • 5,227
  • 3
  • 34
  • 63
  • Saw the WebDav comments. There are a number of other things to try, so do read those posts. One of the fixes will work for you. It has for others. – sarin Aug 06 '15 at 13:49
0

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>

salah
  • 41
  • 2
  • 11