Merge branch 'ViewResultTest' into dev

This commit is contained in:
Pranav K 2014-06-03 13:58:07 -07:00
commit 0467fd906b
18 changed files with 561 additions and 69 deletions

View File

@ -409,10 +409,10 @@
</Rules> </Rules>
<AnalyzerSettings /> <AnalyzerSettings />
</Analyzer> </Analyzer>
<Analyzer AnalyzerId="Microsoft.Web.StyleCop.Rules"> <Analyzer AnalyzerId="StyleCop.KRules.FileHeaderRule">
<Rules /> <Rules />
<AnalyzerSettings> <AnalyzerSettings>
<StringProperty Name="FileHeaderText"> Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.</StringProperty> <StringProperty Name="FileHeaderText"> Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.</StringProperty>
</AnalyzerSettings> </AnalyzerSettings>
</Analyzer> </Analyzer>
</Analyzers> </Analyzers>

View File

@ -18,6 +18,8 @@ copy %CACHED_NUGET% .nuget\nuget.exe > nul
IF EXIST packages\KoreBuild goto run IF EXIST packages\KoreBuild goto run
.nuget\NuGet.exe install KoreBuild -ExcludeVersion -o packages -nocache -pre .nuget\NuGet.exe install KoreBuild -ExcludeVersion -o packages -nocache -pre
.nuget\NuGet.exe install Sake -version 0.2 -o packages -ExcludeVersion .nuget\NuGet.exe install Sake -version 0.2 -o packages -ExcludeVersion
IF "%SKIP_KRE_INSTALL%"=="1" goto run
CALL packages\KoreBuild\build\kvm upgrade -svr50 -x86 CALL packages\KoreBuild\build\kvm upgrade -svr50 -x86
CALL packages\KoreBuild\build\kvm install default -svrc50 -x86 CALL packages\KoreBuild\build\kvm install default -svrc50 -x86

View File

@ -17,9 +17,19 @@ namespace MvcSample.Web.Components
.OrderBy(s => Guid.NewGuid().ToString()) .OrderBy(s => Guid.NewGuid().ToString())
.ToArray(); .ToArray();
public async Task<IViewComponentResult> InvokeAsync(int count) public async Task<IViewComponentResult> InvokeAsync(int count, string returnAs)
{ {
var tags = await GetTagsAsync(count); var tags = await GetTagsAsync(count);
if (returnAs.Equals("Content", StringComparison.OrdinalIgnoreCase))
{
return Content(string.Join(", ", tags));
}
if (returnAs.Equals("Json", StringComparison.OrdinalIgnoreCase))
{
return Json(tags);
}
return View(tags); return View(tags);
} }

View File

@ -15,9 +15,9 @@
<section class="validationSummary"> <section class="validationSummary">
@Html.ValidationSummary() @Html.ValidationSummary()
@Html.ValidationSummary(excludePropertyErrors: true) @Html.ValidationSummary(excludePropertyErrors: true)
@Html.ValidationSummary(message: "Hello from validation message summary 1.") @Html.ValidationSummary(message: "Hello from validation message summary 1.", tag: "h2")
@Html.ValidationSummary(excludePropertyErrors: true, message: "Hello from validation message summary 2") @Html.ValidationSummary(excludePropertyErrors: true, message: "Hello from validation message summary 2")
@Html.ValidationSummary(message: "Hello from validation message summary 3", htmlAttributes: new { style = "color: red" }) @Html.ValidationSummary(message: "Hello from validation message summary 3", htmlAttributes: new { style = "color: red" })
@Html.ValidationSummary(excludePropertyErrors: true, message: "Hello from validation message summary 4", htmlAttributes: new { style = "color: green" }) @Html.ValidationSummary(excludePropertyErrors: true, message: "Hello from validation message summary 4", htmlAttributes: new { style = "color: green" })
@Html.ValidationSummary(message: "Hello from validation message summary 5", htmlAttributes: new Dictionary<string, object> { { "style", "color: blue" } }) @Html.ValidationSummary(message: "Hello from validation message summary 5", htmlAttributes: new Dictionary<string, object> { { "style", "color: blue" } }, tag: "h1")
</section> </section>

View File

@ -146,7 +146,12 @@
</table> </table>
</div> </div>
<div style="float: left; border: 5px solid red; margin: 5px;"> <div style="float: left; border: 5px solid red; margin: 5px;">
@await Component.InvokeAsync("Tags", 15) <p>Tags from View():</p>
@await Component.InvokeAsync("Tags", 5, "View")
<p>Tags from Content():</p>
<p>@await Component.InvokeAsync("Tags", 5, "Content")</p>
<p>Tags from Json():</p>
<p>@await Component.InvokeAsync("Tags", 5, "Json")</p>
<p style="padding: 0px 10px;">'@ViewBag.Title' should match page heading (still)</p> <p style="padding: 0px 10px;">'@ViewBag.Title' should match page heading (still)</p>
</div> </div>
<div style="float: left; border: thick solid lightskyblue; margin: 5px; padding: 7px;"> <div style="float: left; border: thick solid lightskyblue; margin: 5px; padding: 7px;">

View File

@ -3,6 +3,7 @@
using System; using System;
using Microsoft.AspNet.Mvc.Core; using Microsoft.AspNet.Mvc.Core;
using Microsoft.Framework.DependencyInjection;
namespace Microsoft.AspNet.Mvc namespace Microsoft.AspNet.Mvc
{ {
@ -30,10 +31,18 @@ namespace Microsoft.AspNet.Mvc
public override void ExecuteResult([NotNull] ActionContext context) public override void ExecuteResult([NotNull] ActionContext context)
{ {
// It is redirected directly to the input URL. var destinationUrl = Url;
// We would use the context to construct the full URL, var urlHelper = context.HttpContext
// only when relative URLs are supported. (Issue - WEBFX-202) .RequestServices
context.HttpContext.Response.Redirect(Url, Permanent); .GetService<IUrlHelper>();
// IsLocalUrl is called to handle Urls starting with '~/'.
if (urlHelper.IsLocalUrl(destinationUrl))
{
destinationUrl = urlHelper.Content(Url);
}
context.HttpContext.Response.Redirect(destinationUrl, Permanent);
} }
} }
} }

View File

@ -443,13 +443,16 @@ namespace Microsoft.AspNet.Mvc.Rendering
} }
/// <inheritdoc /> /// <inheritdoc />
public HtmlString ValidationMessage(string expression, string message, object htmlAttributes) public HtmlString ValidationMessage(string expression, string message, object htmlAttributes, string tag)
{ {
return GenerateValidationMessage(expression, message, htmlAttributes); return GenerateValidationMessage(expression, message, htmlAttributes, tag);
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual HtmlString ValidationSummary(bool excludePropertyErrors, string message, IDictionary<string, object> htmlAttributes) public virtual HtmlString ValidationSummary(bool excludePropertyErrors,
string message,
IDictionary<string, object> htmlAttributes,
string tag)
{ {
var formContext = ViewContext.ClientValidationEnabled ? ViewContext.FormContext : null; var formContext = ViewContext.ClientValidationEnabled ? ViewContext.FormContext : null;
@ -464,16 +467,20 @@ namespace Microsoft.AspNet.Mvc.Rendering
} }
} }
string messageSpan; string wrappedMessage;
if (!string.IsNullOrEmpty(message)) if (!string.IsNullOrEmpty(message))
{ {
var spanTag = new TagBuilder("span"); if (string.IsNullOrEmpty(tag))
spanTag.SetInnerText(message); {
messageSpan = spanTag.ToString(TagRenderMode.Normal) + Environment.NewLine; tag = ViewContext.ValidationSummaryMessageElement;
}
var messageTag = new TagBuilder(tag);
messageTag.SetInnerText(message);
wrappedMessage = messageTag.ToString(TagRenderMode.Normal) + Environment.NewLine;
} }
else else
{ {
messageSpan = null; wrappedMessage = null;
} }
var htmlSummary = new StringBuilder(); var htmlSummary = new StringBuilder();
@ -516,7 +523,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
divBuilder.AddCssClass(HtmlHelper.ValidationSummaryCssClassName); divBuilder.AddCssClass(HtmlHelper.ValidationSummaryCssClassName);
} }
divBuilder.InnerHtml = messageSpan + unorderedList.ToString(TagRenderMode.Normal); divBuilder.InnerHtml = wrappedMessage + unorderedList.ToString(TagRenderMode.Normal);
if (formContext != null) if (formContext != null)
{ {
@ -1261,8 +1268,10 @@ namespace Microsoft.AspNet.Mvc.Rendering
return tagBuilder.ToHtmlString(TagRenderMode.SelfClosing); return tagBuilder.ToHtmlString(TagRenderMode.SelfClosing);
} }
protected virtual HtmlString GenerateValidationMessage(string expression, string message, protected virtual HtmlString GenerateValidationMessage(string expression,
object htmlAttributes) string message,
object htmlAttributes,
string tag)
{ {
var modelName = ViewData.TemplateInfo.GetFullHtmlFieldName(expression); var modelName = ViewData.TemplateInfo.GetFullHtmlFieldName(expression);
if (string.IsNullOrEmpty(modelName)) if (string.IsNullOrEmpty(modelName))
@ -1294,7 +1303,11 @@ namespace Microsoft.AspNet.Mvc.Rendering
// Even if there are no model errors, we generate the span and add the validation message // Even if there are no model errors, we generate the span and add the validation message
// if formContext is not null. // if formContext is not null.
var builder = new TagBuilder("span"); if (string.IsNullOrEmpty(tag))
{
tag = ViewContext.ValidationMessageElement;
}
var builder = new TagBuilder(tag);
builder.MergeAttributes(AnonymousObjectToHtmlAttributes(htmlAttributes)); builder.MergeAttributes(AnonymousObjectToHtmlAttributes(htmlAttributes));
// Only the style of the span is changed according to the errors if message is null or empty. // Only the style of the span is changed according to the errors if message is null or empty.

View File

@ -229,9 +229,14 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// <inheritdoc /> /// <inheritdoc />
public HtmlString ValidationMessageFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression, public HtmlString ValidationMessageFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression,
string message, object htmlAttributes) string message,
object htmlAttributes,
string tag)
{ {
return GenerateValidationMessage(ExpressionHelper.GetExpressionText(expression), message, htmlAttributes); return GenerateValidationMessage(ExpressionHelper.GetExpressionText(expression),
message,
htmlAttributes,
tag);
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@ -12,95 +12,202 @@ namespace Microsoft.AspNet.Mvc.Rendering
public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper, public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper,
string expression) string expression)
{ {
return htmlHelper.ValidationMessage(expression, message: null, htmlAttributes: null); return htmlHelper.ValidationMessage(expression, message: null, htmlAttributes: null, tag: null);
} }
public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper, public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper,
string expression, string message) string expression,
string message)
{ {
return htmlHelper.ValidationMessage(expression, message, htmlAttributes: null); return htmlHelper.ValidationMessage(expression, message, htmlAttributes: null, tag: null);
} }
public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper, public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper,
string expression, object htmlAttributes) string expression,
object htmlAttributes)
{ {
return htmlHelper.ValidationMessage(expression, message: null, htmlAttributes: htmlAttributes); return htmlHelper.ValidationMessage(expression, message: null, htmlAttributes: htmlAttributes, tag: null);
} }
public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper, public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper,
string expression, string message, object htmlAttributes) string expression,
string message,
string tag)
{ {
return htmlHelper.ValidationMessage(expression, message, htmlAttributes); return htmlHelper.ValidationMessage(expression, message, htmlAttributes: null, tag: tag);
}
public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper,
string expression,
string message,
object htmlAttributes)
{
return htmlHelper.ValidationMessage(expression, message, htmlAttributes, tag: null);
}
public static HtmlString ValidationMessage([NotNull] this IHtmlHelper htmlHelper,
string expression,
string message,
object htmlAttributes,
string tag)
{
return htmlHelper.ValidationMessage(expression, message, htmlAttributes, tag);
} }
public static HtmlString ValidationMessageFor<TModel, TProperty>([NotNull] this IHtmlHelper<TModel> htmlHelper, public static HtmlString ValidationMessageFor<TModel, TProperty>([NotNull] this IHtmlHelper<TModel> htmlHelper,
[NotNull] Expression<Func<TModel, TProperty>> expression) [NotNull] Expression<Func<TModel, TProperty>> expression)
{ {
return htmlHelper.ValidationMessageFor(expression, message: null, htmlAttributes: null); return htmlHelper.ValidationMessageFor(expression, message: null, htmlAttributes: null, tag: null);
} }
public static HtmlString ValidationMessageFor<TModel, TProperty>([NotNull] this IHtmlHelper<TModel> htmlHelper, public static HtmlString ValidationMessageFor<TModel, TProperty>([NotNull] this IHtmlHelper<TModel> htmlHelper,
[NotNull] Expression<Func<TModel, TProperty>> expression, string message) [NotNull] Expression<Func<TModel, TProperty>> expression,
string message)
{ {
return htmlHelper.ValidationMessageFor(expression, message, htmlAttributes: null); return htmlHelper.ValidationMessageFor(expression, message, htmlAttributes: null, tag: null);
} }
public static HtmlString ValidationMessageFor<TModel, TProperty>([NotNull] this IHtmlHelper<TModel> htmlHelper, public static HtmlString ValidationMessageFor<TModel, TProperty>([NotNull] this IHtmlHelper<TModel> htmlHelper,
[NotNull] Expression<Func<TModel, TProperty>> expression, string message, object htmlAttributes) [NotNull] Expression<Func<TModel, TProperty>> expression,
string message,
object htmlAttributes)
{ {
return htmlHelper.ValidationMessageFor(expression, message, htmlAttributes); return htmlHelper.ValidationMessageFor(expression, message, htmlAttributes, tag: null);
}
public static HtmlString ValidationMessageFor<TModel, TProperty>([NotNull] this IHtmlHelper<TModel> htmlHelper,
[NotNull] Expression<Func<TModel, TProperty>> expression,
string message,
string tag)
{
return htmlHelper.ValidationMessageFor(expression, message, htmlAttributes: null, tag: tag);
}
public static HtmlString ValidationMessageFor<TModel, TProperty>([NotNull] this IHtmlHelper<TModel> htmlHelper,
[NotNull] Expression<Func<TModel, TProperty>> expression,
string message,
object htmlAttributes,
string tag)
{
return htmlHelper.ValidationMessageFor(expression, message, htmlAttributes, tag);
} }
public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper) public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper)
{ {
return ValidationSummary(htmlHelper, excludePropertyErrors: false); return htmlHelper.ValidationSummary(excludePropertyErrors: false,
message: null,
htmlAttributes: null,
tag: null);
} }
public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper, bool excludePropertyErrors) public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper, bool excludePropertyErrors)
{ {
return ValidationSummary(htmlHelper, excludePropertyErrors, message: null); return htmlHelper.ValidationSummary(excludePropertyErrors,
message: null,
htmlAttributes: null,
tag: null);
} }
public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper, string message) public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper, string message)
{ {
return ValidationSummary(htmlHelper, excludePropertyErrors: false, message: message, return htmlHelper.ValidationSummary(excludePropertyErrors: false,
htmlAttributes: (object)null); message: message,
htmlAttributes: null,
tag: null);
} }
public static HtmlString ValidationSummary( public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper, string message, string tag)
[NotNull] this IHtmlHelper htmlHelper, {
return htmlHelper.ValidationSummary(excludePropertyErrors: false,
message: message,
htmlAttributes: null,
tag: tag);
}
public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper,
bool excludePropertyErrors, bool excludePropertyErrors,
string message) string message)
{ {
return ValidationSummary(htmlHelper, excludePropertyErrors, message, htmlAttributes: (object)null); return htmlHelper.ValidationSummary(excludePropertyErrors,
message,
htmlAttributes: null,
tag: null);
} }
public static HtmlString ValidationSummary( public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper,
[NotNull] this IHtmlHelper htmlHelper,
string message, string message,
object htmlAttributes) object htmlAttributes)
{ {
return ValidationSummary(htmlHelper, excludePropertyErrors: false, message: message, return htmlHelper.ValidationSummary(excludePropertyErrors: false,
htmlAttributes: HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); message: message,
htmlAttributes: HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),
tag: null);
} }
public static HtmlString ValidationSummary( public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper,
[NotNull] this IHtmlHelper htmlHelper, string message,
object htmlAttributes,
string tag)
{
return htmlHelper.ValidationSummary(excludePropertyErrors: false,
message: message,
htmlAttributes: HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),
tag: tag);
}
public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper,
bool excludePropertyErrors,
string message,
string tag)
{
return htmlHelper.ValidationSummary(excludePropertyErrors,
message,
htmlAttributes: null,
tag: tag);
}
public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper,
bool excludePropertyErrors, bool excludePropertyErrors,
string message, string message,
object htmlAttributes) object htmlAttributes)
{ {
return htmlHelper.ValidationSummary(excludePropertyErrors, message, return htmlHelper.ValidationSummary(excludePropertyErrors,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); message,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),
tag: null);
} }
public static HtmlString ValidationSummary( public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper,
[NotNull] this IHtmlHelper htmlHelper, bool excludePropertyErrors,
string message,
object htmlAttributes,
string tag)
{
return htmlHelper.ValidationSummary(excludePropertyErrors,
message,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),
tag);
}
public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper,
string message, string message,
IDictionary<string, object> htmlAttributes) IDictionary<string, object> htmlAttributes)
{ {
return htmlHelper.ValidationSummary(excludePropertyErrors: false, message: message, return htmlHelper.ValidationSummary(excludePropertyErrors: false,
htmlAttributes: htmlAttributes); message: message,
htmlAttributes: htmlAttributes,
tag: null);
}
public static HtmlString ValidationSummary([NotNull] this IHtmlHelper htmlHelper,
string message,
IDictionary<string, object> htmlAttributes,
string tag)
{
return htmlHelper.ValidationSummary(excludePropertyErrors: false,
message: message,
htmlAttributes: htmlAttributes,
tag: tag);
} }
} }
} }

View File

@ -447,8 +447,10 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// <param name="htmlAttributes"> An object that contains the HTML attributes to set for the element. /// <param name="htmlAttributes"> An object that contains the HTML attributes to set for the element.
/// Alternatively, an <see cref="IDictionary{string, object}"/> instance containing the HTML attributes. /// Alternatively, an <see cref="IDictionary{string, object}"/> instance containing the HTML attributes.
/// </param> /// </param>
/// <param name="tag">The tag to wrap the <paramref name="message"/> in the generated HTML.
/// Its default value is <see cref="ViewContext.ValidationMessageElement" />.</param>
/// <returns>An <see cref="HtmlString"/> that contains the validation message</returns> /// <returns>An <see cref="HtmlString"/> that contains the validation message</returns>
HtmlString ValidationMessage(string modelName, string message, object htmlAttributes); HtmlString ValidationMessage(string modelName, string message, object htmlAttributes, string tag);
/// <summary> /// <summary>
/// Returns an unordered list (ul element) of validation messages that are in the /// Returns an unordered list (ul element) of validation messages that are in the
@ -458,12 +460,15 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// have the summary display all errors.</param> /// have the summary display all errors.</param>
/// <param name="message">The message to display with the validation summary.</param> /// <param name="message">The message to display with the validation summary.</param>
/// <param name="htmlAttributes">A dictionary that contains the HTML attributes for the element.</param> /// <param name="htmlAttributes">A dictionary that contains the HTML attributes for the element.</param>
/// <param name="tag">The tag to wrap the <paramref name="message"/> in the generated HTML.
/// Its default value is <see cref="ViewContext.ValidationMessageElement" />.</param>
/// <returns>An <see cref="HtmlString"/> that contains an unordered list (ul element) of validation messages. /// <returns>An <see cref="HtmlString"/> that contains an unordered list (ul element) of validation messages.
/// </returns> /// </returns>
HtmlString ValidationSummary( HtmlString ValidationSummary(
bool excludePropertyErrors, bool excludePropertyErrors,
string message, string message,
IDictionary<string, object> htmlAttributes); IDictionary<string, object> htmlAttributes,
string tag);
/// <summary> /// <summary>
/// Returns the model value for the given expression <paramref name="name"/>. /// Returns the model value for the given expression <paramref name="name"/>.

View File

@ -253,9 +253,13 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// <param name="htmlAttributes"> An object that contains the HTML attributes to set for the element. /// <param name="htmlAttributes"> An object that contains the HTML attributes to set for the element.
/// Alternatively, an /// <see cref="IDictionary{string, object}"/> instance containing the HTML attributes. /// Alternatively, an /// <see cref="IDictionary{string, object}"/> instance containing the HTML attributes.
/// </param> /// </param>
/// <param name="tag">The tag to wrap the <paramref name="message"/> in the generated HTML.
/// Its default value is <see cref="ViewContext.ValidationMessageElement" />.</param>
/// <returns>An <see cref="HtmlString"/> that contains the validation message</returns> /// <returns>An <see cref="HtmlString"/> that contains the validation message</returns>
HtmlString ValidationMessageFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression, HtmlString ValidationMessageFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression,
string message, object htmlAttributes); string message,
object htmlAttributes,
string tag);
/// <summary> /// <summary>
/// Returns the model value for the given expression <paramref name="expression"/>. /// Returns the model value for the given expression <paramref name="expression"/>.

View File

@ -14,17 +14,17 @@ namespace Microsoft.AspNet.Mvc
_viewEngine = viewEngine; _viewEngine = viewEngine;
} }
public IViewComponentResult Content([NotNull] string content) public virtual ContentViewComponentResult Content([NotNull] string content)
{ {
return new ContentViewComponentResult(content); return new ContentViewComponentResult(content);
} }
public IViewComponentResult Json([NotNull] object value) public virtual JsonViewComponentResult Json([NotNull] object value)
{ {
return new JsonViewComponentResult(value); return new JsonViewComponentResult(value);
} }
public IViewComponentResult View([NotNull] string viewName, [NotNull] ViewDataDictionary viewData) public virtual ViewViewComponentResult View([NotNull] string viewName, [NotNull] ViewDataDictionary viewData)
{ {
return new ViewViewComponentResult(_viewEngine, viewName, viewData); return new ViewViewComponentResult(_viewEngine, viewName, viewData);
} }

View File

@ -7,10 +7,10 @@ namespace Microsoft.AspNet.Mvc
{ {
public interface IViewComponentResultHelper public interface IViewComponentResultHelper
{ {
IViewComponentResult Content([NotNull] string content); ContentViewComponentResult Content([NotNull] string content);
IViewComponentResult Json([NotNull] object value); JsonViewComponentResult Json([NotNull] object value);
IViewComponentResult View([NotNull] string viewName, [NotNull] ViewDataDictionary viewData); ViewViewComponentResult View([NotNull] string viewName, [NotNull] ViewDataDictionary viewData);
} }
} }

View File

@ -35,27 +35,37 @@ namespace Microsoft.AspNet.Mvc
public ViewDataDictionary ViewData { get; set; } public ViewDataDictionary ViewData { get; set; }
public ContentViewComponentResult Content(string content)
{
return Result.Content(content);
}
public void Initialize(IViewComponentResultHelper result) public void Initialize(IViewComponentResultHelper result)
{ {
Result = result; Result = result;
} }
public IViewComponentResult View() public JsonViewComponentResult Json(object value)
{
return Result.Json(value);
}
public ViewViewComponentResult View()
{ {
return View<object>(null, null); return View<object>(null, null);
} }
public IViewComponentResult View(string viewName) public ViewViewComponentResult View(string viewName)
{ {
return View<object>(viewName, null); return View<object>(viewName, null);
} }
public IViewComponentResult View<TModel>(TModel model) public ViewViewComponentResult View<TModel>(TModel model)
{ {
return View(null, model); return View(null, model);
} }
public IViewComponentResult View<TModel>(string viewName, TModel model) public ViewViewComponentResult View<TModel>(string viewName, TModel model)
{ {
var viewData = new ViewDataDictionary<TModel>(ViewData); var viewData = new ViewDataDictionary<TModel>(ViewData);
if (model != null) if (model != null)

View File

@ -29,6 +29,8 @@ namespace Microsoft.AspNet.Mvc
_formContext = _defaultFormContext; _formContext = _defaultFormContext;
UnobtrusiveJavaScriptEnabled = true; UnobtrusiveJavaScriptEnabled = true;
ClientValidationEnabled = true; ClientValidationEnabled = true;
ValidationSummaryMessageElement = "span";
ValidationMessageElement = "span";
} }
public ViewContext( public ViewContext(
@ -41,6 +43,8 @@ namespace Microsoft.AspNet.Mvc
_formContext = viewContext.FormContext; _formContext = viewContext.FormContext;
UnobtrusiveJavaScriptEnabled = viewContext.UnobtrusiveJavaScriptEnabled; UnobtrusiveJavaScriptEnabled = viewContext.UnobtrusiveJavaScriptEnabled;
ClientValidationEnabled = viewContext.ClientValidationEnabled; ClientValidationEnabled = viewContext.ClientValidationEnabled;
ValidationSummaryMessageElement = viewContext.ValidationSummaryMessageElement;
ValidationMessageElement = viewContext.ValidationMessageElement;
View = view; View = view;
ViewData = viewData; ViewData = viewData;
@ -64,6 +68,18 @@ namespace Microsoft.AspNet.Mvc
public bool ClientValidationEnabled { get; set; } public bool ClientValidationEnabled { get; set; }
/// <summary>
/// Element name used to wrap a top-level message generated by
/// <see cref="HtmlHelperValidationExtensions.ValidationSummary(IHtmlHelper)"/> and other overloads.
/// </summary>
public string ValidationSummaryMessageElement { get; set; }
/// <summary>
/// Element name used to wrap a top-level message generated by
/// <see cref="HtmlHelperValidationExtensions.ValidationMessage(IHtmlHelper, string)"/> and other overloads.
/// </summary>
public string ValidationMessageElement { get; set; }
public dynamic ViewBag public dynamic ViewBag
{ {
get get

View File

@ -0,0 +1,108 @@
// Copyright (c) Microsoft Open Technologies, Inc. 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.Collections.Generic;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc.Core.Test
{
public class RedirectResultTest
{
[Theory]
[InlineData("", "/Home/About", "/Home/About")]
[InlineData("/myapproot", "/test", "/test")]
public void Execute_ReturnsContentPath_WhenItDoesNotStartWithTilde(string appRoot,
string contentPath,
string expectedPath)
{
// Arrange
var httpResponse = new Mock<HttpResponse>();
httpResponse.Setup(o => o.Redirect(expectedPath, false))
.Verifiable();
var httpContext = GetHttpContext(appRoot, contentPath, expectedPath, httpResponse.Object);
var actionContext = GetActionContext(httpContext);
var result = new RedirectResult(contentPath);
// Act
result.ExecuteResult(actionContext);
// Assert
// Verifying if Redirect was called with the specific Url and parameter flag.
httpResponse.Verify();
}
[Theory]
[InlineData(null, "~/Home/About", "/Home/About")]
[InlineData("/", "~/Home/About", "/Home/About")]
[InlineData("/", "~/", "/")]
[InlineData("", "~/Home/About", "/Home/About")]
[InlineData("/myapproot", "~/", "/myapproot/")]
[InlineData("", "~/Home/About", "/Home/About")]
[InlineData("/myapproot", "~/", "/myapproot/")]
public void Execute_ReturnsAppRelativePath_WhenItStartsWithTilde(string appRoot,
string contentPath,
string expectedPath)
{
// Arrange
var httpResponse = new Mock<HttpResponse>();
httpResponse.Setup(o => o.Redirect(expectedPath, false))
.Verifiable();
var httpContext = GetHttpContext(appRoot, contentPath, expectedPath, httpResponse.Object);
var actionContext = GetActionContext(httpContext);
var result = new RedirectResult(contentPath);
// Act
result.ExecuteResult(actionContext);
// Assert
// Verifying if Redirect was called with the specific Url and parameter flag.
httpResponse.Verify();
}
private static ActionContext GetActionContext(HttpContext httpContext)
{
return new ActionContext(httpContext,
Mock.Of<IRouter>(),
new Dictionary<string, object>(),
new ActionDescriptor());
}
private static IServiceProvider GetServiceProvider(IUrlHelper urlHelper)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddInstance<IUrlHelper>(urlHelper);
return serviceCollection.BuildServiceProvider();
}
private static HttpContext GetHttpContext(string appRoot,
string contentPath,
string expectedPath,
HttpResponse response)
{
var httpContext = new Mock<HttpContext>();
var actionContext = GetActionContext(httpContext.Object);
var mockContentAccessor = new Mock<IContextAccessor<ActionContext>>();
mockContentAccessor.SetupGet(o => o.Value).Returns(actionContext);
var mockActionSelector = new Mock<IActionSelector>();
var urlHelper = new UrlHelper(mockContentAccessor.Object, mockActionSelector.Object);
var serviceProvider = GetServiceProvider(urlHelper);
httpContext.Setup(o => o.Response)
.Returns(response);
httpContext.SetupGet(o => o.RequestServices)
.Returns(serviceProvider);
httpContext.Setup(o => o.Request.PathBase)
.Returns(new PathString(appRoot));
return httpContext.Object;
}
}
}

View File

@ -25,6 +25,7 @@
<Compile Include="ActionExecutorTests.cs" /> <Compile Include="ActionExecutorTests.cs" />
<Compile Include="ActionResults\RedirectToActionResultTest.cs" /> <Compile Include="ActionResults\RedirectToActionResultTest.cs" />
<Compile Include="ActionResults\RedirectToRouteResultTest.cs" /> <Compile Include="ActionResults\RedirectToRouteResultTest.cs" />
<Compile Include="ActionResults\RedirectResultTest.cs" />
<Compile Include="ActionSelectionConventionTests.cs" /> <Compile Include="ActionSelectionConventionTests.cs" />
<Compile Include="AntiXsrf\AntiForgeryTokenSerializerTest.cs" /> <Compile Include="AntiXsrf\AntiForgeryTokenSerializerTest.cs" />
<Compile Include="AntiXsrf\ITokenProvider.cs" /> <Compile Include="AntiXsrf\ITokenProvider.cs" />
@ -54,6 +55,7 @@
<Compile Include="TypeHelperTest.cs" /> <Compile Include="TypeHelperTest.cs" />
<Compile Include="UrlHelperTest.cs" /> <Compile Include="UrlHelperTest.cs" />
<Compile Include="ViewResultTest.cs" /> <Compile Include="ViewResultTest.cs" />
<Compile Include="ViewComponentTests.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" /> <Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project> </Project>

View File

@ -0,0 +1,196 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc
{
public class ViewComponentTests
{
[Fact]
public void ViewComponent_ViewBag_UsesViewData()
{
// Arrange
var viewComponent = new TestViewComponent()
{
ViewData = new ViewDataDictionary(metadataProvider: null),
};
// Act
viewComponent.ViewBag.A = "Alice";
viewComponent.ViewBag.B = "Bob";
// Assert
Assert.Equal(2, viewComponent.ViewData.Count);
Assert.Equal("Alice", viewComponent.ViewData["A"]);
Assert.Equal("Bob", viewComponent.ViewData["B"]);
}
[Fact]
public void ViewComponent_ViewData_StoresDataForViewBag()
{
// Arrange
var viewComponent = new TestViewComponent()
{
ViewData = new ViewDataDictionary(metadataProvider: null),
};
// Act
viewComponent.ViewData["A"] = "Alice";
viewComponent.ViewData["B"] = "Bob";
// Assert
Assert.Equal(2, viewComponent.ViewData.Count);
Assert.Equal("Alice", viewComponent.ViewBag.A);
Assert.Equal("Bob", viewComponent.ViewBag.B);
}
[Fact]
public void ViewComponent_Content_CallsResultContentWithTestContent()
{
// Arrange
var viewComponent = new TestViewComponent();
var resultHelperMock = new Mock<DefaultViewComponentResultHelper>(It.IsAny<IViewEngine>());
var resultMock = new Mock<ContentViewComponentResult>("TestContent");
resultHelperMock.Setup(r => r.Content(It.IsAny<string>()))
.Returns(resultMock.Object);
viewComponent.Initialize(resultHelperMock.Object);
// Act
var actualResult = viewComponent.Content("TestContent");
// Assert
resultHelperMock.Verify(r => r.Content("TestContent"));
Assert.Same(resultMock.Object, actualResult);
}
[Fact]
public void ViewComponent_Json_CallsResultJsonWithTestValue()
{
// Arrange
var viewComponent = new TestViewComponent();
var resultHelperMock = new Mock<DefaultViewComponentResultHelper>(It.IsAny<IViewEngine>());
var resultMock = new Mock<JsonViewComponentResult>(It.IsAny<object>());
resultHelperMock.Setup(r => r.Json(It.IsAny<object>()))
.Returns(resultMock.Object);
viewComponent.Initialize(resultHelperMock.Object);
var testValue = new object();
// Act
var actualResult = viewComponent.Json(testValue);
// Assert
resultHelperMock.Verify(r => r.Json(testValue));
Assert.Same(resultMock.Object, actualResult);
}
[Fact]
public void ViewComponent_View_WithEmptyParameter_CallsResultViewWithDefaultViewName()
{
// Arrange
var viewComponent = new TestViewComponent()
{
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
};
var resultHelperMock = new Mock<DefaultViewComponentResultHelper>(It.IsAny<IViewEngine>());
var resultMock = new Mock<ViewViewComponentResult>(It.IsAny<IViewEngine>(),
It.IsAny<string>(),
It.IsAny<ViewDataDictionary>());
resultHelperMock.Setup(r => r.View(It.IsAny<string>(), It.IsAny<ViewDataDictionary>()))
.Returns(resultMock.Object);
viewComponent.Initialize(resultHelperMock.Object);
// Act
var actualResult = viewComponent.View();
// Assert
resultHelperMock.Verify(r => r.View("Default", viewComponent.ViewData));
Assert.Same(resultMock.Object, actualResult);
}
[Fact]
public void ViewComponent_View_WithViewNameParameter_CallsResultViewWithCustomViewName()
{
// Arrange
var viewComponent = new TestViewComponent()
{
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
};
var resultHelperMock = new Mock<DefaultViewComponentResultHelper>(It.IsAny<IViewEngine>());
var resultMock = new Mock<ViewViewComponentResult>(It.IsAny<IViewEngine>(),
It.IsAny<string>(),
It.IsAny<ViewDataDictionary>());
resultHelperMock.Setup(r => r.View(It.IsAny<string>(), It.IsAny<ViewDataDictionary>()))
.Returns(resultMock.Object);
viewComponent.Initialize(resultHelperMock.Object);
// Act
var actualResult = viewComponent.View("CustomViewName");
// Assert
resultHelperMock.Verify(r => r.View("CustomViewName", viewComponent.ViewData));
Assert.Same(resultMock.Object, actualResult);
}
[Fact]
public void ViewComponent_View_WithModelParameter_CallsResultViewWithDefaultViewNameAndModel()
{
// Arrange
var viewComponent = new TestViewComponent()
{
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
};
var resultHelperMock = new Mock<DefaultViewComponentResultHelper>(It.IsAny<IViewEngine>());
var resultMock = new Mock<ViewViewComponentResult>(It.IsAny<IViewEngine>(),
It.IsAny<string>(),
It.IsAny<ViewDataDictionary>());
resultHelperMock.Setup(r => r.View(It.IsAny<string>(), It.IsAny<ViewDataDictionary>()))
.Returns(resultMock.Object);
viewComponent.Initialize(resultHelperMock.Object);
var model = new object();
// Act
var actualResult = viewComponent.View(model);
// Assert
resultHelperMock.Verify(r => r.View("Default", viewComponent.ViewData));
Assert.Same(resultMock.Object, actualResult);
}
[Fact]
public void ViewComponent_View_WithViewNameAndModelParameters_CallsResultViewWithCustomViewNameAndModel()
{
// Arrange
var viewComponent = new TestViewComponent()
{
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
};
var resultHelperMock = new Mock<DefaultViewComponentResultHelper>(It.IsAny<IViewEngine>());
var resultMock = new Mock<ViewViewComponentResult>(It.IsAny<IViewEngine>(),
It.IsAny<string>(),
It.IsAny<ViewDataDictionary>());
resultHelperMock.Setup(r => r.View(It.IsAny<string>(), It.IsAny<ViewDataDictionary>()))
.Returns(resultMock.Object);
viewComponent.Initialize(resultHelperMock.Object);
var model = new object();
// Act
var actualResult = viewComponent.View("CustomViewName", model);
// Assert
resultHelperMock.Verify(r => r.View("CustomViewName", viewComponent.ViewData));
Assert.Same(resultMock.Object, actualResult);
}
private class TestViewComponent : ViewComponent
{
}
}
}