将帖子请求发送到C#app的Angular页面

时间:2016-05-06 07:59:41

标签: javascript c# angularjs http post

我知道如何使用angular向API发送post请求,但是想知道如何检索从应用程序发送的post请求。

假设我有这个C#帖子请求:

    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;
    }

在我的角度应用程序中,我有这个控制器:

(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();

          }]);

}());

我可以通过网址发送数据并使用routeParams,但我想使用POST代替。如何访问POST数据?

2 个答案:

答案 0 :(得分:0)

我认为你正在混淆前端和网络服务器技术。

Angular是一种前端技术,你的意思是将帖子发送到角度,实际上意味着向托管角网站的网络服务器发送请求。

一旦明确了这一点,您只需要向Web服务器发送一个API调用,就像对角度一样,服务器将返回您需要的json数据。

如果你想获得特定路线的HTML表示,我担心没有好办法实现这一点。这对于角度也不是特别的,任何使用ajax调用加载数据的站点都会出现这个问题。

答案 1 :(得分:0)

Angular $ 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;
        }
    }
}

这样,从angular发送请求以及请求中需要使用的任何数据。有利的是发送json,在你的业务逻辑中解析json。

使用新的有效负载创建一个HttpResponseMessage,以收取angular $ http。

相关问题