angularjs web api post工作但是找不到404

时间:2015-08-06 13:25:12

标签: c# angularjs model-view-controller asp.net-web-api

我是Angularjs的初学者我有post方法来注册它的工作正常并且已经找不到登录retutn 404的方法。

这是我的网络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);
        }
    }

编辑:在此错误之前的其他错误apear,put方法不允许我通过在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;
}

3 个答案:

答案 0 :(得分:1)

问题在于API控制器而不是Angular。 尝试做这样的事情:

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


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

您也可以使用[FromBody]属性,但DTO是更好的方法。

答案 1 :(得分:0)

我认为默认情况下,Put和Delete不是MVC中启用的动词。要启用关注this postthis one

答案 2 :(得分:0)

通过在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>