How to locate resources in Orchard

后端 未结 5 1571
梦毁少年i
梦毁少年i 2020-12-19 07:52

I am writing an Orchard theme, and I\'d like to be able to locate some of the resources packaged with the theme (images/swfs etc).

What is the best way of doing this

5条回答
  •  没有蜡笔的小新
    2020-12-19 08:21

    I slapped together an extension method for a similar problem:

    public static string ResourceUrl(this HtmlHelper helper, string resourceType, string resourceName)
    {
        var manager = helper.Resolve();
        var settings = new RequireSettings { Type = resourceType, Name = resourceName, BasePath = resourceType };
        var resource = manager.FindResource(settings);
        var context = new ResourceRequiredContext { Resource = resource, Settings = settings };
    
        var url = context.GetResourceUrl(settings, "/");
    
        return url;
    }
    

    Resource Manifest Definition:

    manifest.DefineResource("Content", "MyImage").SetUrl("Content/myimage.png");
    

    View Usage:

    @Html.ResourceUrl("Content", "MyImage")
    

    It's not well tested, and could probably use some error handling, but its working for me.

提交回复
热议问题