c# - 在 ASP.NET MVC 中获取负载

标签 c# asp.net asp.net-mvc request

我想读取 global.asax 文件中的有效负载。 Chrome 中的有效负载类似于 this .

当我想在 global.asax 中读取它时,我得到的 Request.InputStream.Length 为 610。当我将其复制到 MemoryStream 时, memstream 切换到 0。

public void Application_BeginRequest(object sender, EventArgs e)
    {
        MemoryStream memstream = new MemoryStream();
        Request.InputStream.CopyTo(memstream);
        memstream.Position = 0;
        using (StreamReader reader = new StreamReader(memstream))
        {
            string text = reader.ReadToEnd();
        }
    }

所以最后,text 是空的。

最佳答案

为什么需要将输入流复制到另一个输入流,我不知道,但你可以简单地获取它;

Request.InputStream.Seek(0,SeekOrigin.Begin);
using (Stream receiveStream = Request.InputStream)
{
    using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
    {
        var text = readStream.ReadToEnd();
    }
}

此外,我不知道您的要求,但这不是处理请求的正确方法。我建议您在操作中获取请求正文。

关于c# - 在 ASP.NET MVC 中获取负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48377672/

相关文章:

c# - 使用 linq with join 在两个表上使用两个 where

c# - 如何更新 CSV 文件中的行?

c# - 在 ASP.NET 网站中使用 Dll

c# - 无法在 ASP.NET 中转换类型为 System.Web.UI.WebControls.GridView 的对象

.net - 绕过获取请求的 MVC 验证

jquery - 在点击事件 jQuery 中使用 @Url.Action 和参数

c# - 在 C# 中创建一个具有许多要比较的变量的 equals 函数

c# - LINQ:子 IEnumerable 的平均值

asp.net - 使用 RsaProtectedConfigurationProvider 进行 Web.Config 加密 - "Bad Data"错误

c# - SQLCommand批量更新查询