我正在将一些代码从 ASPX View 引擎迁移到 Razor,但遇到了障碍。
我有这个代码:
<link rel="Stylesheet" type="text/css" href="
<%=Page.ClientScript.GetWebResourceUrl
(typeof(DotNetOpenAuth.OpenId.RelyingParty.OpenIdSelector),
"DotNetOpenAuth.OpenId.RelyingParty.OpenIdSelector.css")%>" />
这里的问题是,使用 Razor,我没有 Page 属性。
所以我退后了一步,我正在思考这个问题:在 Razor 中获取嵌入式资源的正确方法是什么?
我花了很多时间试图找到关于这个主题的解决方案,但除了“在助手中包装一个新页面”之外,我还没有真正找到任何东西
这是唯一的方法吗?或者有什么更正确的吗?
最佳答案
不幸的是,Web 资源与 Webforms 基础架构紧密相关,没有它就很难重用它们。所以有点hacky,但你可以写一个助手:
public static class UrlExtensions
{
public static string WebResource(this UrlHelper urlHelper, Type type, string resourcePath)
{
var page = new Page();
return page.ClientScript.GetWebResourceUrl(type, resourcePath);
}
}
在您的 Razor View 中:
<link rel="stylesheet" type="text/css" href="@Url.WebResource(typeof(DotNetOpenAuth.OpenId.RelyingParty.OpenIdSelector), "DotNetOpenAuth.OpenId.RelyingParty.OpenIdSelector.css")" />
另一种可能性是编写一个自定义 HTTP 处理程序/ Controller ,它将从程序集中读取嵌入的资源,并通过设置正确的内容类型将其流式传输到响应。
关于asp.net-mvc-3 - 在 Razor View 上处理嵌入式资源的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4996522/