$.ajax({
url: '/Controller/Action',
type: 'POST',
dataType: 'json',
data: { key1: value1, key2: value2 },
success: function (data) {
// handle success here
},
error: function (xhr, status, error) {
// handle error here
}
});
public async Task Action(string key1, string key2) {
// Your code here
// Use await to wait for async tasks
string result = await SomeAsyncMethod();
return Json(new { response = result });
}
public async Task Action(string key1, string key2) {
// Store data in session
HttpContext.Session.SetString("key", "value");
// Retrieve data from session
string value = HttpContext.Session.GetString("key");
return Json(new { response = value });
}
请注意,Session属性可能需要在您的应用程序中启用并配置。您可以在startup.cs文件中使用以下代码中进行配置:
public void ConfigureServices(IServiceCollection services) {
services.AddMvc();
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(30);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
app.UseSession();
app.UseMvcWithDefaultRoute();
}
通过这些步骤,您可以管理用于Asp.NET Core MVC的jQuery Ajax的异步请求和Session数据。