小编Sup*_*Bob的帖子

为什么我们不再需要在更高版本的 ASP.NET Core 中手动验证模型?

我的所有模型在到达端点之前都会自动验证,如果某种形式的验证失败,则返回适当的错误。

我记得在 ASP.NET Core 2.2 中,我们需要手动调用ModelState.IsValid以确保对象已通过验证检查,但在最新的 ASP.NET Core 3.0 中,情况似乎并非如此,而且我无处包括/为存在此行为显式配置任何服务。

有人可以对此事有所了解,也许可以链接他们提到此更改的相关来源吗?

编辑:是由于[ApiController]属性吗?请参阅:https : //docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1#automatic-http-400-responses

谢谢!

c# validation model-view-controller model asp.net-core

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

属性“[ApiExplorerSettings(IgnoreApi = true)]”有什么作用?

编辑:我知道一般属性有什么作用,问题仅针对这个特定属性。对困惑感到抱歉!

我已经阅读了以下问题以及这个问题,其中指出了如何使用该属性来忽略为特定方法或整个控制器生成的 swagger/swashbuckle 文档。(文档是我相信列出了所有 api 的 swagger 页面?)

但是除了 swagger/swashbuckle(这是一个 NuGet 包),这个属性在 ASP.NET 中还有什么其他功能?

c# asp.net swagger

8
推荐指数
2
解决办法
8910
查看次数

处理异步任务时,使用await与使用ContinueWith有何不同?

这就是我的意思:

public Task<SomeObject> GetSomeObjectByTokenAsync(int id)
    {
        string token = repository.GetTokenById(id);
        if (string.IsNullOrEmpty(token))
        {
            return Task.FromResult(new SomeObject()
            {
                IsAuthorized = false
            });
        }
        else
        {
            return repository.GetSomeObjectByTokenAsync(token).ContinueWith(t =>
            {
                t.Result.IsAuthorized = true;
                return t.Result;
            });
        }
    }
Run Code Online (Sandbox Code Playgroud)

可以等待上述方法,我认为它与基于TA同步P模式建议的操作非常相似?(我知道的其他模式是APMEAP模式。)

现在,下面的代码呢?

public async Task<SomeObject> GetSomeObjectByToken(int id)
    {
        string token = repository.GetTokenById(id);
        if (string.IsNullOrEmpty(token))
        {
            return new SomeObject()
            {
                IsAuthorized = false
            };
        }
        else
        {
            SomeObject result = await repository.GetSomeObjectByTokenAsync(token);
            result.IsAuthorized = true; …
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading task .net-core

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

Where is the dictionary key "MS_HttpContext" defined for HttpRequestMessage.Properties?

I've searched everywhere and nowhere does it mention where this key is defined, or similar ones such as HTTP_X_FORWARDED_FOR, REMOTE_ADDR etc.

MSDN documentation doesn't mention anything useful. The only thing close to useful I came about was some stackoverflow questions (this and this), along with this short blog post.

None of these sadly address my current question - from where does all these dictionary keys come from? Where is their specification, so that one knows they …

c# asp.net asp.net-web-api

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

在 ASP.NET Core 3.1 中使用带有 HttpContext.Response 的新 Json 序列化器

当我们想在 ASP.NET Core 的管道中将对象序列化为 JSON 字符串时,我们需要使用HttpContext.Response.Body.WriteAsync,除非我遗漏了一些东西,因为没有Result可以轻松用于分配JsonResult对象的属性。

除非有更好的替代方案,否则使用上述方法究竟是如何实现序列化的?

注意: JSON 序列化程序的选项应与 ASP.NET Core 3.1 中使用的(默认)选项相同。

如果需要(不是在我们的例子中),它们可以通过IServiceCollection.AddJsonOptions中间件进行修改。

例子:

app.Use( next =>
{
    return async context =>
    {
        if (<someFunkyConditionalExample>)
        {
            // serialize a JSON object as the response's content, returned to the end-user.
            // this should use ASP.NET Core 3.1's defaults for JSON Serialization.
        }
        else
        {
            await next(context);
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

c# json httpcontext asp.net-core asp.net-core-middleware

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

是否可以使用TBA作为身份验证方法来找到网络套件员工的角色?

为了生成所需的令牌-消费者密钥,消费者密钥,令牌ID,令牌密钥-我们正在创建集成和访问令牌,并将它们分配给具有访问TBA特定角色的员工。(请参阅https://medium.com/@morrisdev/netsuite-token-based-authentication-tba-342c7df56386

那么,有没有可能几乎没有麻烦地获得该员工的特定角色呢?

我正在尝试这样做,但是我找不到办法,所以我才开始列出所有可能的雇员,并要求进行身份验证的人员(除了提供其4个令牌(以及其帐户ID)之外,还应提供他们的身份)。角色,这看起来很愚蠢。(一旦有了员工,我就可以找到具有所需角色的员工,因为他们是唯一使用它的员工

private static void GetEmployees()
{
    EmployeeSearch search = new EmployeeSearch();
    EmployeeSearchBasic esb = new EmployeeSearchBasic();

    esb.isInactive = new SearchBooleanField();
    esb.isInactive.searchValue = false;
    esb.isInactive.searchValueSpecified = true;

    search.basic = esb;
    SearchResult res = Client.Service.search(search);
    res.pageSize = 2000;
    res.pageSizeSpecified = true;

    if (res.status.isSuccess)
    {
        Record[] searchRecords = res.recordList;
        if (searchRecords != null && searchRecords.Length >= 1)
        {
            //Do something...
        }
        else
        {
            //Do something...
        }
    }
    else
    {
        throw new Exception("Couldn't find any …
Run Code Online (Sandbox Code Playgroud)

c# soap netsuite

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

KeyDerivation.Pbkdf2 和 Rfc2898DeriveBytes 有什么区别?

MSDN中的密钥推导:

使用 PBKDF2 算法执行密钥派生。

MSDN 中的 Rfc2898DeriveBytes:

通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 PBKDF2。

这些不是一样的东西吗?我们可以在这两种方法中设置哈希算法。

c# security hash .net-core

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

在 C# 中解构元组的性能损失?

如果我们这样做

var (hello, world) = GetHelloAndWorldStrings();
if (hello == "hello" && world == "world")
  Environment.Exit(0);
Run Code Online (Sandbox Code Playgroud)

除了仅执行以下操作外,这是否会产生任何额外费用:

var helloAndWorld = GetHelloAndWorldStrings();
if (helloAndWorld.Hello == "hello" && helloAndWorld.World == "world")
  Environment.Exit(0);
Run Code Online (Sandbox Code Playgroud)

或者这都是语法糖 - 最终生成的代码总是使用 Item1 和 Item2。

c# performance types .net-core valuetuple

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

为什么我的 IOCompetionCallback 从未在我的 IO 完成端口上执行?

最小可重现示例:

互操作文件

public static class Interop
{
    [DllImport("kernel32.dll")]
    public static extern IntPtr CreateIoCompletionPort(
        [In] IntPtr fileHandle,
        [In] IntPtr existingCompletionPort,
        [In] UInt32 completionKey,
        [In] UInt32 numberOfConcurrentThreads);

    [DllImport("kernel32.dll")]
    public static extern UInt32 GetLastError();

    [DllImport("kernel32.dll")]
    public static unsafe extern bool GetQueuedCompletionStatus(
        [In] IntPtr completionPort,
        [Out] out UInt32 ptrBytesTransferred,
        [Out] out UInt32 ptrCompletionKey,
        [Out] NativeOverlapped** lpOverlapped,
        [In] UInt32 dwMilliseconds);

    [DllImport("kernel32.dll")]
    public static extern IntPtr CreateFile(
        [In] string fileName,
        [In] UInt32 dwDesiredAccess,
        [In] UInt32 dwShareMode,
        [In] IntPtr lpSecurityAttributes,
        [In] UInt32 dwCreationDisposition,
        [In] UInt32 dwFlagsAndAttributes,
        [In] …
Run Code Online (Sandbox Code Playgroud)

c# multithreading asynchronous async-await .net-core

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

ASP.NET Core 中的 Ok(null) 与 NoContent() - 哪个更有效?

两者最终都会产生一个空的 204 状态响应,但哪个更快?

显然,如果您遵循 DRY 准则,则编写起来会更简洁

return Ok(something);
Run Code Online (Sandbox Code Playgroud)

而不是

if (something == null)
{
    return NoContent()
}
else
{
    return Ok(something);
}
Run Code Online (Sandbox Code Playgroud)

检查源代码后,NoContent()转换为调用StatusCode(204),因为Ok(null)我没有深入了解他们在何处检查(如果有的话)为空值,如果它为空,决定返回一个 StatusCode 204(或处理它以其他方式)。

我个人认为这NoContent()会产生更快的性能,即使我们将讨论的差异在几分之一秒内。

c# optimization asp.net-core

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