小编Tor*_*rfi的帖子

在正文中使用 int/string(简单类型)发布到 asp.net core web api 2.1 不起作用

我只是没有运气将 url 编码的表单值从邮递员发送到使用文件-> 新项目创建的 vanilla asp.net core 2.1 web api。我什么也没做,但新的模型验证功能似乎仍然启动并向邮递员返回 400 Bad Request。谁能告诉我我做错了什么?

控制器动作:

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}
Run Code Online (Sandbox Code Playgroud)

原始请求(如 fiddler 所示):

POST http://localhost:60843/api/values HTTP/1.1
Content-Type: application/x-www-form-urlencoded
cache-control: no-cache
Postman-Token: a791eee7-63ff-4106-926f-2s67a8dcf37f
User-Agent: PostmanRuntime/7.3.0
Accept: */*
Host: localhost:60843
accept-encoding: gzip, deflate
content-length: 7
Connection: keep-alive

value=test
Run Code Online (Sandbox Code Playgroud)

原始回复:

HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Kestrel
X-SourceFiles: =?UTF-8?BQzpcUmVwb3NcVGVzdGJlZFxNb2RlbEJpbmRpbmdcTW9kZWxCaW5kaW5nXGFwaVx2YWx1ZXM=?=
X-Powered-By: ASP.NET
Date: Thu, 25 Oct 2018 15:23:49 GMT

21
{"":["The input was not valid."]} …
Run Code Online (Sandbox Code Playgroud)

c# model-binding asp.net-core

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

简单的注入器在运行时更改注册

我正在使用带有ASP.NET Web API项目的Simple Injector,我想对一个接口使用不同的实现,具体取决于所使用的REST端点的版本.例如,如果使用端点的v1,IPaymentData则应使用调用的类进行实例化PaymentData,但如果使用v2 IPaymentData则应由调用的类实现PaymentDataNew.结果证明这是有问题的!

我不想使用类似Simple Injector文档的一部分的复合类,因为这意味着我的代码需要注意并考虑我正在使用的注入框架(坏).

我在最新版本(3.0)中注意到一种称为"基于上下文的注入"的功能.使用该container.RegisterConditional函数,每次解析类型时都应该可以运行委托.

container.RegisterConditional(typeof(IPaymentData),
    c => ((HttpContext.Current.Items["version"] as string ?? "1") == "2") 
        ? typeof(PaymentDataNew) 
        : typeof(PaymentData),
    Lifestyle.Scoped,
    c => true);
Run Code Online (Sandbox Code Playgroud)

但是,这似乎不起作用,因为即使生命周期是作用域,并且默认生活方式是WebApiRequestLifestyle根据版本返回实现的委托只调用第一个请求.后续请求跳过此(它们似乎使用缓存实现).

有什么我想念的吗?如何在每次请求进入时确保调用委托?

c# dependency-injection simple-injector asp.net-web-api

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