简体   繁体   English

asp.net MVC Url.Content()CDN重定向

[英]asp.net MVC Url.Content() CDN redirection

I'm wondering if there's an easy way to specify a CDN for all content that I reference through Url.Content in my views. 我想知道是否有一种简单的方法可以为我在视图中通过Url.Content引用的所有内容指定CDN。

Something that I could configure in my Web.config file in a way similar to the following. 我可以在Web.config文件中以类似于以下的方式配置的东西。


    <cdn>
        <add filetype="css" root="http://mycdn.mydomain.com/stylesheets/" />
        <add filetype="js" root="http://myjscdn.mydomain.com/js/ />
    </cdn>

Then, I could just have <%= Url.Content("~/Content/StyleSheets/What.css") %> and it would output http://mycdn.mydomain.com/stylesheets/Content/StyleSheets/What.css . 然后,我可以只有<%= Url.Content(“〜/ Content / StyleSheets / What.css”)%>并输出http://mycdn.mydomain.com/stylesheets/Content/StyleSheets/What.css

If nothing is available, I'll just do it myself via extension methods, but I was wondering if it was possible out of the box. 如果没有可用的东西,我会通过扩展方法自己做,但我想知道它是否可以开箱即用。

The answer above is probably correct, here is how it might look in practice: 上面的答案可能是正确的,这是它在实践中的样子:

// @ 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" />

I think that works.. You can also use multiple extension methods to segregate sorts of content to serve locally and those on the CDN.. There is no requirement that the configuration is in web.config though, just useful for development and management. 我认为这样做..你也可以使用多种扩展方法来分离各种内容以便在本地和CDN上进行服务。虽然配置在web.config中没有要求,但这对开发和管理非常有用。

I don't believe there is anything built in to facilitate this for you. 我不相信内置任何东西可以为您提供便利。 That said, is adding this to Web.config really necessary? 那就是说,将这个添加到Web.config真的有必要吗?

I guess if you don't want to include your CDN-distributed items on every page in your application, I could understand extrapolating the URLs to a central location somewhere, but even then it seems like you could just write some extension methods that return a constant string instead of messing with the Web.config. 我想如果你不想在你的应用程序的每个页面上包含你的CDN分布式项目,我可以理解将URL外推到某个地方的中心位置,但即便如此,你似乎只能写一些返回方法的扩展方法常量字符串而不是搞乱Web.config。 I know some people cringe at this because you have to recompile to change the URL's, but how often are they going to change? 我知道有些人对此感到畏缩,因为你必须重新编译以更改URL,但是他们多久会改变一次?

In the event, however, that you do want to include these CDN-distributed items on every page, what's wrong with throwing the URLs in your master page? 但是,如果您确实希望在每个页面上包含这些CDN分发的项目,那么在主页面中丢弃URL有什么问题? In most cases, changing your master page is just as simple as changing a web.config value. 在大多数情况下,更改母版页就像更改web.config值一样简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM