Domanda

In my MVC web application, I have a Tech.UI layer which all the user interface related components are located in there.

I want to use ImageResizer to generate thumbnail in my web application. I saw the configuration which should be set in web.config file.

As the settings will not be changed without building the Tech.UI project, is there anyway to define all the configuration outside of web.config file? How should I define the settings on run-time or statically hard-coded?

Here is my sample web.config file:

<?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>
È stato utile?

Soluzione

There are several different ways to do this:

  1. Externalize that part of Web.Config using configSource.

  2. Use Config.Current.setConfigXmlText(xml) to replace the XML configuration.

  3. Install all plugins via code new Plugin(settings).Install(Config.Current), configuring the instances directly. Make sure there are no conflicting XML settings, as those may get loaded instead.

  4. Implement ICurrentConfigProvider as a plugin to control which Config instance is used based on the current HTTP request (or lack of one). If you want to completely replace the primary instance, this is how. All calls to Config.Current get delegated to the first responding ICurrentConfigProvider plugin.

  5. Don't use Config.Current. Create and manage your own instance(s) of ImageResizer.Configuration.Config. Note that some plugins can't have multiple instances per application (DiskCache) or process (Faces, RedEye, PdfRenderer).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top