简体   繁体   English

如何将后代UrlHelper类注入WebViewPage以启用缓存清除?

[英]How to inject descendant UrlHelper class into WebViewPage to enable cache busting?

I've overridden UrlHelper.Content() method. 我已经重写了UrlHelper.Content()方法。 Now I want my implementation to be used instead of default UrlHelper class. 现在,我希望使用实现代替默认的UrlHelper类。

How can I configure MVC to tell it which class to inject into WebViewPage.Url property? 我如何配置MVC来告诉它要注入WebViewPage.Url属性的类?

Update 1: 更新1:
The idea is simple. 这个想法很简单。 Bundles support cache busting by adding timestamp query parameter to the url. 捆绑包通过向URL添加时间戳查询参数来支持缓存清除。
I want the same functionality for single resource. 我希望单个资源具有相同的功能。
UrlHelper class allows to override its Content(string) method. UrlHelper类允许重写其Content(string)方法。 Consequently it is possible to take resource's timestamp into account when generating final string. 因此,在生成最终字符串时可以考虑资源的时间戳。

Update 2: 更新2:
It seems like my premise was wrang. 看来我的前提发生了变化。 I thout that src="~..." is equivalent to src="@Url.Content("~...")". 我认为src =“〜...”等同于src =“ @ Url.Content(”〜...“)”。 That is not the case. 事实并非如此。

I don't have a direct answer to your question but you might just create an extension of the URLHelper class like this: 我没有直接回答您的问题,但是您可以像这样创建URLHelper类的扩展:

public static class CustomUrlHelper
{
    public static string CustomContent(this UrlHelper helper, string contentPath)
    {
        // your Content method 
    }
}

and then just call this method on the Url object like this: 然后像这样在Url对象上调用此方法:

@Url.CustomContent("~/Content/Site.css")

You're gonna need to introduce your own WebViewPage that is providing its own implementation of UrlHelper which will override the Content() method. 您将需要引入自己的WebViewPage ,该WebViewPage提供自己的UrlHelper实现,该实现将覆盖Content()方法。

First, create the types: 首先,创建类型:

public class MyUrlHelper : UrlHelper
{
    public MyUrlHelper() {}
    public MyUrlHelper(RequestContext requestContext) : base(requestContext) {}
    public MyUrlHelper(RequestContext requestContext, RouteCollection routeCollection) : base(requestContext, routeCollection) { }

    public override string Content(string contentPath)
    {
        // do your own custom implemetation here,
        // you access original Content() method using base.Content()
    }
}

public abstract class MyWebPage : WebViewPage
{
    protected override void InitializePage()
    {
        this._urlHelper = new MyUrlHelper(this.Request.RequestContext, RouteTable.Routes);
    }

    private MyUrlHelper _urlHelper;
    public new MyUrlHelper Url { get { return _urlHelper; } }
}

// provide generic version for strongly typed views
public abstract class MyWebPage<T> : WebViewPage<T>
{
    protected override void InitializePage()
    {
        this._urlHelper = new MyUrlHelper(this.Request.RequestContext, RouteTable.Routes);
    }

    private MyUrlHelper _urlHelper;
    public new MyUrlHelper Url { get { return _urlHelper; } }
}

Then register your custom MyWebPage in ~/Views/Web.Config : 然后在~/Views/Web.Config注册您的自定义MyWebPage

  <system.web.webPages.razor>
    ....
    <pages pageBaseType="Your.NameSpace.MyWebPage">
         ....
    </pages>
  </system.web.webPages.razor>

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

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