c#'HttpContext'不包含'Current'的定义,无法读取我的Web应用程序中发布的值

问题描述 投票:-2回答:2

我正在创建一个单页Web应用程序,该应用程序将处理从Microsoft Azure Webhook发布的数据。我创建了一个Core Web应用程序,并获取了预构建文件以在IIS上运行它。问题是我无法在我的应用中读取已发布的值/获取值。这是我在startup.cs文件中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Web.Http;
using System.Net.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace office365notification
{
    public class Startup
    {

        public void ConfigureServices(IServiceCollection services)
        {
        }

        public class User
        {
            public double id { get; set; }
            public string email { get; set; }
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Run(async (context) =>
            {
                var queryVals = HttpContext.Current.Request.RequestUri.ParseQueryString();
                await context.Response.WriteAsync(queryVals["id"]);
            });
        }

    }
}
c# asp.net azure-active-directory http-post webhooks
2个回答
1
投票

将其放入您的Configure方法中

app.Use(async (context, next) =>
        {
            // Here you should have the context.
            await next.Invoke();
        });

-1
投票

我已经检索了这样的查询字符串值

app.Run(async (context) =>
{
   await context.Response.WriteAsync(context.Request.QueryString.Value);
});

并且此页面/ API的发布字段无法使用context.Response.Query或context.Response.Form进行检索。为了实现这一点,我需要将我的项目转换为使用示例控制器设置初始项目的Web api类型,我已经在该示例控制器中实现了post方法功能。代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.IO;

namespace webAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value1", "value2" };
        }

        [HttpPost]
        public string Post(User user)
        {
            string userId = user.id;
            string userEmail = user.email;
            string msg = "There is no posted data!";
            if (!string.IsNullOrEmpty(userId) && !string.IsNullOrEmpty(userEmail))
            {
                WriteToLogFile("User id : " + userId + "\n" + ", User Email : " + userEmail + "\n");
                msg = "Posted data added successfully!";
            }
            return msg;
        }
    class User {
        public string id { get; set; }
        public string email { get; set; }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.