玩一玩https://github.com/ServiceStack/RazorRockstars , 很喜欢它。只是想知道是否有人可以帮助我找到一种在使用自托管应用程序时从 View 中输出绝对 URL 的方法?
这是我所能想到的:
public static class UrlHelperExtensions
{
public static string AbsoluteContent(this UrlHelper urlHelper, string path)
{
var baseUri = new Uri(EndpointHost.AppHost.Config.WebHostUrl);
return new Uri(baseUri, path).ToString();
}
}
其中AppHost.Config.WebHostUrl是我在配置host的时候设置的
更新
抱歉,我应该说得更清楚,我实际上是在以一种稍微不标准的方式使用 ServiceStack——作为呈现电子邮件模板的一种方式。所以我有这样的服务:
public class EmailService : Service
{
public void Post(EmailTask request)
{
var format = GetAppHost().Plugins.Find(x => x is RazorFormat) as RazorFormat;
var template = format.ExecuteTemplate(request.Model, request.Template.ToString(), string.Empty);
var html = template.Result;
// email the rendered template etc
}
}
渲染模板时,Request对象为null
最佳答案
在 Razor 中 ViewPageBase您可以访问 HTTP base.Request
和 base.Response
,您可以在其中访问有关请求的元数据,例如返回的 base.Request.AbsoluteUri
当前请求 Url,其中应包含您想要的信息。
关于c# - ServiceStack.Razor 从 razor View 输出绝对 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13976434/