定义图像调整器设置运行时

本文关键字:运行时 设置 调整器 图像 定义 | 更新日期: 2023-09-27 18:34:33

在我的MVC Web应用程序中,我有一个Tech.UI层,所有与用户界面相关的组件都位于其中。

我想使用图像调整器在我的 Web 应用程序中生成缩略图。我看到了应该在web.config文件中设置的配置。

由于如果不构建Tech.UI项目,设置将不会更改,因此是否可以定义web.config文件之外的所有配置?我应该如何定义运行时或静态硬编码的设置?

这是我的示例 web.config 文件:

<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="resizer" type="ImageResizer.ResizerSection,ImageResizer" requirePermission="false" />
      </configSections>
      <appSettings>
        <add key="webpages:Version" value="2.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="PreserveLoginUrl" value="true" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
      </appSettings>
      <system.web>
        <httpRuntime targetFramework="4.5" />
        <compilation debug="true" targetFramework="4.5" />
        <pages>
          <namespaces>
            <add namespace="System.Web.Helpers" />
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
            <add namespace="System.Web.WebPages" />
          </namespaces>
        </pages>
        <httpModules>
          <add name="ImageResizingModule" type="ImageResizer.InterceptModule" />
        </httpModules>
      </system.web>
      <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <modules>
          <add name="ImageResizingModule" type="ImageResizer.InterceptModule" />
        </modules>
        <handlers>
          <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
          <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
          <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
          <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%'Microsoft.NET'Framework'v4.0.30319'aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
          <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%'Microsoft.NET'Framework64'v4.0.30319'aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
          <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
        <security>
          <requestFiltering allowDoubleEscaping="true" />
        </security>
      </system.webServer>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <resizer>
        <remotereader signingKey="blahblahblah" allowAllSignedRequests="false" allowRedirects="5">
          <allow domain="*" />
        </remotereader>
        <diskCache dir="~/img/t" autoClean="false" hashModifiedDate="true" enabled="true" subfolders="32" cacheAccessTimeout="15000" asyncWrites="false" asyncBufferSize="10485760" />
        <pipeline fakeExtensions=".ashx" />
        <plugins>
          <add name="MvcRoutingShim" />
          <add name="DiskCache" />
          <add name="RemoteReader" />
          <add name="SeamCarving" />
        </plugins>
      </resizer>
    </configuration>

定义图像调整器设置运行时

有几种不同的方法可以做到这一点:

  1. 使用 configSource 将 Web.Config 的该部分外部化。

  2. 使用 Config.Current.setConfigXmlText(xml) 替换 XML 配置。

  3. 通过代码new Plugin(settings).Install(Config.Current)安装所有插件,直接配置实例。确保没有冲突的 XML 设置,因为这些设置可能会被加载。

  4. ICurrentConfigProvider实现为插件,以根据当前 HTTP 请求(或缺少请求(控制使用哪个Config实例。如果要完全替换主实例,方法就是这样做的。所有对Config.Current的调用都将委托给第一个响应的ICurrentConfigProvider插件。

  5. 不要使用 Config.Current .创建和管理您自己的ImageResizer.Configuration.Config实例。请注意,某些插件不能为每个应用程序(DiskCache(或进程(Faces,RedEye,PdfRenderer(有多个实例。