简体   繁体   English

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

[英]c# 'HttpContext' does not contain a definition for 'Current', Could not read posted values on my web application

I am creating a single page web application which will handle the posted data from microsoft azure webhook.我正在创建一个单页 web 应用程序,它将处理来自 microsoft azure webhook 的发布数据。 I have created a Core web application and got prebuild files to run it on IIS.我已经创建了一个核心 Web 应用程序并获得了预构建文件以在 IIS 上运行它。 The issue is that i am unable to read the Posted values/Get values in my app.问题是我无法在我的应用程序中读取发布值/获取值。 Here is my code in the startup.cs file这是我在 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"]);
            });
        }

    }
}

Put this inside your Configure method将其放入您的 Configure 方法中

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

I have retreive the query string value like this我已经像这样检索查询字符串值

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

And the posted field to this page/API could not retrieve with context.Response.Query or context.Response.Form.并且无法使用 context.Response.Query 或 context.Response.Form 检索到此页面/API 的发布字段。 To achieve that i need to convert my project into web api type which sets up initial project with a sample controller i have implement my post method function in that sample controller.为了实现这一点,我需要将我的项目转换为 web api 类型,它使用示例控制器设置初始项目,我在该示例控制器中实现了我的 post 方法函数。 the code is below代码在下面

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 网络核心:“ HttpContext”不包含“当前”的定义 - Net Core: 'HttpContext' does not contain a definition for 'Current'` HttpContext.Request.Form在MVC中不包含发布的值 - HttpContext.Request.Form does not contain posted values in MVC HttpContext不包含Current的定义 - HttpContext doess not contain definition for Current 我使用“HttpContext.Current”,但调试时出现错误“HttpContext”不包含“Current”的定义 - I use “HttpContext.Current” but debug got error “HttpContext' does not contain a definition for 'Current” “IApplicationBuilder”不包含“HttpContext”的定义 - 'IApplicationBuilder' does not contain a definition for 'HttpContext' c# Web App - Encoding' 不包含 'UTF8 的定义 - c# Web App - Encoding' does not contain a definition for 'UTF8 有没有一种方法可以使用web.config文件在c#应用程序中更新HttpContext.Current.User.Identity.Name? - Is there a way to update HttpContext.Current.User.Identity.Name in an c# application using web.config file? HttpContext.GetOwinContext() 不包含 GetOwinContext() 的定义吗? - HttpContext.GetOwinContext() does not contain definition for GetOwinContext()? c# 不包含“InitializeComponent”的定义,名称“controlName”在当前上下文中不存在 - c# does not contain a definition for "InitializeComponent" and name "controlName" does not exist in current context 是什么导致我的httpcontext.cache被删除在我的c#MVC Web应用程序中? - What is causing my httpcontext.cache to be removed in my c# MVC web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM