[英]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.