diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Page.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Page.cs
index 062c52c9b2..8aed2c7875 100644
--- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Page.cs
+++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Page.cs
@@ -2,13 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
-using System.Collections.Generic;
using System.Diagnostics;
-using System.Text.Encodings.Web;
-using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Razor;
-using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
+using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Mvc.RazorPages
@@ -49,6 +46,12 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
}
}
+ ///
+ /// Gets the from the .
+ ///
+ /// Returns null if is null.
+ public ITempDataDictionary TempData => PageContext?.TempData;
+
///
public override void EnsureRenderedBodyOrSections()
{
@@ -90,5 +93,32 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
}
}
+ ///
+ /// Creates a object that redirects to the specified .
+ ///
+ /// The URL to redirect to.
+ /// The created for the response.
+ protected RedirectResult Redirect(string url)
+ {
+ if (string.IsNullOrEmpty(url))
+ {
+ throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(url));
+ }
+
+ return new RedirectResult(url);
+ }
+
+ ///
+ /// Creates a object that renders this page as a view to the response.
+ ///
+ /// The created object for the response.
+ ///
+ /// Returning a from a page handler method is equivalent to returning void.
+ /// The view associated with the page will be executed.
+ ///
+ protected PageViewResult View()
+ {
+ return new PageViewResult(this);
+ }
}
}
diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs
index 46f1d0e3c5..b59bc66acd 100644
--- a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs
+++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs
@@ -44,6 +44,12 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
public ModelStateDictionary ModelState => PageContext.ModelState;
+ ///
+ /// Gets the from the .
+ ///
+ /// Returns null if is null.
+ public ITempDataDictionary TempData => PageContext?.TempData;
+
public ViewDataDictionary ViewData => PageContext?.ViewData;
protected Task BindAsync(string name)
diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Properties/Resources.Designer.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Properties/Resources.Designer.cs
index af7fffe698..365bdcc9e4 100644
--- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Properties/Resources.Designer.cs
+++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Properties/Resources.Designer.cs
@@ -74,6 +74,22 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
return string.Format(CultureInfo.CurrentCulture, GetString("PageViewResult_ContextIsInvalid"), p0, p1);
}
+ ///
+ /// Value cannot be null or empty.
+ ///
+ internal static string ArgumentCannotBeNullOrEmpty
+ {
+ get { return GetString("ArgumentCannotBeNullOrEmpty"); }
+ }
+
+ ///
+ /// Value cannot be null or empty.
+ ///
+ internal static string FormatArgumentCannotBeNullOrEmpty()
+ {
+ return GetString("ArgumentCannotBeNullOrEmpty");
+ }
+
private static string GetString(string name, params string[] formatterNames)
{
var value = _resourceManager.GetString(name);
diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Resources.resx b/src/Microsoft.AspNetCore.Mvc.RazorPages/Resources.resx
index 1f025bfbb1..b3c021e3da 100644
--- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Resources.resx
+++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Resources.resx
@@ -129,4 +129,7 @@
Argument '{0}' is not the same instance used to create '{1}'.
+
+ Value cannot be null or empty.
+
\ No newline at end of file
diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs
index 1d509eb401..b67c5405f2 100644
--- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs
+++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+using System;
+using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
@@ -80,5 +82,66 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
var content = await response.Content.ReadAsStringAsync();
Assert.Equal("Hello, pagemodel!", content.Trim());
}
+
+ [Fact]
+ public async Task TempData_SetTempDataInPage_CanReadValue()
+ {
+ // Arrange 1
+ var url = "http://localhost/TempData/SetTempDataOnPageAndRedirect?message=Hi1";
+ var request = new HttpRequestMessage(HttpMethod.Get, url);
+
+ // Act 1
+ var response = await Client.SendAsync(request);
+
+ // Assert 1
+ Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
+
+ // Arrange 2
+ request = new HttpRequestMessage(HttpMethod.Get, response.Headers.Location);
+ request.Headers.Add("Cookie", GetCookie(response));
+
+ // Act2
+ response = await Client.SendAsync(request);
+
+ var content = await response.Content.ReadAsStringAsync();
+ Assert.Equal("Hi1", content.Trim());
+ }
+
+ [Fact]
+ public async Task TempData_SetTempDataInPageModel_CanReadValue()
+ {
+ // Arrange 1
+ var url = "http://localhost/TempData/SetTempDataOnPageModelAndRedirect?message=Hi2";
+ var request = new HttpRequestMessage(HttpMethod.Get, url);
+
+ // Act 1
+ var response = await Client.SendAsync(request);
+
+ // Assert 1
+ Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
+
+ // Arrange 2
+ request = new HttpRequestMessage(HttpMethod.Get, response.Headers.Location);
+ request.Headers.Add("Cookie", GetCookie(response));
+
+ // Act2
+ response = await Client.SendAsync(request);
+
+ var content = await response.Content.ReadAsStringAsync();
+ Assert.Equal("Hi2", content.Trim());
+ }
+
+ private static string GetCookie(HttpResponseMessage response)
+ {
+ var setCookie = response.Headers.GetValues("Set-Cookie").ToArray();
+ return setCookie[0].Split(';').First();
+ }
+
+ public class CookieMetadata
+ {
+ public string Key { get; set; }
+
+ public string Value { get; set; }
+ }
}
}
diff --git a/test/WebSites/RazorPagesWebSite/Startup.cs b/test/WebSites/RazorPagesWebSite/Startup.cs
index e563d2241a..0959cd28d8 100644
--- a/test/WebSites/RazorPagesWebSite/Startup.cs
+++ b/test/WebSites/RazorPagesWebSite/Startup.cs
@@ -10,7 +10,7 @@ namespace RazorPagesWebSite
{
public void ConfigureServices(IServiceCollection services)
{
- services.AddMvc();
+ services.AddMvc().AddCookieTempDataProvider();
}
public void Configure(IApplicationBuilder app)
diff --git a/test/WebSites/RazorPagesWebSite/TempData/SetTempDataOnPageAndRedirect.cshtml b/test/WebSites/RazorPagesWebSite/TempData/SetTempDataOnPageAndRedirect.cshtml
new file mode 100644
index 0000000000..b604b14d54
--- /dev/null
+++ b/test/WebSites/RazorPagesWebSite/TempData/SetTempDataOnPageAndRedirect.cshtml
@@ -0,0 +1,10 @@
+@page
+
+@functions {
+
+ public IActionResult OnGet(string message)
+ {
+ TempData["Message"] = message;
+ return Redirect("~/TempData/ShowMessage");
+ }
+}
\ No newline at end of file
diff --git a/test/WebSites/RazorPagesWebSite/TempData/SetTempDataOnPageModelAndRedirect.cs b/test/WebSites/RazorPagesWebSite/TempData/SetTempDataOnPageModelAndRedirect.cs
new file mode 100644
index 0000000000..3f46ef442d
--- /dev/null
+++ b/test/WebSites/RazorPagesWebSite/TempData/SetTempDataOnPageModelAndRedirect.cs
@@ -0,0 +1,17 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.RazorPages;
+
+namespace RazorPagesWebSite
+{
+ public class SetTempDataOnPageModelAndRedirect : PageModel
+ {
+ public IActionResult OnGet(string message)
+ {
+ TempData["Message"] = message;
+ return Redirect("~/TempData/ShowMessage");
+ }
+ }
+}
diff --git a/test/WebSites/RazorPagesWebSite/TempData/SetTempDataOnPageModelAndRedirect.cshtml b/test/WebSites/RazorPagesWebSite/TempData/SetTempDataOnPageModelAndRedirect.cshtml
new file mode 100644
index 0000000000..8f8ed4ae22
--- /dev/null
+++ b/test/WebSites/RazorPagesWebSite/TempData/SetTempDataOnPageModelAndRedirect.cshtml
@@ -0,0 +1,2 @@
+@page
+@model RazorPagesWebSite.SetTempDataOnPageModelAndRedirect
\ No newline at end of file
diff --git a/test/WebSites/RazorPagesWebSite/TempData/ShowMessage.cshtml b/test/WebSites/RazorPagesWebSite/TempData/ShowMessage.cshtml
new file mode 100644
index 0000000000..f674605030
--- /dev/null
+++ b/test/WebSites/RazorPagesWebSite/TempData/ShowMessage.cshtml
@@ -0,0 +1,3 @@
+@page
+
+@(TempData["Message"])
\ No newline at end of file