locked
Refactoring Web.config RRS feed

  • Question

  • User1055826223 posted

    Hello,

    I've been working on an ASP.NET MVC 4 project for some months now, and it's been growing in size. Everything I know today about ASP.NET MVC comes from developing this project and because of that the project really needs a big refactoring.

    To start, I've been cleaning my web.config file, that was clustered with stuff I didn't know what was the purpose. But, I still have some doubts about some of the things that are there:

    1. What's the purpose of this? I know I use Telerik, but I don't know why may I need that reference in the Web.Config. I remove it and everythins seems to work just fine.

        <httpHandlers>
          <add verb="GET,HEAD" path="asset.axd" validate="false" type="Telerik.Web.Mvc.WebAssetHttpHandler, Telerik.Web.Mvc" />
        </httpHandlers>

    2. On my <system.webServer> I have this, and I also don't know what's the purpose if whether I can just remove it or not.

        <urlCompression doStaticCompression="true" doDynamicCompression="true" />
        <validation validateIntegratedModeConfiguration="false" />
        <handlers>
          <remove name="asset" />
          <add name="asset" preCondition="integratedMode" verb="GET,HEAD" path="asset.axd" type="Telerik.Web.Mvc.WebAssetHttpHandler, Telerik.Web.Mvc" />
          <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
          <remove name="OPTIONSVerbHandler" />
          <remove name="TRACEVerbHandler" />
          <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>

    3. Is there a way to put all runtime assembly bindings in an external config file? When I do it like:

    <runtime configSource="Config\runtime.config" />

    I get the following error:

    Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxx' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    I understand that some things may seem basic but I want to understand everything I have on my project and do things right this time. Thanks!

    Sunday, December 20, 2015 12:15 AM

Answers

  • User-369506445 posted
    You can't use configSource to externalize the <runtime> configuration.

    <runtime> is a special case that doesn't read the values in any external file specified in configSource.

    Specifying the "configSource" attribute won't cause an error, the source file simply won't be read
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 20, 2015 5:39 PM

All replies

  • User-369506445 posted

    hi

    here are 2 issue

    about your error , your project can't find the assembly , for solve it you can go to Tools->Nuget Package Manager->Package Manager Console and write below syntax :

    Install-Package Microsoft.AspNet.WebPages

    above code add the assembly to your project

    another issue is , if you'll load dynamic dll at run-time , you can use below code :

    Assembly assembly = Assembly.LoadFrom("yourAssembly.dll");
    

    and also you can following below link:

    https://msdn.microsoft.com/en-us/library/25y1ya39(v=vs.110).aspx

    Sunday, December 20, 2015 6:20 AM
  • User1055826223 posted

    Thanks for the answer.

    But if I load the assembly files directly in the Web.config file, not using an external file, there are no errors, so the Microsoft.AspNet.WebPages is not missing. The issue comes from using the external file.

    I know that if I do something like this in the Web.config:

    <runtime>
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Web.WebPages" publicKeyToken="xxxxxxxxxxx" />
            <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
     </assemblyBinding>
    </runtime>

    It works! But, if put that same thing in an external file and then try to load like:

    <runtime configSource="Config\runtime.config" />

    It gives the error I previously said.

    Sunday, December 20, 2015 2:44 PM
  • User-369506445 posted
    Please following below linkhttp://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx
    Sunday, December 20, 2015 4:35 PM
  • User-369506445 posted
    Please following below

    http://stackoverflow.com/questions/1731513/best-practice-to-include-log4net-external-config-file-in-asp-net

    linkhttp://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx
    Sunday, December 20, 2015 4:40 PM
  • User1055826223 posted

    Thanks for the links. I already had followed those for connectionString and appSettings and it worked like a charm, the problem still with runtime section. But, after searching more I found that runtime section in Web.config simply doesn't read external files, so no solution for that one, yet.

    Link: http://world.episerver.com/blogs/Magnus-Rahl/Dates/2011/6/Todays-gotcha-configSource-on-the-runtime-section-element/

    Have you any idea about my other two questions?

    Sunday, December 20, 2015 5:20 PM
  • User-369506445 posted
    You can't use configSource to externalize the <runtime> configuration.

    <runtime> is a special case that doesn't read the values in any external file specified in configSource.

    Specifying the "configSource" attribute won't cause an error, the source file simply won't be read
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 20, 2015 5:39 PM