[英]Using sessions in ASP.NET 5 (MVC 6)
我無法在MVC 6項目中使用會話。
我讀了一篇關於此的精彩文章 ,但我無法獲得app.UseInMemorySession();
按照描述在我的Startup.cs
工作。
我將"Microsoft.AspNet.Session": "1.0.0-beta6"
到我的project.json
的依賴項部分,並修改了我的Startup.cs
,如下所示:
我的配置部分如下所示:
public void ConfigureServices(IServiceCollection services)
{
// Add MVC services to the services container.
services.AddMvc();
services.AddSession();
services.AddCaching();
}
我的配置部分如下所示:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.MinimumLevel = LogLevel.Information;
loggerFactory.AddConsole();
// Configure the HTTP request pipeline.
// Add the following to the request pipeline only in development environment.
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseErrorPage();
}
else
{
// Add Error handling middleware which catches all application specific errors and
// send the request to the following path or controller action.
app.UseErrorHandler("/Home/Error");
}
// Add static files to the request pipeline.
app.UseStaticFiles();
// Configure sessions:
//app.UseInMemorySession();
// Add MVC to the request pipeline.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
請注意app.UseInMemorySession();
被注釋掉,因為如果我啟用它,我會收到以下錯誤:
'IApplicationBuilder' does not contain a definition for 'UseInMemorySession' and no extension method 'UseInMemorySession' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
如果我嘗試在沒有app.UseInMemorySession();
情況下運行應用程序app.UseInMemorySession();
使用會話時出現以下錯誤:
An exception of type 'System.InvalidOperationException' occurred in Microsoft.AspNet.Http.dll but was not handled in user code
關於如何解決這個問題的任何建議表示贊賞:-)
你應該使用:
app.UseSession();
它位於Microsoft.AspNet.Builder
命名空間中,因此您不需要Session
命名空間。
請參閱: https : //github.com/aspnet/Session/blob/1.0.0-beta6/src/Microsoft.AspNet.Session/SessionMiddlewareExtensions.cs
似乎我需要兩個改變。
首先,我需要using Microsoft.AspNet.Session;
添加using Microsoft.AspNet.Session;
,我不得不改變: app.UseInMemorySession();
到app.UseSession();
在Startup.cs
的Configure部分中。
至少當我這樣做時,它開始工作而不會拋出異常。
為任何試圖使用rc2的人添加注釋,都會出現以下異常。
ILoggerFactory'不包含'MinimumLevel'的定義,並且沒有擴展方法'MinimumLevel'接受類型'ILoggerFactory'的第一個參數可以找到(你是否缺少using指令或匯編引用?)
MinimumLevel已根據以下重大更改刪除: https : //github.com/aspnet/Announcements/issues/122
loggerFactory.MinimumLevel = LogLevel.Information;
該行只需要從rc2 +的代碼(和OP的代碼)中刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.