简体   繁体   中英

Using web.config from base folder and not bin folder

I have an asp.net mvc web page. I'm using config transforms with it. The config transform in the bin directory is correct. However, when I debug with IIS express, all the app settings being used are actually from the original web.config, not the transformed web.config in the bin folder. If I rename the web.config to something else in the root folder, debugging will halt as soon as it tries to read a connection string because it says it's not initialized. What is the problem?

Transformations only apply when you publish the project. However their is a hack to it

You can try using the below code. Your original configuration should be in Web.Base.Config

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild">
    <TransformXml 
        Source="Web.Base.config" 
        Transform="Web.$(Configuration).config" 
        Destination="Web.config" />
</Target>

You can also refer to below URL as sometime it makes transformation twice and you need to add steps for beforebuild

https://sebnilsson.com/blog/asp-net-transform-web-config-with-debug-release-on-build/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM