Add TempData property and some functional tests

This commit is contained in:
Ryan Nowak 2017-02-21 18:03:27 -08:00
parent ed9025cc5b
commit e11e6b3be7
10 changed files with 155 additions and 5 deletions

View File

@ -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
}
}
/// <summary>
/// Gets the <see cref="ITempDataDictionary"/> from the <see cref="PageContext"/>.
/// </summary>
/// <remarks>Returns null if <see cref="PageContext"/> is null.</remarks>
public ITempDataDictionary TempData => PageContext?.TempData;
/// <inheritdoc />
public override void EnsureRenderedBodyOrSections()
{
@ -90,5 +93,32 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
}
}
/// <summary>
/// Creates a <see cref="RedirectResult"/> object that redirects to the specified <paramref name="url"/>.
/// </summary>
/// <param name="url">The URL to redirect to.</param>
/// <returns>The created <see cref="RedirectResult"/> for the response.</returns>
protected RedirectResult Redirect(string url)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(url));
}
return new RedirectResult(url);
}
/// <summary>
/// Creates a <see cref="PageViewResult"/> object that renders this page as a view to the response.
/// </summary>
/// <returns>The created <see cref="PageViewResult"/> object for the response.</returns>
/// <remarks>
/// Returning a <see cref="PageViewResult"/> from a page handler method is equivalent to returning void.
/// The view associated with the page will be executed.
/// </remarks>
protected PageViewResult View()
{
return new PageViewResult(this);
}
}
}

View File

@ -44,6 +44,12 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
public ModelStateDictionary ModelState => PageContext.ModelState;
/// <summary>
/// Gets the <see cref="ITempDataDictionary"/> from the <see cref="PageContext"/>.
/// </summary>
/// <remarks>Returns null if <see cref="PageContext"/> is null.</remarks>
public ITempDataDictionary TempData => PageContext?.TempData;
public ViewDataDictionary ViewData => PageContext?.ViewData;
protected Task<T> BindAsync<T>(string name)

View File

@ -74,6 +74,22 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
return string.Format(CultureInfo.CurrentCulture, GetString("PageViewResult_ContextIsInvalid"), p0, p1);
}
/// <summary>
/// Value cannot be null or empty.
/// </summary>
internal static string ArgumentCannotBeNullOrEmpty
{
get { return GetString("ArgumentCannotBeNullOrEmpty"); }
}
/// <summary>
/// Value cannot be null or empty.
/// </summary>
internal static string FormatArgumentCannotBeNullOrEmpty()
{
return GetString("ArgumentCannotBeNullOrEmpty");
}
private static string GetString(string name, params string[] formatterNames)
{
var value = _resourceManager.GetString(name);

View File

@ -129,4 +129,7 @@
<data name="PageViewResult_ContextIsInvalid" xml:space="preserve">
<value>Argument '{0}' is not the same instance used to create '{1}'.</value>
</data>
<data name="ArgumentCannotBeNullOrEmpty" xml:space="preserve">
<value>Value cannot be null or empty.</value>
</data>
</root>

View File

@ -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; }
}
}
}

View File

@ -10,7 +10,7 @@ namespace RazorPagesWebSite
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddMvc().AddCookieTempDataProvider();
}
public void Configure(IApplicationBuilder app)

View File

@ -0,0 +1,10 @@
@page
@functions {
public IActionResult OnGet(string message)
{
TempData["Message"] = message;
return Redirect("~/TempData/ShowMessage");
}
}

View File

@ -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");
}
}
}

View File

@ -0,0 +1,2 @@
@page
@model RazorPagesWebSite.SetTempDataOnPageModelAndRedirect

View File

@ -0,0 +1,3 @@
@page
@(TempData["Message"])