小编Yon*_*hun的帖子

如何激活中间件

我必须在其他地方激活它还是你可以帮助我?当我运行该项目时,它直接进入控制器并且中间件不运行。

这是我的中间件。

public class AuthenticationMiddleWare
{
    private RequestDelegate nextDelegate;
        
    public AuthenticationMiddleWare(RequestDelegate next, IConfiguration config)
    {
        nextDelegate = next;
    }
        
    public async Task Invoke(HttpContext httpContext)
    {
        try
        {
            // somecode
            await nextDelegate.Invoke(httpContext);
        
        }
        catch (Exception ex)
        {
        
        }
    }    
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core asp.net-core-middleware asp.net-core-webapi

1
推荐指数
1
解决办法
825
查看次数

MongoDB - 查找包含空格的值

我有以下样本文档:

[
    { _id: "1", value: " 1" },
    { _id: "2", value: "2" },
    { _id: "3", value: "3 " }
]
Run Code Online (Sandbox Code Playgroud)

如何运行 MongoDBfind函数来返回值包含空格的文档?因此,在这种情况下,它将返回 1,因为它的开头有一个空格,而返回 3,因为它的末尾有一个空格。

mongodb mongodb-query

1
推荐指数
1
解决办法
2529
查看次数

将 3 个 IEnumerable<Application> 列表合并为一个

我已经简化了我的问题

public class Application
{
    public int Id { get; set; }
    public int Version { get; set; }
    public int Status { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的 3 个清单:

IEnumerable<Application> applications1 = new List<Application>
{
    new Application {Id = 1, Version = 1, Status = 1},
    new Application {Id = 2, Version = 1, Status = 1},
    new Application {Id = 3, Version = 3, Status = 1}
};

IEnumerable<Application> applications2 = new List<Application>
{
    new Application {Id = …
Run Code Online (Sandbox Code Playgroud)

c# linq ienumerable merge

1
推荐指数
1
解决办法
91
查看次数

如何从 Razor 页面返回 HTTP 状态代码 500 的 JsonResult?

在我的 ASP.NET Core Razor Pages 应用程序中,我有时会通过 ajax 向服务器发送请求。

在这些情况下,我想返回JsonResult成功或错误。

但我找不到更改JsonResult.

这是我的方法:

public IActionResult OnPostFileUpload()
{
   try
   {
       // code to manage and save the file, this request is AJAX
       return new JsonResult("done");
   }
   catch (Exception ex)
   {
       return new JsonResult(message); // how should I set it to be 500?
   }
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core razor-pages

0
推荐指数
1
解决办法
745
查看次数

获取异常:InvalidOperationException:无法解析 Booking.Repositories.DriverRepository 类型的服务

我知道我需要DriverRepository在使用该服务之前注册该服务。

这是我所做的:

builder.Services.AddScoped<IDriverRepository, DriverRepository>();
Run Code Online (Sandbox Code Playgroud)

现在在我的DriverAccountsModel课堂上:

public class DriverAccountsModel : PageModel
{
    public IHttpContextAccessor HttpContextAccessor { get; }
    public IDriverRepository DriverRepository { get; }

    public DriverAccountsModel(IHttpContextAccessor 
        httpContextAccessor,
        DriverRepository driverRepository)
    {
        HttpContextAccessor = httpContextAccessor;
        DriverRepository = driverRepository;
    }
    public List<DriverAccounts> results;
    public IActionResult OnGet()
    {
        var results = DriverRepository.GetAll();

        return Redirect("DriverAccounts");
    }

    public IActionResult OnPost(int id, Models.DriverAccounts driverAccounts)
    {
        return Redirect("DriverAccounts");
    }
}
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection asp.net-core

0
推荐指数
1
解决办法
64
查看次数

AutoMapper - 将模型映射到字典

我正在尝试使用 AutoMapper 映射 DTO Dictionary<string, object>

public class SetEmr
{
    public string Name { get; set; }
    public int Repeat { get; set; }
    public int RestTime { get; set; }
    public int Order { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试做这样的事情:

CreateMap<SetEmr, Dictionary<string, object>>()
    .ForMember(dest => dest, opt => opt.MapFrom((src, dst, _, context) =>
    {
        return new Dictionary<string, object>
        {
            { "Name", src.Name },
            { "Repeat", src.Repeat},
            { "Order", src.Order}
        };
    }));
Run Code Online (Sandbox Code Playgroud)

这不起作用。

“只有类型上的顶级个人成员才支持成员的自定义配置。”

还有其他方法可以实现这样的映射吗?

c# automapper

0
推荐指数
1
解决办法
873
查看次数

LINQ Group by 未按预期返回结果

我有一个动物课:

public class Animals
{
    public string Type { get; set; }
    public string Gender { get; set; }
    public int Amount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

以及与不同动物相关的成本的类别成本:

public class Cost 
{
    public string AnimalType { get; set; }
    public int Cost { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有一个不同类别的列表:

List<Animals> animalList = new List<Animals>()
{
     new Animals() { Type = "Wolf", Gender = "Female", Amount = 10 },
     new Animals() { Type = "Wolf", Gender = "Male", Amount = 20 }, …
Run Code Online (Sandbox Code Playgroud)

c# linq group-by

-1
推荐指数
1
解决办法
87
查看次数

'ExecuteNonQuery 需要一个开放且可用的连接。连接的当前状态

我一直有以下错误:

\n
\n

ExecuteNonQuery 需要一个打开且可用的连接。连接的当前状态已关闭。

\n
\n

我也无法插入到我的数据库中。

\n

在此输入图像描述

\n
private void btnXoa_Click(object sender, EventArgs e)\n{\n    SqlConnection conn = new SqlConnection(chuoiKetnoi);\n    if (MessageBox.Show("B\xe1\xba\xa1n c\xc3\xb3 ch\xe1\xba\xafc ch\xe1\xba\xafn mu\xe1\xbb\x91n x\xc3\xb3a ?", "Th\xc3\xb4ng b\xc3\xa1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)\n    {\n        // Thuc hien xoa du lieu\n        string delete = "delete from tblKET_QUA where MaSV=\'" + txtMaSinhVien.Text + "\' and MaMH=\'" + cbbMonHoc.Text + "\' ";\n        using SqlCommand cmd = new SqlCommand(delete, conn);\n        cmd.ExecuteNonQuery();\n        MessageBox.Show("X\xc3\xb3a d\xe1\xbb\xaf li\xe1\xbb\x87u th\xc3\xa0nh c\xc3\xb4ng", "Th\xc3\xb4ng b\xc3\xa1o!");\n\n        // Tr\xe1\xba\xa3 t\xc3\xa0i nguy\xc3\xaan\n …
Run Code Online (Sandbox Code Playgroud)

c# sql sql-server ado.net

-2
推荐指数
1
解决办法
845
查看次数