C# 在运行时定义ImageResizer设置

C# 在运行时定义ImageResizer设置,c#,.net,asp.net-mvc-4,imageresizer,C#,.net,Asp.net Mvc 4,Imageresizer,在我的MVC web应用程序中,我有一个Tech.UI层,所有与用户界面相关的组件都位于该层 我想用它在我的web应用程序中生成缩略图。我看到了应该在web.config文件中设置的配置 由于在未构建Tech.UI项目的情况下不会更改设置,因此是否需要定义web.config文件之外的所有配置?我应该如何定义运行时或静态硬编码的设置 下面是我的示例web.config文件: <?xml version="1.0" encoding="utf-8"?> <configur

在我的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>

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

  • 使用将Web.Config的该部分外部化

  • 使用
    Config.Current.setConfigXmlText(xml)
    替换xml配置

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

  • ICurrentConfigProvider
    实现为一个插件,以控制基于当前HTTP请求(或缺少)使用哪个
    Config
    实例。如果您想完全替换主实例,这就是方法。对
    Config.Current
    的所有调用都被委托给第一个响应的
    ICurrentConfigProvider
    插件

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