将原始发布数据保存到asp.net中的文件

时间:2016-04-20 08:44:16

标签: c# asp.net

这是我第一次使用stackoverflow。我在php中创建了此脚本,以将原始发布数据保存到文件中。我只会发布最重要的部分:

<?php
$jsonString = file_get_contents("php://input");
$jfile = rand(100000,999999);
file_put_contents($jfile,$jsonString);
?>

我需要将此脚本转换为asp.net,但我对.net知之甚少,我搜遍了所有内容,但找不到任何内容。我还需要知道客户端如何发送数据。

这样的事情?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace WebApplication1.Controllers
    {
        public class HomeController : Controller
        {
            public HttpResponseMessage Post(string person)
            {
                System.IO.File.WriteAllText(@"C:\Users\Public                \TestFolder        \WriteText.txt", person);
                return Request.CreateResponse(HttpStatusCode.OK);
            }
        }
    }
    }

1 个答案:

答案 0 :(得分:0)

这是一个很好的例子,简而言之:

https://msdn.microsoft.com/en-GB/library/8bh11f1k.aspx

为了在ASP.NET中使用代码,您需要创建一个ASP.NET应用程序(可能是Web Api或MVC)。看一下本教程:

http://www.w3schools.com/aspnet/mvc_app.asp

要回答关于从客户端发送数据的其他问题,这里有一组基于使用jQuery的示例(我假设客户端是指在浏览器中运行的应用程序):

http://api.jquery.com/jquery.post/

<强>编辑:

这是一个从客户端向WebApi服务发送数据的简单示例

您的客户使用jQuery的post方法发送数据:

$("button").click(function(){
    $.post("http://myservicehost/api/Persons",
    {
        name: "Donald Duck",
        city: "Duckburg"
    },
    function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });
});

您的WebApi控制器收到它:

public HttpResponseMessage Post(string person)
{
    //do whatever you want to do with this person
    //
    //
    return Request.CreateResponse(HttpStatusCode.OK);
}
相关问题