How to inject descendant UrlHelper class into WebV

2019-08-05 06:22发布

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.

2条回答
Melony?
2楼-- · 2019-08-05 07:06

I don't have a direct answer to your question but you might just create an extension of the URLHelper class like this:

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.CustomContent("~/Content/Site.css")
查看更多
迷人小祖宗
3楼-- · 2019-08-05 07:18

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>
查看更多
登录 后发表回答