Add TagHelper functional test.
- Added an end-to-end test that verifies all content behaviors, interactions and functionalities of tag helpers. - Added some common user scenarios to verify that the system works how we expect. #1116
This commit is contained in:
parent
b68fae9b8c
commit
c87de5a0fc
13
Mvc.sln
13
Mvc.sln
|
|
@ -83,6 +83,8 @@ EndProject
|
||||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ApiExplorerWebSite", "test\WebSites\ApiExplorerWebSite\ApiExplorerWebSite.kproj", "{61061528-071E-424E-965A-07BCC2F02672}"
|
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ApiExplorerWebSite", "test\WebSites\ApiExplorerWebSite\ApiExplorerWebSite.kproj", "{61061528-071E-424E-965A-07BCC2F02672}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "VersioningWebSite", "test\WebSites\VersioningWebSite\VersioningWebSite.kproj", "{C6304029-78C8-4604-99BE-2078DCA1DD36}"
|
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "VersioningWebSite", "test\WebSites\VersioningWebSite\VersioningWebSite.kproj", "{C6304029-78C8-4604-99BE-2078DCA1DD36}"
|
||||||
|
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ReflectedModelWebSite", "test\WebSites\ReflectedModelWebSite\ReflectedModelWebSite.kproj", "{C2EF54F8-8886-4260-A322-44F76245F95D}"
|
||||||
|
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TagHelpersWebSite", "test\WebSites\TagHelpersWebSite\TagHelpersWebSite.kproj", "{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "FilesWebSite", "test\WebSites\FilesWebSite\FilesWebSite.kproj", "{0EF9860B-10D7-452F-B0F4-A405B88BEBB3}"
|
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "FilesWebSite", "test\WebSites\FilesWebSite\FilesWebSite.kproj", "{0EF9860B-10D7-452F-B0F4-A405B88BEBB3}"
|
||||||
EndProject
|
EndProject
|
||||||
|
|
@ -470,6 +472,16 @@ Global
|
||||||
{CAE52CB7-0FAC-4B5B-8251-B0FF837DB657}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
{CAE52CB7-0FAC-4B5B-8251-B0FF837DB657}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||||
{CAE52CB7-0FAC-4B5B-8251-B0FF837DB657}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
{CAE52CB7-0FAC-4B5B-8251-B0FF837DB657}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||||
{CAE52CB7-0FAC-4B5B-8251-B0FF837DB657}.Release|x86.ActiveCfg = Release|Any CPU
|
{CAE52CB7-0FAC-4B5B-8251-B0FF837DB657}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
|
||||||
|
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
|
||||||
|
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||||
|
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||||
|
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
@ -513,5 +525,6 @@ Global
|
||||||
{0EF9860B-10D7-452F-B0F4-A405B88BEBB3} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
|
{0EF9860B-10D7-452F-B0F4-A405B88BEBB3} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
|
||||||
{2B2B9876-903C-4065-8D62-2EE832BBA106} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
|
{2B2B9876-903C-4065-8D62-2EE832BBA106} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
|
||||||
{CAE52CB7-0FAC-4B5B-8251-B0FF837DB657} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
|
{CAE52CB7-0FAC-4B5B-8251-B0FF837DB657} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
|
||||||
|
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
// 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.Net;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using BasicWebSite;
|
||||||
|
using Microsoft.AspNet.Builder;
|
||||||
|
using Microsoft.AspNet.TestHost;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
|
{
|
||||||
|
public class TagHelpersTests
|
||||||
|
{
|
||||||
|
private readonly IServiceProvider _provider = TestHelper.CreateServices("TagHelpersWebSite");
|
||||||
|
private readonly Action<IApplicationBuilder> _app = new Startup().Configure;
|
||||||
|
|
||||||
|
// Some tests require comparing the actual response body against an expected response baseline
|
||||||
|
// so they require a reference to the assembly on which the resources are located, in order to
|
||||||
|
// make the tests less verbose, we get a reference to the assembly with the resources and we
|
||||||
|
// use it on all the rest of the tests.
|
||||||
|
private readonly Assembly _resourcesAssembly = typeof(TagHelpersTests).GetTypeInfo().Assembly;
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("Index")]
|
||||||
|
[InlineData("About")]
|
||||||
|
[InlineData("Help")]
|
||||||
|
public async Task CanRenderViewsWithTagHelpers(string action)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = TestServer.Create(_provider, _app);
|
||||||
|
var client = server.CreateClient();
|
||||||
|
var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");
|
||||||
|
|
||||||
|
// The K runtime compiles every file under compiler/resources as a resource at runtime with the same name
|
||||||
|
// as the file name, in order to update a baseline you just need to change the file in that folder.
|
||||||
|
var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync(
|
||||||
|
"compiler/resources/TagHelpersWebSite.Home." + action + ".html");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
|
||||||
|
// The host is not important as everything runs in memory and tests are isolated from each other.
|
||||||
|
var response = await client.GetAsync("http://localhost/Home/" + action);
|
||||||
|
var responseContent = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
|
||||||
|
Assert.Equal(expectedContent, responseContent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8"></meta>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
|
||||||
|
<title>About - My ASP.NET Application</title>
|
||||||
|
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1 style="font-family: cursive;">ASP.NET vNext - About</h1>
|
||||||
|
<p>| <a href="/" style="background-color: gray;
|
||||||
|
color: white;
|
||||||
|
border-radius: 3px;
|
||||||
|
border: 1px solid black;
|
||||||
|
padding: 3px;
|
||||||
|
font-family: cursive;">My Home</a>
|
||||||
|
| <a href="/home/about" style="background-color: gray;
|
||||||
|
color: white;
|
||||||
|
border-radius: 3px;
|
||||||
|
border: 1px solid black;
|
||||||
|
padding: 3px;
|
||||||
|
font-family: cursive;">My About</a>
|
||||||
|
| <a href="/home/help" style="background-color: gray;
|
||||||
|
color: white;
|
||||||
|
border-radius: 3px;
|
||||||
|
border: 1px solid black;
|
||||||
|
padding: 3px;
|
||||||
|
font-family: cursive;">My Help</a> |</p>
|
||||||
|
<div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<p>Hello, you've reached the about page.</p>
|
||||||
|
|
||||||
|
<h3>Information about our website (outdated):</h3>
|
||||||
|
<section><p><strong>Version:</strong> 1.1</p>
|
||||||
|
<p><strong>Copyright Year:</strong> 1990</p>
|
||||||
|
<p><strong>Approved:</strong> True</p>
|
||||||
|
<p><strong>Number of tags to show:</strong> 30</p>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
<hr></hr>
|
||||||
|
<footer>
|
||||||
|
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8"></meta>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
|
||||||
|
<title>Help - My ASP.NET Application</title>
|
||||||
|
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1 style="font-family: cursive;">ASP.NET vNext - Help</h1>
|
||||||
|
<p>| <a href="/" style="background-color: gray;
|
||||||
|
color: white;
|
||||||
|
border-radius: 3px;
|
||||||
|
border: 1px solid black;
|
||||||
|
padding: 3px;
|
||||||
|
font-family: cursive;">My Home</a>
|
||||||
|
| <a href="/home/about" style="background-color: gray;
|
||||||
|
color: white;
|
||||||
|
border-radius: 3px;
|
||||||
|
border: 1px solid black;
|
||||||
|
padding: 3px;
|
||||||
|
font-family: cursive;">My About</a>
|
||||||
|
| <a href="/home/help" style="background-color: gray;
|
||||||
|
color: white;
|
||||||
|
border-radius: 3px;
|
||||||
|
border: 1px solid black;
|
||||||
|
padding: 3px;
|
||||||
|
font-family: cursive;">My Help</a> |</p>
|
||||||
|
<div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<p>Hello, you've reached the help page. If you're having troubles try visiting our <a href="/?approved=true">My Approved Home Page</a></p>
|
||||||
|
</div>
|
||||||
|
<hr></hr>
|
||||||
|
<footer>
|
||||||
|
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8"></meta>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
|
||||||
|
<title>Home Page - My ASP.NET Application</title>
|
||||||
|
|
||||||
|
|
||||||
|
<style>
|
||||||
|
h1 {
|
||||||
|
color:red;
|
||||||
|
font-size:2em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1 style="font-family: cursive;">ASP.NET vNext - Home Page</h1>
|
||||||
|
<p>| <a href="/" style="background-color: gray;
|
||||||
|
color: white;
|
||||||
|
border-radius: 3px;
|
||||||
|
border: 1px solid black;
|
||||||
|
padding: 3px;
|
||||||
|
font-family: cursive;">My Home</a>
|
||||||
|
| <a href="/home/about" style="background-color: gray;
|
||||||
|
color: white;
|
||||||
|
border-radius: 3px;
|
||||||
|
border: 1px solid black;
|
||||||
|
padding: 3px;
|
||||||
|
font-family: cursive;">My About</a>
|
||||||
|
| <a href="/home/help" style="background-color: gray;
|
||||||
|
color: white;
|
||||||
|
border-radius: 3px;
|
||||||
|
border: 1px solid black;
|
||||||
|
padding: 3px;
|
||||||
|
font-family: cursive;">My Help</a> |</p>
|
||||||
|
<div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<p>This website has <strong style="font-size: 1.25em;
|
||||||
|
text-decoration: underline;">not</strong> been approved yet. Visit <strong><a target="_blank" href="http://www.contoso.com">www.contoso.com</a></strong> for <strong>more</strong> information.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<h3 style="font-family: cursive;">Current Tag Cloud from Tag Helper</h3>
|
||||||
|
<section>["Lorem","ipsum","dolor","sit","amet","consectetur","adipisicing","elit","sed","do","eiusmod","tempor","incididunt","ut","labore","et","dolore","magna","aliquaUt","enim"]</section>
|
||||||
|
<h3 style="font-family: cursive;">Current Tag Cloud from ViewComponentHelper:</h3>
|
||||||
|
<section>["Lorem","ipsum","dolor","sit","amet","consectetur","adipisicing","elit","sed","do","eiusmod","tempor","incididunt","ut","labore"]</section>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<hr></hr>
|
||||||
|
<footer>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
@ -33,6 +33,7 @@
|
||||||
"Microsoft.Framework.Logging": "1.0.0-*",
|
"Microsoft.Framework.Logging": "1.0.0-*",
|
||||||
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*",
|
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*",
|
||||||
"Microsoft.AspNet.PipelineCore": "1.0.0-*",
|
"Microsoft.AspNet.PipelineCore": "1.0.0-*",
|
||||||
|
"TagHelpersWebSite": "1.0.0",
|
||||||
"Xunit.KRunner": "1.0.0-*"
|
"Xunit.KRunner": "1.0.0-*"
|
||||||
},
|
},
|
||||||
"commands": {
|
"commands": {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
// 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 Microsoft.AspNet.Mvc;
|
||||||
|
using TagHelpersWebSite.Models;
|
||||||
|
|
||||||
|
namespace TagHelpersWebSite.Controllers
|
||||||
|
{
|
||||||
|
public class HomeController : Controller
|
||||||
|
{
|
||||||
|
public IActionResult Index(bool approved = false)
|
||||||
|
{
|
||||||
|
return View(new WebsiteContext
|
||||||
|
{
|
||||||
|
Approved = approved,
|
||||||
|
CopyrightYear = 2015,
|
||||||
|
Version = new Version(1, 3, 3, 7),
|
||||||
|
TagsToShow = 20
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult About()
|
||||||
|
{
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult Help()
|
||||||
|
{
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
namespace TagHelpersWebSite.Models
|
||||||
|
{
|
||||||
|
public class WebsiteContext
|
||||||
|
{
|
||||||
|
public Version Version { get; set; }
|
||||||
|
|
||||||
|
public int CopyrightYear { get; set; }
|
||||||
|
|
||||||
|
public bool Approved { get; set; }
|
||||||
|
|
||||||
|
public int TagsToShow { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
// 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 Microsoft.AspNet.Builder;
|
||||||
|
using Microsoft.Framework.DependencyInjection;
|
||||||
|
|
||||||
|
namespace TagHelpersWebSite
|
||||||
|
{
|
||||||
|
public class Startup
|
||||||
|
{
|
||||||
|
public void Configure(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
var configuration = app.GetTestConfiguration();
|
||||||
|
|
||||||
|
app.UseServices(services =>
|
||||||
|
{
|
||||||
|
services.AddMvc(configuration);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.UseMvc();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
// 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.Linq;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||||
|
using Microsoft.AspNet.Razor.TagHelpers;
|
||||||
|
|
||||||
|
namespace TagHelpersWebSite.TagHelpers
|
||||||
|
{
|
||||||
|
[ContentBehavior(ContentBehavior.Prepend)]
|
||||||
|
public class ATagHelper : TagHelper
|
||||||
|
{
|
||||||
|
[Activate]
|
||||||
|
public IUrlHelper UrlHelper { get; set; }
|
||||||
|
|
||||||
|
public string Controller { get; set; }
|
||||||
|
|
||||||
|
public string Action { get; set; }
|
||||||
|
|
||||||
|
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
if (Controller != null && Action != null)
|
||||||
|
{
|
||||||
|
var methodParameters = output.Attributes.ToDictionary(attribute => attribute.Key,
|
||||||
|
attribute => (object)attribute.Value);
|
||||||
|
|
||||||
|
// We remove all attributes from the resulting HTML element because they're supposed to
|
||||||
|
// be parameters to our final href value.
|
||||||
|
output.Attributes.Clear();
|
||||||
|
|
||||||
|
output.Attributes["href"] = UrlHelper.Action(Action, Controller, methodParameters);
|
||||||
|
|
||||||
|
output.Content = "My ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
// 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.Text.RegularExpressions;
|
||||||
|
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||||
|
using Microsoft.AspNet.Razor.TagHelpers;
|
||||||
|
|
||||||
|
namespace TagHelpersWebSite.TagHelpers
|
||||||
|
{
|
||||||
|
[TagName("p")]
|
||||||
|
[ContentBehavior(ContentBehavior.Modify)]
|
||||||
|
public class AutoLinkerTagHelper : TagHelper
|
||||||
|
{
|
||||||
|
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
// Find Urls in the content and replace them with their anchor tag equivalent.
|
||||||
|
output.Content = Regex.Replace(
|
||||||
|
output.Content,
|
||||||
|
@"\b(?:https?://|www\.)(\S+)\b",
|
||||||
|
"<strong><a target=\"_blank\" href=\"http://$0\">$0</a></strong>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
// 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 Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||||
|
using Microsoft.AspNet.Razor.TagHelpers;
|
||||||
|
|
||||||
|
namespace TagHelpersWebSite.TagHelpers
|
||||||
|
{
|
||||||
|
[TagName("div", "style", "p")]
|
||||||
|
[ContentBehavior(ContentBehavior.Modify)]
|
||||||
|
public class ConditionTagHelper : TagHelper
|
||||||
|
{
|
||||||
|
public bool? Condition { get; set; }
|
||||||
|
|
||||||
|
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
// If a condition is set and evaluates to false, don't render the tag.
|
||||||
|
if (Condition.HasValue && !Condition.Value)
|
||||||
|
{
|
||||||
|
output.TagName = null;
|
||||||
|
output.Content = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
// 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.Mvc;
|
||||||
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
|
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||||
|
|
||||||
|
namespace TagHelpersWebSite.TagHelpers
|
||||||
|
{
|
||||||
|
[TagName("*")]
|
||||||
|
public class PrettyTagHelper : TagHelper
|
||||||
|
{
|
||||||
|
private static readonly Dictionary<string, string> PrettyTagStyles =
|
||||||
|
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
||||||
|
{
|
||||||
|
{ "a", @"background-color: gray;
|
||||||
|
color: white;
|
||||||
|
border-radius: 3px;
|
||||||
|
border: 1px solid black;
|
||||||
|
padding: 3px;
|
||||||
|
font-family: cursive;" },
|
||||||
|
{ "strong", @"font-size: 1.25em;
|
||||||
|
text-decoration: underline;" },
|
||||||
|
{ "h1", @"font-family: cursive;" },
|
||||||
|
{ "h3", @"font-family: cursive;" }
|
||||||
|
};
|
||||||
|
|
||||||
|
public bool? MakePretty { get; set; }
|
||||||
|
|
||||||
|
[Activate]
|
||||||
|
public ViewContext ViewContext { get; set; }
|
||||||
|
|
||||||
|
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
if (MakePretty.HasValue && !MakePretty.Value)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string prettyStyle;
|
||||||
|
|
||||||
|
if (PrettyTagStyles.TryGetValue(output.TagName, out prettyStyle))
|
||||||
|
{
|
||||||
|
var style = string.Empty;
|
||||||
|
|
||||||
|
if (output.Attributes.TryGetValue("style", out style))
|
||||||
|
{
|
||||||
|
style += ";";
|
||||||
|
}
|
||||||
|
|
||||||
|
output.Attributes["style"] = style + prettyStyle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,64 @@
|
||||||
|
// 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.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
|
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||||
|
using Microsoft.AspNet.Razor.TagHelpers;
|
||||||
|
|
||||||
|
namespace MvcSample.Web.Components
|
||||||
|
{
|
||||||
|
[TagName("tag-cloud")]
|
||||||
|
[ViewComponent(Name = "Tags")]
|
||||||
|
[ContentBehavior(ContentBehavior.Replace)]
|
||||||
|
public class TagCloudViewComponentTagHelper : ITagHelper
|
||||||
|
{
|
||||||
|
private static readonly string[] Tags =
|
||||||
|
("Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua" +
|
||||||
|
"Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat Duis aute irure " +
|
||||||
|
"dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat" +
|
||||||
|
"non proident, sunt in culpa qui officia deserunt mollit anim id est laborum")
|
||||||
|
.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
public int Count { get; set; }
|
||||||
|
|
||||||
|
[Activate]
|
||||||
|
public ViewContext ViewContext { get; set; }
|
||||||
|
|
||||||
|
public async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
var result = await InvokeAsync(Count);
|
||||||
|
var writer = new StringWriter();
|
||||||
|
|
||||||
|
await result.ExecuteAsync(
|
||||||
|
new ViewComponentContext(typeof(TagCloudViewComponentTagHelper).GetTypeInfo(),
|
||||||
|
ViewContext,
|
||||||
|
writer));
|
||||||
|
|
||||||
|
output.TagName = null;
|
||||||
|
output.Content = writer.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IViewComponentResult> InvokeAsync(int count)
|
||||||
|
{
|
||||||
|
var tags = await GetTagsAsync(count);
|
||||||
|
return new JsonViewComponentResult(tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task<string[]> GetTagsAsync(int count)
|
||||||
|
{
|
||||||
|
return Task.FromResult(GetTags(count));
|
||||||
|
}
|
||||||
|
|
||||||
|
private string[] GetTags(int count)
|
||||||
|
{
|
||||||
|
return Tags.Take(count).ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
// 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 Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||||
|
using Microsoft.AspNet.Razor.TagHelpers;
|
||||||
|
using TagHelpersWebSite.Models;
|
||||||
|
|
||||||
|
namespace TagHelpersWebSite.TagHelpers
|
||||||
|
{
|
||||||
|
[ContentBehavior(ContentBehavior.Append)]
|
||||||
|
public class WebsiteInformationTagHelper : TagHelper
|
||||||
|
{
|
||||||
|
public WebsiteContext Info { get; set; }
|
||||||
|
|
||||||
|
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||||
|
{
|
||||||
|
output.TagName = "section";
|
||||||
|
output.Content = string.Format(
|
||||||
|
"<p><strong>Version:</strong> {0}</p>" + Environment.NewLine +
|
||||||
|
"<p><strong>Copyright Year:</strong> {1}</p>" + Environment.NewLine +
|
||||||
|
"<p><strong>Approved:</strong> {2}</p>" + Environment.NewLine +
|
||||||
|
"<p><strong>Number of tags to show:</strong> {3}</p>" + Environment.NewLine,
|
||||||
|
Info.Version.ToString(),
|
||||||
|
Info.CopyrightYear.ToString(),
|
||||||
|
Info.Approved.ToString(),
|
||||||
|
Info.TagsToShow.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="__ToolsVersion__" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup>
|
||||||
|
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||||
|
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||||
|
<PropertyGroup Label="Globals">
|
||||||
|
<ProjectGuid>6db9b8d0-80f7-4e70-bbb0-0b4c04d79a47</ProjectGuid>
|
||||||
|
<OutputType>Web</OutputType>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" Label="Configuration">
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'" Label="Configuration">
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup>
|
||||||
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
|
<DevelopmentServerPort>45493</DevelopmentServerPort>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||||
|
</Project>
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
@using TagHelpersWebSite.Models
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewBag.Title = "About";
|
||||||
|
}
|
||||||
|
|
||||||
|
@addtaghelper "TagHelpersWebSite.TagHelpers.ATagHelper, TagHelpersWebSite"
|
||||||
|
@addtaghelper "TagHelpersWebSite.TagHelpers.WebsiteInformationTagHelper, TagHelpersWebSite"
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<p>Hello, you've reached the about page.</p>
|
||||||
|
|
||||||
|
<h3>Information about our website (outdated):</h3>
|
||||||
|
<websiteinformation info="new WebsiteContext {
|
||||||
|
Version = new Version(1, 1),
|
||||||
|
CopyrightYear = 1990,
|
||||||
|
Approved = true,
|
||||||
|
TagsToShow = 30 }" />
|
||||||
|
</div>
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
@{
|
||||||
|
ViewBag.Title = "Help";
|
||||||
|
}
|
||||||
|
|
||||||
|
@addtaghelper "TagHelpersWebSite.TagHelpers.ATagHelper, TagHelpersWebSite"
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<p>Hello, you've reached the help page. If you're having troubles try visiting our <a controller="home" action="index" approved="true">Approved Home Page</a></p>
|
||||||
|
</div>
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
@using TagHelpersWebSite.Models
|
||||||
|
@model WebsiteContext
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewBag.Title = "Home Page";
|
||||||
|
}
|
||||||
|
|
||||||
|
@addtaghelper "TagHelpersWebSite"
|
||||||
|
|
||||||
|
@section css {
|
||||||
|
<style condition="!Model.Approved">
|
||||||
|
h1 {
|
||||||
|
color:red;
|
||||||
|
font-size:2em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
}
|
||||||
|
|
||||||
|
<div condition="!Model.Approved">
|
||||||
|
<p>This website has <strong>not</strong> been approved yet. Visit www.contoso.com for <strong makePretty="false">more</strong> information.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<h3>Current Tag Cloud from Tag Helper</h3>
|
||||||
|
<section><tag-cloud count="Model.TagsToShow" /></section>
|
||||||
|
<h3>Current Tag Cloud from ViewComponentHelper:</h3>
|
||||||
|
<section>@await Component.InvokeAsync("Tags", 15)</section>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@section footerContent {
|
||||||
|
<p condition="Model.Approved">© @Model.CopyrightYear - My ASP.NET Application</p>
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
@using TagHelpersWebSite.Controllers
|
||||||
|
|
||||||
|
@addtaghelper "TagHelpersWebSite.TagHelpers.ATagHelper, TagHelpersWebSite"
|
||||||
|
@addtaghelper "TagHelpersWebSite.TagHelpers.PrettyTagHelper, TagHelpersWebSite"
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>@ViewBag.Title - My ASP.NET Application</title>
|
||||||
|
|
||||||
|
@RenderSection("css", required: false)
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>ASP.NET vNext - @ViewBag.Title</h1>
|
||||||
|
<p>| <a controller="home" action="index">Home</a>
|
||||||
|
| <a controller="home" action="about">About</a>
|
||||||
|
| <a controller="home" action="help">Help</a> |</p>
|
||||||
|
<div>
|
||||||
|
@RenderBody()
|
||||||
|
<hr />
|
||||||
|
<footer>
|
||||||
|
@RenderSection("footerContent", required: false)
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
@{
|
||||||
|
Layout = "/Views/Shared/_Layout.cshtml";
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"Microsoft.AspNet.Mvc": "",
|
||||||
|
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
|
||||||
|
"Microsoft.AspNet.Mvc.TestConfiguration": ""
|
||||||
|
},
|
||||||
|
"frameworks": {
|
||||||
|
"aspnet50": {},
|
||||||
|
"aspnetcore50": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue