Adding support for Url.Content, Href and ~/ in Razor views
This commit is contained in:
parent
49de9d2828
commit
2e0bed750e
File diff suppressed because one or more lines are too long
|
|
@ -4,7 +4,7 @@
|
|||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>@ViewBag.Title - My ASP.NET Application</title>
|
||||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
|
||||
<link rel="stylesheet" href="~/content/bootstrap.min.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="navbar navbar-inverse navbar-fixed-top">
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Abstractions;
|
||||
using Microsoft.AspNet.DependencyInjection;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
|
|
@ -50,5 +51,21 @@ namespace Microsoft.AspNet.Mvc
|
|||
// The intent is to use full URLs by default.
|
||||
return _httpContext.Request.PathBase + path;
|
||||
}
|
||||
|
||||
public string Content([NotNull] string contentPath)
|
||||
{
|
||||
return GenerateClientUrl(_httpContext.Request.PathBase, contentPath);
|
||||
}
|
||||
|
||||
private static string GenerateClientUrl([NotNull] PathString applicationPath,
|
||||
[NotNull] string path)
|
||||
{
|
||||
if (path.StartsWith("~/", StringComparison.Ordinal))
|
||||
{
|
||||
var segment = new PathString(path.Substring(1));
|
||||
return applicationPath.Add(segment).Value;
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -219,6 +219,11 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
}
|
||||
}
|
||||
|
||||
public virtual string Href([NotNull] string contentPath)
|
||||
{
|
||||
return Url.Content(contentPath);
|
||||
}
|
||||
|
||||
private void WritePositionTaggedLiteral(TextWriter writer, string value, int position)
|
||||
{
|
||||
WriteLiteralTo(writer, value);
|
||||
|
|
|
|||
|
|
@ -5,5 +5,7 @@
|
|||
string Action(string action, string controller, object values);
|
||||
|
||||
string Route(object values);
|
||||
|
||||
string Content(string contentPath);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,79 @@
|
|||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Abstractions;
|
||||
using Microsoft.AspNet.DependencyInjection;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Core.Test
|
||||
{
|
||||
public class UrlHelperTest
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("", "/Home/About", "/Home/About")]
|
||||
[InlineData("/myapproot", "/test", "/test")]
|
||||
public void Content_ReturnsContentPath_WhenItDoesNotStartWithToken(string appRoot,
|
||||
string contentPath,
|
||||
string expectedPath)
|
||||
{
|
||||
// Arrange
|
||||
var context = CreateHttpContext(appRoot);
|
||||
var contextAccessor = CreateActionContext(context);
|
||||
var urlHelper = new UrlHelper(contextAccessor);
|
||||
|
||||
// Act
|
||||
var path = urlHelper.Content(contentPath);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedPath, path);
|
||||
}
|
||||
|
||||
[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 Content_ReturnsAppRelativePath_WhenItStartsWithToken(string appRoot,
|
||||
string contentPath,
|
||||
string expectedPath)
|
||||
{
|
||||
// Arrange
|
||||
var context = CreateHttpContext(appRoot);
|
||||
var contextAccessor = CreateActionContext(context);
|
||||
var urlHelper = new UrlHelper(contextAccessor);
|
||||
|
||||
// Act
|
||||
var path = urlHelper.Content(contentPath);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedPath, path);
|
||||
}
|
||||
|
||||
private static HttpContext CreateHttpContext(string appRoot)
|
||||
{
|
||||
var appRootPath = new PathString(appRoot);
|
||||
var request = new Mock<HttpRequest>();
|
||||
request.SetupGet(r => r.PathBase)
|
||||
.Returns(appRootPath);
|
||||
var context = new Mock<HttpContext>();
|
||||
context.SetupGet(c => c.Request)
|
||||
.Returns(request.Object);
|
||||
return context.Object;
|
||||
}
|
||||
|
||||
private static IContextAccessor<ActionContext> CreateActionContext(HttpContext context)
|
||||
{
|
||||
var actionContext = new ActionContext(context,
|
||||
Mock.Of<IRouter>(),
|
||||
new Dictionary<string, object>(),
|
||||
new ActionDescriptor());
|
||||
var contextAccessor = new Mock<IContextAccessor<ActionContext>>();
|
||||
contextAccessor.SetupGet(c => c.Value)
|
||||
.Returns(actionContext);
|
||||
return contextAccessor.Object;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,14 @@
|
|||
{
|
||||
"version" : "0.1-alpha-*",
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.DependencyInjection": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Mvc.Core" : "",
|
||||
"Microsoft.AspNet.Mvc" : "",
|
||||
"Microsoft.AspNet.Testing": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Mvc.ModelBinding": "",
|
||||
"Microsoft.AspNet.Mvc.Rendering": "",
|
||||
"Microsoft.AspNet.Routing": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Abstractions" : "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Mvc.ModelBinding" : "",
|
||||
|
|
|
|||
Loading…
Reference in New Issue