c# 'HttpContext' 不包含 'Current' 的定义,无法读取我的 Web 应用程序上发布的值
c# 'HttpContext' does not contain a definition for 'Current', Could not read posted values on my web application
我正在创建一个单页 Web 应用程序,它将处理来自 Microsoft Azure Webhook 的发布数据。
我已经创建了一个核心 Web 应用程序,并在 IIS 上将预构建文件 运行 它。问题是我无法在我的应用程序中读取已发布的 values/Get 值。这是我在 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"]);
});
}
}
}
将其放入您的 Configure 方法中
app.Use(async (context, next) =>
{
// Here you should have the context.
await next.Invoke();
});
我已经像这样检索了查询字符串值
app.Run(async (context) =>
{
await context.Response.WriteAsync(context.Request.QueryString.Value);
});
并且 posted 字段到此 page/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; }
}
}
我正在创建一个单页 Web 应用程序,它将处理来自 Microsoft Azure Webhook 的发布数据。 我已经创建了一个核心 Web 应用程序,并在 IIS 上将预构建文件 运行 它。问题是我无法在我的应用程序中读取已发布的 values/Get 值。这是我在 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"]);
});
}
}
}
将其放入您的 Configure 方法中
app.Use(async (context, next) =>
{
// Here you should have the context.
await next.Invoke();
});
我已经像这样检索了查询字符串值
app.Run(async (context) =>
{
await context.Response.WriteAsync(context.Request.QueryString.Value);
});
并且 posted 字段到此 page/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; }
}
}