从C#应用程序向Angular页面发送帖子请求
Send post request to Angular page from C# app
我知道如何将带有angular的post请求发送到API,但不知道如何检索从应用程序发送的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;
}
在我的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();
}]);
}());
我可以通过url发送数据并使用routeParams,但我想使用POST。如何访问POST数据?
我认为您混淆了前端和web服务器技术。
Angular是一种前端技术,向Angular发送帖子实际上意味着向托管Angular网站的web服务器发送请求。
一旦你弄清楚了这一点,你只需要像处理angular一样向web服务器发送一个API调用,服务器就会返回你需要的json数据。
如果你想获得特定路由的HTML表示,恐怕没有什么好的方法可以实现。angular也不例外,任何使用ajax调用加载数据的站点都会遇到这个问题。
您可以通过以下方式从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。
为angular$http创建一个具有新有效负载的HttpResponseMessage
以接收。
相关文章:
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- 在openshift node js应用程序中获取请求
- 从C#应用程序向Angular页面发送帖子请求
- 只需添加一个'允许跨来源请求'到我的节点应用程序工作
- 如何让应用程序帮助程序方法在发送的请求为 JS 格式时工作
- 在安装后首次运行应用程序时从 UIBebView 发出 jQuery Ajax 请求
- 读取Angular应用程序中请求的url
- 为什么不't我的Facebook应用程序发送邀请请求
- 当我们打开ADF应用程序的同一网页的不同版本时,如何让浏览器请求java脚本
- 在angular cordova应用程序中发布一个请求中的多个文件
- Chrome应用程序中AJAX请求的限制是什么
- 在express应用程序中,请求参数两次附加到URL
- 如何在同构应用程序中通过节点提取传递请求cookie
- Facebook应用程序未请求manage_pages许可
- 我正在用apachecordova开发一个应用程序,但每次我向局域网上的服务器发出ajax请求时,我都会收到这个错误
- 使用沙箱从Chrome应用程序发出ajax请求
- Facebook Graph API-#4错误-已达到应用程序请求限制
- Ajax get jsonp给出错误代码500“;请求被阻止”;来自MVC应用程序
- HTTP 请求后,数据未显示在 Ember 应用程序中
- 发送请求2.0应用程序生成的请求到非"/me"