2017-01-31 2 views
0

こんにちは、Web api2を使用し、angularjs経由で呼び出して1つのアプリケーションを開発しています。私はWeb API呼び出しを作成し、iisサーバー(パブリックIP)でホストしました。私は以下の形式でWeb api2メソッドにアクセスしています。api /コントローラ形式でWeb API呼び出しにアクセスできない

$http.post('http://111.93.133.98:4500/api/NCT_Login/', credentials).success(function (response) { alert(response); }); 

これは私のWeb APIのconfig.csファイルです。

routeTemplate: "api/{controller}/{id}" 

これは私のコントローラコードです。

public class NCT_LoginController : ApiController 
{ 
    public NCTEntities entityObject = new NCTEntities(); 
    [EnableCors(origins: "*", headers: "*", methods: "GET, POST, PUT, DELETE")] 

    public IHttpActionResult Post(LoginClass obj) 
    { 
     try 
     { 
      if (ModelState.IsValid) 
      { 
       obj.User_Password = PasswordEncryption.sha256_hash(obj.User_Password); 
       bool result = (from c in entityObject.NCT_UserRegistration where obj.User_Name ==c.User_Name && obj.User_Password == c.User_Password select c).Any(); 
       if(result==true) 
       { 

        obj.UserRole = (from c in entityObject.NCT_UserRegistration where obj.User_Name == c.User_Name && obj.User_Password == c.User_Password select c.User_Role).FirstOrDefault(); 
        obj.Success = 0; 
        obj.User_Password = ""; 
        var response = Request.CreateResponse(HttpStatusCode.OK, obj); 
        var newSessionId = new SessionIDManager().CreateSessionID(HttpContext.Current); 
        var cookie = new CookieHeaderValue("session-id", newSessionId); 
        cookie.Expires = DateTimeOffset.Now.AddDays(1); 
        cookie.Domain = Request.RequestUri.Host; 
        cookie.Path = "/"; 
        response.Headers.AddCookies(new[] { cookie }); 
        return ResponseMessage(response); 
       } 
       else 
       { 
        return Content(HttpStatusCode.BadRequest, 1); 
       } 

      } 
      else 
      { 
       return Content(HttpStatusCode.BadRequest, 1); 
      } 
     } 
     catch (DbEntityValidationException e) 
     { 
      foreach (var eve in e.EntityValidationErrors) 
      { 
       Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", 
        eve.Entry.Entity.GetType().Name, eve.Entry.State); 
       foreach (var ve in eve.ValidationErrors) 
       { 
        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", 
         ve.PropertyName, ve.ErrorMessage); 
       } 
      } 
      throw; 
     } 
    } 

私はAPIを利用することができる午前ルートテンプレートからAPIを削除し、私はapi.NCT_Loginを置くならば、私は、プリフライトエラーを取得しています。私はここで何が欠けているのか分かりません。どんな助けもありがとう。ありがとうございました。

+0

完全コントローラクラスを共有してください –

+0

ありがとう。私は更新しました。 –

+0

コントローラーのクラス名も必要です。 –

答えて

0

私はそれがどのような各方法は非常に明確になるように、これはIMOその良い練習がattribute routingを使用するapi/NCT_Login/Login

へのルートを設定します。この

[RoutePrefix("api/NCT_Login")] 
public class NCT_LoginController : ApiController 
{ 
    public NCTEntities entityObject = new NCTEntities(); 
    [EnableCors(origins: "*", headers: "*", methods: "GET, POST, PUT, DELETE")] 

    [Route("Login")] 
    [HttpPost] 
    public IHttpActionResult Post(LoginClass obj) 
    { 

のような属性のルートでPOSTメソッドを飾るだろうルートはです。 コントローラが同じhttp動詞で新しいルートを簡単に定義できるようになったとき

+0

彼がapi configで属性ルーティングを有効にしていることを確認し、 'api/NCT_Login /'自体にPOSTするようにLoginアクションに '[Route()]'をつけることができます:) – Developer

+0

ありがとう。だから私は以下のように打つことができます。 http://111.93.133.98:4500/api/NCT_Login/Login。 –

+0

@developerはデフォルトでWeb API 2の属性ルーティングを有効にしていませんか? –

関連する問題