2016-05-06 10 views
2

私はAPIを使って投稿要求を送る方法を知っていますが、アプリケーションから送信された投稿要求を取り出す方法は不思議でした。私は、URLや使用routeParamsを介してデータを送信することができますC#アプリケーションからAngularページへの投稿要求を送信

(function() { 
    'use strict'; 

    angular.module('anbud') 
     .controller('editController', ['$scope', 'localStorage', 'md5', '$routeParams', 'httpError', 
      function ($scope, localStorage, md5, $routeParams, httpError) { 

       function processHeaderData() { 
        console.log($routeParams.json); 
       } 

       processHeaderData(); 

      }]); 

}()); 

、私:私はこのコントローラを持っている私の角度のアプリで

public string sendPost(string data) 
    { 
     // Create a request using a URL that can receive a post. 
     WebRequest request = WebRequest.Create("http://localhost:5000/app/edit"); 
     // Set the Method property of the request to POST. 
     request.Method = "POST"; 
     // Create POST data and convert it to a byte array. 

     string postData = "{\"data\":\"" + data + "\"}"; 

     byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
     // Set the ContentType property of the WebRequest. 
     request.ContentType = "application/json"; 
     // Set the ContentLength property of the WebRequest. 
     request.ContentLength = byteArray.Length; 
     // Get the request stream. 
     Stream dataStream = request.GetRequestStream(); 
     // Write the data to the request stream. 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     // Close the Stream object. 
     dataStream.Close(); 
     // Get the response. 
     WebResponse response = request.GetResponse(); 
     // Display the status. 
     Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
     // Get the stream containing content returned by the server. 
     dataStream = response.GetResponseStream(); 
     // Open the stream using a StreamReader for easy access. 
     StreamReader reader = new StreamReader(dataStream); 
     // Read the content. 
     string responseFromServer = reader.ReadToEnd(); 
     // Display the content. 
     Console.WriteLine(responseFromServer); 
     // Clean up the streams. 
     reader.Close(); 
     dataStream.Close(); 
     response.Close(); 

     return responseFromServer; 
    } 

レッツは、私はこのC#のポスト要求を持っていると言います代わりにPOSTを使用したいと考えています。 POSTデータにアクセスするにはどうすればよいですか?

答えて

0

私はあなたがフロントエンド技術とウェブサーバー技術を混在させていると思います。

角度はフロントエンドの技術です。投稿を角度に送信するとは、実際には角度のあるウェブサイトをホストするWebサーバーにリクエストを送信することを意味します。

これを明確にしたら、角度を使って行うようにWebサーバーにAPI呼び出しを送信するだけで済みます。サーバーは必要なjsonデータを返します。

特定のルートのHTML表現を取得したい場合、これを実現する良い方法はないと思います。これは角にも特に問題はなく、ajax呼び出しでデータをロードするサイトでこの問題が発生します。

0

角度$ http.postは、データを取得することもできます。セキュリティ上の理由から、これは非常に有益です。論理は同じままです。

あなたはこのを通して、あなたのC#のコントローラからデータを送信することもできます。これにより

using System.Net; 
using System.Net.Http; 
using System.Web.Http; 

namespace Sample.API.Controllers 
{ 
    public class ContentDataController 
    { 

     [Route("api/ContentData")] 
     [HttpPost] 

     public HttpResponseMessage SamplePost(SampleRequest request) 
     { 
      HttpResponseMessage response; 
      try 
      { 
       var cda = dataContentAdapter(); 
       //Business logic here 
       var result = cda.GetContent(request); 

       //pass the above result to angular 
       response = Request.CreateResponse(HttpStatusCode.OK, result); 
       response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300)); 
      } 
      catch (Exception ex) 
      { 
       ApplicationLogger.LogCompleteException(ex, "ContentDataController", "Post"); 
       HttpError myCustomError = new HttpError(ex.Message) { { "IsSuccess", false } }; 
       return Request.CreateErrorResponse(HttpStatusCode.OK, myCustomError); 
      } 
      return response; 
     } 
    } 
} 

は、リクエストで使用する必要がどのようなデータで角度からのリクエストを送信します。有利なのは、jsonを送信し、ビジネスロジックのjsonを解析することです。

角$ httpを受信する新しいペイロードを使用してHttpResponseMessageを作成します。

関連する問題