我的所有模型在到达端点之前都会自动验证,如果某种形式的验证失败,则返回适当的错误。
我记得在 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
谢谢!
这就是我的意思:
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)
可以等待上述方法,我认为它与基于T的A同步P模式建议的操作非常相似?(我知道的其他模式是APM和EAP模式。)
现在,下面的代码呢?
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) 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 …
当我们想在 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) 为了生成所需的令牌-消费者密钥,消费者密钥,令牌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) MSDN中的密钥推导:
使用 PBKDF2 算法执行密钥派生。
MSDN 中的 Rfc2898DeriveBytes:
通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 PBKDF2。
这些不是一样的东西吗?我们可以在这两种方法中设置哈希算法。
如果我们这样做
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。
最小可重现示例:
互操作文件
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) 两者最终都会产生一个空的 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# ×10
.net-core ×4
asp.net-core ×3
asp.net ×2
.net ×1
async-await ×1
asynchronous ×1
hash ×1
httpcontext ×1
json ×1
model ×1
netsuite ×1
optimization ×1
performance ×1
security ×1
soap ×1
swagger ×1
task ×1
types ×1
validation ×1
valuetuple ×1