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("[email protected]", "[email protected]","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;
}
2
  • show us the link you call, when you get the 404 - not found status Commented Aug 6, 2015 at 13:30
  • i test in visual studio not published website Commented Aug 6, 2015 at 13:37

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

try to comment [HttpPut]
What "HomeApi" is? Your action called "Login". Also user_maill should be without underscore.
0

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

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.