我有一个Web API方法:
public List<Task> GetTasks([FromUri] TaskFilter filter)
{
}
Run Code Online (Sandbox Code Playgroud)
该方法具有带可空标识符列表的参数:
public class TaskFilter
{
public IList<int?> Assignees { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我打电话的时候:
GET /tasks?assignees=null
Run Code Online (Sandbox Code Playgroud)
服务器返回错误:
{
"message":"The request is invalid.",
"modelState": {
"assignees": [ "The value 'null' is not valid for Nullable`1." ]
}
}
Run Code Online (Sandbox Code Playgroud)
它只有在我传递空字符串时才有效:
GET /tasks?assignees=
Run Code Online (Sandbox Code Playgroud)
但是标准查询字符串转换器(来自JQuery, Angular等)不能以这种方式使用空值.
如何让ASP.NET解释'null'为null?
Upd:查询字符串可以包含多个标识符,例如:
GET /tasks?assignees=1&assignees=2&assignees=null
Run Code Online (Sandbox Code Playgroud)
Upd2: JQuery将数组中的空值转换为空字符串,ASP.NET将它们解释为null.所以问题是从Angular 1.6($HttpParamSerializerProvider)调用WebAPI
Upd3:我知道解决方法,但我没有要求它们.我想要一个特定问题的解决方案:
null值List<int?>因为API文档是自动生成的,我不希望将文本数组视为参数类型JQuery.param …我配置了Identity Server:
public void Configuration(IAppBuilder app)
{
var factory = new IdentityServerServiceFactory().UseInMemoryClients(new Client[] {
new Client()
{
ClientName = "MyClient",
ClientId = "MyClientId",
Enabled = true,
Flow = Flows.Implicit,
RedirectUris = new List<string> { "MyClientServer/callback" },
};
});
}
Run Code Online (Sandbox Code Playgroud)
和客户端服务器:
public void Configuration(IAppBuilder app)
{
var cookieOptions = new CookieAuthenticationOptions();
cookieOptions.AuthenticationType = "Cookies";
app.UseCookieAuthentication(cookieOptions);
var authenticationOptions = new OpenIdConnectAuthenticationOptions() {
Authority = "https://MyIdentityServer/core",
ClientId = "MyClientId",
SignInAsAuthenticationType = "Cookies",
UseTokenLifetime = true,
RedirectUri = "MyClientServer/callback"
});
app.UseOpenIdConnectAuthentication(authenticationOptions);
}
Run Code Online (Sandbox Code Playgroud)
当用户使用"记住我"选项登录时,身份cookie已过期日期:
idsvr.session …Run Code Online (Sandbox Code Playgroud) cookies session-cookies katana asp.net-identity identityserver3
我有一个代码来获取与之相关的书籍和借书卡:
// mimic http requests
const fetchBook = (bookId: number) => {
const title = 'Book' + bookId;
return timer(200).pipe(mapTo({ bookId, title }));
}
const fetchLibraryCard = (bookId: number) => {
const borrowerName = 'Borrower of Book' + bookId;
return timer(300).pipe(mapTo({ borrowerName }));
}
const bookId$ = new Subject<number>();
const book$ = bookId$.pipe(
switchMap(bookId => fetchBook(bookId)),
shareReplay(1)
);
// e.g. 'Refresh library card' button
const libraryCardUpdater$ = new BehaviorSubject<void>(undefined);
const libraryCard$ = combineLatest([bookId$, libraryCardUpdater$]).pipe(
switchMap(([bookId]) => fetchLibraryCard(bookId)),
shareReplay(1)
);
combineLatest([book$, …Run Code Online (Sandbox Code Playgroud)