asp.net-mvc – asp.net MVC Url.Content()CDN重定向
发布时间:2020-12-16 03:32:33 所属栏目:asp.Net 来源:网络整理
导读:我想知道是否有一种简单的方法可以为我在视图中通过Url.Content引用的所有内容指定CDN. 我可以在Web.config文件中以类似于以下的方式配置的东西. cdn add filetype="css" root="http://mycdn.mydomain.com/stylesheets/" / add filetype="js" root="http://m
|
我想知道是否有一种简单的方法可以为我在视图中通过Url.Content引用的所有内容指定CDN.
我可以在Web.config文件中以类似于以下的方式配置的东西.
<cdn>
<add filetype="css" root="http://mycdn.mydomain.com/stylesheets/" />
<add filetype="js" root="http://myjscdn.mydomain.com/js/ />
</cdn>
然后,我可以只有<%= Url.Content(“?/ Content / StyleSheets / What.css”)%>它会输出http://mycdn.mydomain.com/stylesheets/Content/StyleSheets/What.css. 如果没有可用的东西,我会通过扩展方法自己做,但我想知道它是否可以开箱即用. 解决方法
上面的答案可能是正确的,这是它在实践中的样子:
// @ UrlHelperExtensions.cs
public static class UrlHelperExtensions
{
public static string CdnContent(this UrlHelper helper,string contentPath)
{
return (ConfigurationManager.AppSettings["CDN_Available"] == "True")
? ConfigurationManager.AppSettings["CDN_ContentRootUrl"]
+ contentPath.TrimStart('~')
: helper.Content(contentPath);
}
// @ Web.config
<appSettings>
// ...
<add key="CDN_Available" value="True" />
<add key="CDN_SiteImageRoot" value="http://cdn.gbase.com/images/" />
// ...
</appSettings>
// @ MyPage.cs
<link href="@Url.CdnContent("~/client/css/mymin.css")" rel="stylesheet" type="text/css" media="all" />
我认为这样做..你也可以使用多种扩展方法来分离各种内容以便在本地和CDN上进行服务.虽然配置在web.config中没有要求,但这对开发和管理非常有用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
