Skip to content

Commit

Permalink
Fix Request in Partial Views. Don't escape Raw strings
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 19, 2013
1 parent 60c89a9 commit ac8b029
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/ServiceStack.Razor/ViewPage.cs
Expand Up @@ -34,7 +34,7 @@ public override void Init(IRazorViewEngine viewEngine, ViewDataDictionary viewDa
{
this.Request = httpReq;
this.Response = httpRes;
Html.Init(httpReq, viewEngine, viewData, null);
Html.Init(httpReq, httpRes, viewEngine, viewData, null);
this.Model = new DynamicRequestObject(httpReq);
}

Expand Down
4 changes: 3 additions & 1 deletion src/ServiceStack.Razor/ViewPage`1.cs
Expand Up @@ -19,6 +19,8 @@ public override void SetState(HtmlHelper htmlHelper)
{
if (htmlHelper == null) return;

this.Request = htmlHelper.HttpRequest;
this.Response = htmlHelper.HttpResponse;
Html.SetState(htmlHelper);
}

Expand Down Expand Up @@ -47,7 +49,7 @@ public override void Init(IRazorViewEngine viewEngine, ViewDataDictionary viewDa
this.Request = httpReq;
this.Response = httpRes;
Html = new HtmlHelper<TModel>();
Html.Init(httpReq, viewEngine, viewData, null);
Html.Init(httpReq, httpRes, viewEngine, viewData, null);
if (viewData.Model is TModel)
this.Model = (TModel)viewData.Model;
else
Expand Down
11 changes: 7 additions & 4 deletions src/ServiceStack/Html/HtmlHelper.cs
Expand Up @@ -85,6 +85,7 @@ public static MethodInfo GetMethod(string methodName)
public bool RenderHtml { get; protected set; }

public IHttpRequest HttpRequest { get; set; }
public IHttpResponse HttpResponse { get; set; }
public IViewEngine ViewEngine { get; set; }

public MarkdownPage MarkdownPage { get; protected set; }
Expand All @@ -106,6 +107,7 @@ public void SetState(HtmlHelper htmlHelper)
if (htmlHelper == null) return;

HttpRequest = htmlHelper.HttpRequest;
HttpResponse = htmlHelper.HttpResponse;
ScopeArgs = htmlHelper.ScopeArgs;
viewData = htmlHelper.ViewData;
}
Expand All @@ -122,17 +124,18 @@ public HtmlHelper()
public void Init(MarkdownPage markdownPage, Dictionary<string, object> scopeArgs,
bool renderHtml, ViewDataDictionary viewData, HtmlHelper htmlHelper)
{
Init(null, markdownPage.Markdown, viewData, htmlHelper);
Init(null, null, markdownPage.Markdown, viewData, htmlHelper);

this.RenderHtml = renderHtml;
this.MarkdownPage = markdownPage;
this.ScopeArgs = scopeArgs;
}

public void Init(IHttpRequest httpReq, IViewEngine viewEngine, ViewDataDictionary viewData, HtmlHelper htmlHelper)
public void Init(IHttpRequest httpReq, IHttpResponse httpRes, IViewEngine viewEngine, ViewDataDictionary viewData, HtmlHelper htmlHelper)
{
this.RenderHtml = true;
this.HttpRequest = httpReq ?? (htmlHelper != null ? htmlHelper.HttpRequest : null);
this.HttpResponse = httpRes ?? (htmlHelper != null ? htmlHelper.HttpResponse : null);
this.ViewEngine = viewEngine;
this.ViewData = viewData;
this.ViewData.PopulateModelState();
Expand Down Expand Up @@ -168,11 +171,11 @@ public string Debug(object model)
return null;
}

public string Raw(object content)
public MvcHtmlString Raw(object content)
{
if (content == null) return null;
var strContent = content as string;
return strContent ?? content.ToString(); //MvcHtmlString
return MvcHtmlString.Create(strContent ?? content.ToString()); //MvcHtmlString
}

public static RouteValueDictionary AnonymousObjectToHtmlAttributes(object htmlAttributes)
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceStack/Markdown/MarkdownViewBase.cs
Expand Up @@ -114,7 +114,7 @@ public virtual void Init(IAppHost appHost, MarkdownPage markdownPage, Dictionary
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public string Raw(string content)
public MvcHtmlString Raw(string content)
{
return Html.Raw(content);
}
Expand Down
Expand Up @@ -287,6 +287,12 @@ public void Can_get_PartialModel()
Assert200(Host + "/partialmodel", containsItems.ToArray());
}

[Test]
public void Can_get_RequestPathInfo_in_PartialChildModel()
{
Assert200(Host + "/partialmodel", Template_PartialModel, ViewPartialChildModel, "PathInfo: <b>/partialmodel</b>");
}

}
}

Expand Up @@ -2,4 +2,6 @@

@Html.TextBoxFor(x => x.SomeProperty)

PathInfo: <b>@base.Request.PathInfo</b>

<!--view:PartialChildModel.cshtml-->
2 changes: 2 additions & 0 deletions tests/RazorRockstars.Console.Files/Views/PartialModel.cshtml
Expand Up @@ -10,6 +10,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>@Html.Raw("b&#225;i s&#232;")</h1>

@foreach (var item in Model.Items)
{
<div>@Html.Partial("PartialChildModel", item)</div>
Expand Down
@@ -1,4 +1,4 @@
@inherits ViewPage<List<Rockstar>>
@model List<Rockstar>

<div id="sidebar">
<h4>Razor Partials with Model</h4>
Expand All @@ -7,6 +7,4 @@
}
</div>

PathInfo: <b>@base.Request.PathInfo</b>

<!--view:RazorPartialModel.cshtml-->

0 comments on commit ac8b029

Please sign in to comment.