标签: .net-core

如何将临时类转换为 json?

我有一个课程并且很容易转换为 json

但有时我想向 json 添加一些属性

我应该创建另一个类并转换为 json 吗?

我有一堂课

    class Person{
string name{get;set;}
string age{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

如果我想添加一个像性这样的属性,我必须创建一个具有名称、年龄和性别的类?

c# asp.net .net-core

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

EF CORE 中的 143 个查找表

目前我正在重新设计一个现有的程序,它使用一个包含多个值的主表。(C# .net core 3.0 & EF) (一大查表)

这些值中的大部分很少改变,我会将它们放在 ac# 枚举中。

一些示例:Language、Sex、ReceiptStatus、RiskType、RelationType、SignatureStatus、CommunicationType、PartKind、LegalStatute ……该列表不断增加,目前有 143 个不同的类别,每个类别都有自己的值,其中包含 2 个翻译。

我的公司希望这些值存在于数据库中,因此非程序员可以在必要时更改它们。

不过感觉一点都不好。我很想将表格分开,但创建 143 个表格似乎有点矫枉过正。如果只有 5-10 个查找表,那就没问题了..

有什么建议吗?坚持 1 个查找表?感觉我的眼睛不对。多桌?

说服我的公司我们应该只使用运行良好的 C# 枚举,排除非程序员可以编辑它们的可能性?

c# lookup-tables entity-framework-core .net-core

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

Blazor GetAsync 请求返回 401 状态代码

我是 blazor 的新手,并尝试使用 .NET Core /EF Core 3 和 Visual Studio 2019 创建应用程序。我已经设置了一个数据库模型和一个 API,用于获取所有地址 (/api/Address) 并在浏览器中浏览到此返回数据库中的所有记录。但是我在 Razor 文件中的 My GetAsync 方法返回 401,最终返回 null。

这是我的剃刀代码:

@functions 
{
   Address[] addresses;
   protected override async Task OnInitializedAsync()
   {
     addresses = await Http.GetJsonAsync<Address[]>("api/Address");
   }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 API

[Produces("application/json")]
[Route("api/[controller]")]
[ApiController]
public class AddressController : ControllerBase
{
    private readonly MyDataContext _context;
    // GET: api/Address
    [HttpGet]
    public async Task<ActionResult<IEnumerable<Address>>> GetAddresses()
    {
        return await _context.addressesDbSet.ToListAsync();
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到的所有错误说

 HttpRequestException: Response status code does not indicate success: 401 (Unauthorized). …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework .net-core asp.net-core blazor

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

StreamWriter 在文件末尾写了一个奇怪的行

I'm using a StreamWriter to write a string to memory and then return it as a file via an IActionResult in an ASP.Net Core Web API, and I'm running into a weird issue where I'm getting a line of indecipherable characters at the end of the output file...

Here's an image of what I'm talking about:

在此处输入图片说明

The text on line 513 is not supposed to be there... I'm thinking it has something to do with encoding, but I don't know …

c# .net-core asp.net-core

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

C# Linq Net Core:排除包含搜索词的词

如何在 Linq Net Core 2 中执行 String Not Contains 'paraphrase key term'?

为此,尝试在产品名称中不包含“ag”的列表中获取。

foreach (var product in Products().Where(c=>c.ProductName.NotContains("ag")))
Run Code Online (Sandbox Code Playgroud)

.net c# .net-core .net-core-2.0

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

C# 非静态字段、方法或属性“HttpContext.Request”需要对象引用

我正在尝试创建一个通用工厂类来调用 WCF 并注入一些标头。在这个类中,我试图读取 HTTP 标头属性。

    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using ServiceReference;
    using Microsoft.AspNetCore.Http;
namespace Service
{
     public class ServiceFactory
        {

            public static ServiceClient Create()
            {
                ServiceProxy service = new ServiceProxy();
                string userName = HttpContext.Request.Headers["AUTH_USERNAME"];
                string authenricationType = HttpContext.Request.Headers["AUTH_TYPE"];

                using (new System.ServiceModel.OperationContextScope((System.ServiceModel.IClientChannel)service.InnerChannel))
                {          
                    HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                    requestMessage.Headers["AUTH_USERNAME"] = userName;
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;

                    requestMessage.Headers["AUTH_TYPE"] = authenricationType;
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
                }
                return service;
            }       
        }
}
Run Code Online (Sandbox Code Playgroud)

但是我收到一个编译错误,因为“非静态字段、方法或属性 'HttpContext.Request' 需要一个对象引用。由于我不是从静态方法或类调用,这是怎么发生的。任何帮助都会受到高度赞赏。

谢谢你。

c# wcf .net-core

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

使用 is 关键字检查可空对象是否为空

我刚刚发现,可以使用is-keywordin C# 对可空结构进行空检查。我认为它看起来很干净并且很好理解,但它也有性能吗?口译员是否必须双重投射它或者这样可以吗?

    static void Main(string[] args)
    {
        DateTime? test = null;

        PrintDT(test);//wont print
        test = DateTime.Now;
        PrintDT(test);//will print
    }
    private static void PrintDT(DateTime? dt)
    {
        if (dt is DateTime a)
            Console.WriteLine(a);
    }
Run Code Online (Sandbox Code Playgroud)

.net c# .net-core

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

如何解决错误不包含定义 - C# 新手

我在 nodejs/typescript 编程后开始学习 c#。

我遇到了以下错误:

TypeTests.cs(22,50):错误 CS1061:“书”不包含“名称”的定义,并且找不到接受“书”类型的第一个参数的可访问扩展方法“名称”(您是否缺少使用指令或程序集引用?)

这是我的班级

using System;
using System.Collections.Generic;

namespace GradeBook
{
    public class Book
    {
        // this is how we generate a constructor in c#
        public Book(string name)
        {
            grades = new List<double>();
            Name = name;
        }

        public void AddGrade(double grade)
        {
            grades.Add(grade);
            Console.WriteLine($"A new added grade: {grade:N2}");
        }

        public Stats GetStats()
        {
            var result = new Stats();
            result.Average = 0.0;
            result.High = double.MinValue;
            result.Low = double.MaxValue;

            foreach (var grade in grades)
            {
                // log hightest …
Run Code Online (Sandbox Code Playgroud)

c# .net-core

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

控制器动作方法中没有异步/等待的任务

所以我有一个关于 Task 和 async/await 操作的问题。

我有一个这样的方法:

public Task<IActionResult> PostCompleteFormModel(string name, [FromBody]IndicatorFormApi apiModel, Guid participantId, [FromServices] IChallengeCycleService challengeCycleService)
{
    return Post(name, apiModel.ToSubmitApi(), participantId, challengeCycleService);
}
Run Code Online (Sandbox Code Playgroud)

在控制器中。

正如你所看到的,它没有 async/await。

但是这样做不是更好吗:

public async Task<IActionResult> PostCompleteFormModel(string name, [FromBody]IndicatorFormApi apiModel, Guid participantId, [FromServices] IChallengeCycleService challengeCycleService)
{
    return await Post(name, apiModel.ToSubmitApi(), participantId, challengeCycleService);
}
Run Code Online (Sandbox Code Playgroud)

那么使用 async/await 关键字?

或者不让它有所不同?

谢谢

c# multithreading .net-core

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

Entity Framework Core + Linq:如何获得具有最大值的整个实体?寻找更优雅的解决方案

这是我到目前为止:

var TEST = await context.ProcessRevision.FirstOrDefaultAsync(i => i.ProcessRevId == (context.ProcessRevision.Max(i => i.ProcessRevId)));
Run Code Online (Sandbox Code Playgroud)

我不确定这是否是一个好的解决方案,或者我是否可以用更少的处理做一些更优雅的事情。

有什么建议?

c# linq entity-framework-core .net-core

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