dotnet / dotnet/aspnetcore

TempData type detection

Open
#57,692 0 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

When using `TempData`, reading values from it has an annoying "feature" that tries to auto-detect the stored type based on the value (see https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.ViewFeatures/src/Infrastructure/DefaultTempDataSerializer.cs#L44). A discussion happened in https://github.com/dotnet/aspnetcore/issues/8873, but was abadoned after it received no responses.

As an example, I want to store some ID form an external service in a TempData field. This works as long as the received ID is NOT a GUID.

Note that this applies to all external data. If user-supplied data is stored in the TempData, as soon as a user enters a GUID, the deserialization will fail.

### Expected Behavior

The `TempData` should accept a generic type argument to retrieve a value.

If a property is annotated with the `TempDataAttribute`, the type of that property should be taken into account when retrieving the value instead of relying on detecting the data type solely based on the stored value.

### Steps To Reproduce

```
public class IndexModel
{
[TempData]
public string? Value { get; set; }

public ActionResult OnPost()
{
Value = "test"; // this works
Value = "2d3c1067-c17c-438a-bd9f-f16b772f3a44"; // This would throw with an `InvalidCastException` in the `OnGet` method
return RedirectToPage();
}

public void OnGet()
{
// If the Value can be parsed as a GUID, we get an exception here
}
}
```

### Exceptions (if any)

```
InvalidCastException: Unable to cast object of type 'System.Guid' to type 'System.String'.
Microsoft.Extensions.Internal.PropertyHelper.CallPropertySetter(Action setter, object target, object value)
Microsoft.AspNetCore.Mvc.ViewFeatures.Filters.SaveTempDataPropertyFilterBase.SetPropertyValues(ITempDataDictionary tempData)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker)
Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

```

### .NET Version

8.0.206

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.