2017-01-01 7 views
4

アラートリストを作成して、TempDataに追加しようとしています。しかし、私がリダイレクトをしなければうまくいく。リダイレクトすると500エラーが出ます。私も同様にビューにブレークポイントを設定しましたが、他の賢明なリダイレクトが正しくヒットしたときにヒットしませんでした。Asp.net core TempDataはリストを追加して別のビューにリダイレクトすると500エラーを返します

ActionMethod

私はビューにアクセスする方法
public IActionResult Create(CategoryCreateVM input) 
{ 
    if (ModelState.IsValid) 
    { 
     var category = mapper.Map<Categories>(input); 
     categoryBL.Add(category); 

     List<Alert> alert = new List<Alert>(); 
     alert.Add(new Alert("alert-success", "success message")); 
     alert.Add(new Alert("alert-danger", "danger message")); 

     TempData["Alert"] = alert; 

     return RedirectToAction("Index"); 
    } 
    return View(input); 
} 

@{ 
    var alerts = TempData["Alert"] as List<Alert>; 
} 
@if (alerts != null && alerts.Count > 0) 
{ 
    <div class=""> 
     @foreach (var alert in alerts) 
     { 
      <div class="alert @alert.AlertClass alert-dismissible"> 
       <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> 
       @alert.Message 
      </div> 

     } 
    </div> 

} 

スタックトレース。

Exception thrown: 'System.InvalidOperationException' in Microsoft.AspNetCore.Mvc.ViewFeatures.dll 
Exception thrown: 'System.InvalidOperationException' in mscorlib.dll 
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Information: Executed action MvcSandbox.Controllers.HomeController.Index (MvcSandbox) in 46.4619ms 
Exception thrown: 'System.InvalidOperationException' in mscorlib.dll 
Exception thrown: 'System.InvalidOperationException' in mscorlib.dll 
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware: Error: An unhandled exception has occurred while executing the request 

System.InvalidOperationException: The 'Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.TempDataSerializer' cannot serialize an object of type 'MvcSandbox.Controllers.Alert'. 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.TempDataSerializer.EnsureObjectCanBeSerialized(Object item) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\Internal\TempDataSerializer.cs:line 207 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.TempDataSerializer.Serialize(IDictionary`2 values) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\Internal\TempDataSerializer.cs:line 142 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.SessionStateTempDataProvider.SaveTempData(HttpContext context, IDictionary`2 values) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\ViewFeatures\SessionStateTempDataProvider.cs:line 62 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary.Save() in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\ViewFeatures\TempDataDictionary.cs:line 166 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.SaveTempData(IActionResult result, ITempDataDictionaryFactory factory, HttpContext httpContext) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\Internal\SaveTempDataFilter.cs:line 91 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.OnResultExecuted(ResultExecutedContext context) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\Internal\SaveTempDataFilter.cs:line 80 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.Core\Internal\ControllerActionInvoker.cs:line 1023 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextResourceFilter>d__22.MoveNext() in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.Core\Internal\ControllerActionInvoker.cs:line 1105 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.Core\Internal\ControllerActionInvoker.cs:line 1377 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.Core\Internal\ControllerActionInvoker.cs:line 493 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__20.MoveNext() in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.Core\Internal\ControllerActionInvoker.cs:line 164 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.GetResult() 
    at Microsoft.AspNetCore.Session.SessionMiddleware.<Invoke>d__9.MoveNext() in D:\temp\Session-dev\Session-dev\src\Microsoft.AspNetCore.Session\SessionMiddleware.cs:line 106 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at Microsoft.AspNetCore.Session.SessionMiddleware.<Invoke>d__9.MoveNext() in D:\temp\Session-dev\Session-dev\src\Microsoft.AspNetCore.Session\SessionMiddleware.cs:line 123 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext() 
Exception thrown: 'System.InvalidOperationException' in Microsoft.AspNetCore.Http.dll 
Microsoft.AspNetCore.Server.Kestrel: Error: Connection id "0HL1I8OFS17SU": An unhandled exception was thrown by the application. 

System.InvalidOperationException: Session has not been configured for this application or request. 
    at Microsoft.AspNetCore.Http.DefaultHttpContext.get_Session() 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.SessionStateTempDataProvider.SaveTempData(HttpContext context, IDictionary`2 values) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\ViewFeatures\SessionStateTempDataProvider.cs:line 57 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary.Save() in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\ViewFeatures\TempDataDictionary.cs:line 166 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.SaveTempData(IActionResult result, ITempDataDictionaryFactory factory, HttpContext httpContext) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\Internal\SaveTempDataFilter.cs:line 91 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.<>c.<OnResultExecuting>b__5_0(Object state) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\Internal\SaveTempDataFilter.cs:line 57 
    at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.<FireOnStarting>d__178.MoveNext() 
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware: Error: An exception was thrown attempting to display the error page. 

System.ObjectDisposedException: The response has been aborted due to an unhandled application exception. ---> System.InvalidOperationException: Session has not been configured for this application or request. 
    at Microsoft.AspNetCore.Http.DefaultHttpContext.get_Session() 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.SessionStateTempDataProvider.SaveTempData(HttpContext context, IDictionary`2 values) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\ViewFeatures\SessionStateTempDataProvider.cs:line 57 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary.Save() in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\ViewFeatures\TempDataDictionary.cs:line 166 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.SaveTempData(IActionResult result, ITempDataDictionaryFactory factory, HttpContext httpContext) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\Internal\SaveTempDataFilter.cs:line 91 
    at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.<>c.<OnResultExecuting>b__5_0(Object state) in D:\temp\Mvc-dev\src\Microsoft.AspNetCore.Mvc.ViewFeatures\Internal\SaveTempDataFilter.cs:line 57 
    at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.<FireOnStarting>d__178.MoveNext() 
    --- End of inner exception stack trace --- 
    at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.ThrowResponseAbortedException() 
    at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.<ProduceStartAndFireOnStartingAwaited>d__194.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.Write(ArraySegment`1 data) 
    at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.FrameResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count) 
    at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) 
    at System.IO.StreamWriter.Write(String value) 
    at Microsoft.Extensions.RazorViews.BaseView.Write(String value) 
    at Microsoft.AspNetCore.Diagnostics.RazorViews.ErrorPage.<ExecuteAsync>d__6.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.Extensions.RazorViews.BaseView.<ExecuteAsync>d__29.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext() 
Microsoft.AspNetCore.Server.Kestrel: Error: Connection id "0HL1I8OFS17SU": An unhandled exception was thrown by the application. 
+0

スタックトレースは何を示していますか? –

+0

Strackトレースが表示されていないと、localhostは動作しません。エラーコード500 – Ahmar

+0

スタックトレースを表示するようにログを有効にします – Tratcher

答えて

13

最後に、私はソースコードを掘り下げた後に何が問題であるかを考え出しました。 Asp.Net Core MVC現在、TempDataの複合データ型はサポートされていません。今のところstringしかサポートしていません。データをシリアライズしている間、この例外を介して、それ以外の場合はstringを渡します。

'Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.TempDataSerializer' はタイプのオブジェクトをシリアル化することはできません 'MvcSandbox.Controllers.Alert'。

私のリストをjsonにシリアライズし、次にTempDataに保存します。

これは私がこれをした方法です。 TempDataからデータを追加および取得する拡張メソッドを作成します。

const string Alerts = "Alerts"; 
public static List<Alert> GetAlert(this ITempDataDictionary tempData) 
{ 
    CreateAlertTempData(tempData); 
    return DeserializeAlerts(tempData[Alerts] as string); 
} 

public static void CreateAlertTempData(this ITempDataDictionary tempData) 
{ 
    if (!tempData.ContainsKey(Alerts)) 
    { 
     tempData[Alerts] = ""; 
    } 
} 
public static void AddAlert(this ITempDataDictionary tempData, Alert alert) 
{ 
    if(alert == null) 
    { 
     throw new ArgumentNullException(nameof(alert)); 
    } 
    var deserializeAlertList = tempData.GetAlert(); 
    deserializeAlertList.Add(alert); 
    tempData[Alerts] = SerializeAlerts(deserializeAlertList); 
} 
public static string SerializeAlerts(List<Alert> tempData) 
{ 
    return JsonConvert.SerializeObject(tempData); 
} 
public static List<Alert> DeserializeAlerts(string tempData) 
{ 
    if(tempData.Length == 0) 
    { 
     return new List<Alert>(); 
    } 
    return JsonConvert.DeserializeObject<List<Alert>>(tempData); 
} 

アラートを追加し、それTempData

tempData.AddAlert(new Alert(AlertClass, Message)); 

最後に 'TempDataをinビュー' と表示を取得します。

@if (TempData.GetAlert().Count > 0) 
{ 
    <div class="alert-container"> 
     @foreach (var alert in TempData.GetAlert()) 
     { 
      <div class="alert @alert.AlertClass alert-dismissible"> 
       <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> 
       @alert.Message 
      </div> 
     } 
    </div> 
} 
2

セッションを設定しましたか? TempDataはバックグラウンドでセッションを使用しています。

Project.json

"Microsoft.AspNetCore.Session": "1.1.0" 

ここStartup.csファイルです。 - ConfigureServices方法

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddSession(); 
    services.AddMvc(); 
} 

そしてConfigure方法。

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    app.UseSession(); 
     app.UseMvc(routes => 
     { 
      routes.MapRoute(
        name: "default", 
        template: "{controller=Home}/{action=Index}/{id?}"); 
    }); 
} 

ここでTempDataを試してみてください。

環境変数をset ASPNETCORE_ENVIRONMENT=Developmentに設定することができます。

+0

私はこれらのすべての手順を既に完了しています – Ahmar

+0

まだ問題はありますか?スタックトレースとは何ですか?コマンドラインから実行している場合、例外は何ですか? – Anuraj

+0

@ Sami-Kuhmonenスタックトレースが追加されました。 – Ahmar

0

また、.AddSessionStateTempDataProvider()をservices.AddMvc()に追加する必要があります。

services.AddMvc() 
    .AddSessionStateTempDataProvider(); 
関連する問題