如何解决是否可以在中间件Asp .Net Core中调用WebApi
我正在MVC Aps.Net Core项目中工作,我遇到这种情况:
用户 A 已登录设备 A ,而用户 B 试图登录设备 A 。我允许将用户 B 登录到设备 A ,但不会出现问题,但在这种情况下,我会向用户 A 显示该用户 A 中删除了strong> A 。
一切正常。我称其为具有sql函数的WebApi,该函数可以完成所有工作。问题是我的项目中的每个函数都具有相同的代码(称为WebApi)。因此,我正在考虑制作自定义中间件,这样就不必在项目中的每个函数/方法中都替换该代码。
这是我尝试过的:
public class Middleware
{
private readonly RequestDelegate _next;
string strBaseUrl = string.Empty;
string strMappaturaUrl = string.Empty;
private readonly IConfiguration config;
private readonly HttpClient client;
public Middleware(RequestDelegate next,IConfiguration _config,HttpClient _client)
{
_next = next;
client = _client;
config = _config;
strBaseUrl = config.GetValue<string>("AppSettings:BaseUrl");
strMappaturaUrl = config.GetValue<string>("AppSettings:MapUrl");
}
public async Task Invoke(HttpContext httpContext)
{
string returnErrore = string.Empty;
CheckUtenteDTO userRequest = new CheckUtenteDTO();
string strUrlApi = string.Empty;
string strContext = string.Empty;
try
{
userRequest.user = HttpContext.Session.GetString("nomeUten");
userRequest.macchina = HttpContext.Session.GetString("macchina");
//Here I call WebApi where I have sql functio
strUrlApi = config.GetValue<string>("AppSettings:BaseUrl") + "/Users/CheckLoginUser";
string stringData = JsonConvert.SerializeObject(userRequest);
var contentData = new StringContent(stringData,System.Text.Encoding.UTF8,"application/json");
using (var responseMessage = await client.PostAsync(strUrlApi,contentData))
{
if (responseMessage.IsSuccessstatusCode)
{
strContext = await responseMessage.Content.ReadAsstringAsync();
var strReturn = JsonConvert.DeserializeObject<string>(strContext);
if (string.IsNullOrWhiteSpace(strReturn))
returnErrore = string.Empty;
else
throw new UtenteException(strReturn);
}
else
{
strContext = await responseMessage.Content.ReadAsstringAsync();
throw new Exception(strContext);
}
}
}
catch (UserException ex1)
{
returnErrore = ex1.Message.Trim();
HttpContext.Session.SetString("strErrore",ex1.Message);
HttpContext.Session.SetString("nextAction","logoutAfterCheck");
HttpContext.Session.SetString("nextController","Login");
}
catch (Exception ex)
{
returnErrore = ex.Message.Trim();
HttpContext.Session.SetString("strErrore",ex.Message);
HttpContext.Session.SetString("nextAction","Index");
HttpContext.Session.SetString("nextController","HomeScreen");
}
return Json(returnErrore);
//return _next(httpContext);
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class MiddlewareExtensions
{
public static IApplicationBuilder UseMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<Middleware>();
}
}
}
我得到这个errors 。
是否可以通过这种方式进行? 任何建议如何解决此问题? 预先感谢!
解决方法
您的中间件似乎无权访问HttpContext
,因此错误。在这种情况下,您可以将上下文作为参数从表示层传递到中间件,在该层您可以访问HttpContext并调用中间件函数。
本质上,您仅在会话中使用以下两个参数..那么为什么不将它们提取到表示层中并将它们作为参数传递给中间件函数
userRequest.user = HttpContext.Session.GetString("nomeUten");
userRequest.macchina = HttpContext.Session.GetString("macchina");
将您的Invoke
方法签名更改为
Task Invoke(string user,string macchina) //assuming both are type string
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。