如何注入后裔UrlHelper班级分成WebViewPage使缓存无效? [英] How to inject descendant UrlHelper class into WebViewPage to enable cache busting?

查看:131
本文介绍了如何注入后裔UrlHelper班级分成WebViewPage使缓存无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重写 UrlHelper.Content()方法。现在我想用来取代默认的我的执行 UrlHelper 类。

我如何配置MVC告诉它注入到 WebViewPage.Url 属性,它的类?

更新1:

这个想法很简单。套件支持缓存通过添加时间戳查询参数的URL破坏。

我想为单一的资源相同的功能。

UrlHelper 类,可以覆盖它的内容(字符串)方法。
因此有可能生成最终的字符串时采取资源的时间戳考虑。

更新2:

这似乎是我的premise被wrang。我认为thout SRC =?...相当于SRC =@ Url.Content(〜...)。事实并非如此。


解决方案

你会需要推出自己的 WebViewPage 正在提供其自身的实施<$ C的$ C> UrlHelper 这将覆盖内容()方法。

首先,创建类型:

 公共类MyUrlHelper:UrlHelper
{
    公共MyUrlHelper(){}
    公共MyUrlHelper(RequestContext的RequestContext的):基地(的RequestContext){}
    公共MyUrlHelper(RequestContext的RequestContext的,RouteCollection routeCollection):基地(RequestContext的,routeCollection){}    公众覆盖字符串内容(字符串的contentPath)
    {
        //这里做自己的自定义实现,
        //访问使用base.Content原创内容()方法()
    }
}公共抽象类MyWebPage:WebViewPage
{
    保护覆盖无效InitializePage()
    {
        this._urlHelper =新MyUrlHelper(this.Request.RequestContext,RouteTable.Routes);
    }    私人MyUrlHelper _urlHelper;
    公开新MyUrlHelper网址{{返回_urlHelper; }}
}//为强类型的视图提供通用版
公共抽象类MyWebPage&LT; T&GT; :WebViewPage&LT; T&GT;
{
    保护覆盖无效InitializePage()
    {
        this._urlHelper =新MyUrlHelper(this.Request.RequestContext,RouteTable.Routes);
    }    私人MyUrlHelper _urlHelper;
    公开新MyUrlHelper网址{{返回_urlHelper; }}
}

然后在〜/查看/ Web.Config中注册您的自定义 MyWebPage

 &LT; system.web.webPages.razor&GT;
    ....
    &LT;页面pageBaseType =Your.NameSpace.MyWebPage&GT;
         ....
    &LT; /页&GT;
  &LT; /system.web.webPages.razor>

I've overridden UrlHelper.Content() method. Now I want my implementation to be used instead of default UrlHelper class.

How can I configure MVC to tell it which class to inject into WebViewPage.Url property?

Update 1:
The idea is simple. Bundles support cache busting by adding timestamp query parameter to the url.
I want the same functionality for single resource.
UrlHelper class allows to override its Content(string) method. Consequently it is possible to take resource's timestamp into account when generating final string.

Update 2:
It seems like my premise was wrang. I thout that src="~..." is equivalent to src="@Url.Content("~...")". That is not the case.

解决方案

You're gonna need to introduce your own WebViewPage that is providing its own implementation of UrlHelper which will override the Content() method.

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:

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

这篇关于如何注入后裔UrlHelper班级分成WebViewPage使缓存无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆