Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common binding redirects mechanism for Razor Views and both SDK types - Closes #58 #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/Binding_Redirects/Autogenerating-Binding-Redirects.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ If you want this to happen automatically, you can add the following to your proj
<PropertyGroup>
<GeneratedBindingRedirectsAction>Overwrite</GeneratedBindingRedirectsAction>
</PropertyGroup>
```
```

# Razor Views
MVC Views folders often contain a customized version of `web.config`, including a `system.web.webPages.razor` section and may include other settings to prevent serving files in the folder using the `BlockViewHandler`.

Since views may reference models or components from assemblies which have/require binding redirects, the redirects from the main `web.config` should normally be copied to these files too.

To facilitate this scenario, a new item group is added, and any `web.config` files in Views or Areas\*\Views folders are marked as `RazorAppConfigFiles`.

The same rules and property (`GeneratedBindingRedirectsAction`) that affects the main `web.config` will also update these files.

This applies to both the main SDK and the RazorLibrary SDK.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ Add this into your csproj
</PropertyGroup>
```

And then look for a `Web.BindingRedirects.config` file in your project's Solution Explorer.
And then look for a `Web.BindingRedirects.config` file in your project's Solution Explorer.

# Razor Views
If you set `GeneratedBindingRedirectsAction` to `Preview` then any MVC Views related `web.config` files, such as `View\web.config` or `Areas\*\Views\web.config`, or any file marked as `RazorAppConfigFiles` will also create a preview file next to it named `web.BindingRedirects.config`.

8 changes: 6 additions & 2 deletions docs/RazorLibrary.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ For more information see
| -------- | ------------- | ----------- |
| `ExcludeSDKDefaultPackages` | false | Do not include the default packages `Microsoft.Net.Compilers.Toolset` and `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` |
| `ApplySDKDefaultPackageVersions` | true* | Apply default version numbers to packages unless using a Central Package Management system |
| `GeneratedBindingRedirectsAction` | None | Set the desired default behavior of what to do with SuggestedBindingRedirects if not yet set.<br>See [automatically generated binding redirects](Binding_Redirects/Autogenerating-Binding-Redirects.md)<br><ul><li>`None` - Do nothing except show the warning</li><li>`Preview` - Creates new Web.BindingRedirects.config file showing proposed changes</li><li>`Overwrite` - Updates the $(AppConfig) aka web.config in the project root and any `RazorAppConfigFiles` files</li></ul> |

*Version numbers are not applied if you are using `Microsoft.Build.CentralPackageVersions` version 2.1.1 or higher. Remember to include the packages in your central package versions file.

Expand All @@ -55,12 +56,13 @@ For more information see
| -------- | ------------- | ----------- |
| `MvcBuildViews` | true if Configuration is Release<br/>false otherwise | Whether to invoke the AspNetCompiler automatically after build |
| `EnableWebFormsDefaultItems` | Same as `EnableDefaultItems` | Whether to automatically include WebForms files as content<br><ul><li> *.asax</li><li> *.ascx</li><li> *.ashx</li><li> *.asmx</li><li> *.aspx</li><li> *.master</li><li> *.svc</li></ul> |
| `OverwriteAppConfigWithBindingRedirects` | false | If set, then any [automatically generated binding redirects](Binding_Redirects/Autogenerating-Binding-Redirects.md) will be copied into your web.config and `RazorAppConfigFiles` files. |

### Deprecated Properties

| Property | Default value | Description |
| -------- | ------------- | ----------- |
| `ExcludeDefaultRazorPackages` | false | Use `ExcludeSDKDefaultPackages` instead |
| `OverwriteAppConfigWithBindingRedirects` | false | If set, then any [automatically generated binding redirects](Binding_Redirects/Autogenerating-Binding-Redirects.md) will be copied into your web.config and `RazorAppConfigFiles` files. |

## Automatic Default Packages

Expand All @@ -81,6 +83,8 @@ For more information see

## Items

### Common Items

| Item Name | Default | Description |
| --------- | ------- | ----------- |
| `RazorAppConfigFiles` | `Views/web.config`<br>`Areas/**/web.config` | List of config files that will be updated with binding redirects |
| `RazorAppConfigFiles` | `Views/web.config`<br>`Areas/**/web.config` | List of config files that will be updated with binding redirects in addition to $(AppConfig) |
8 changes: 8 additions & 0 deletions docs/SDK.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,11 @@ You can also use the [templates](Templates.md) to easily create new projects.
| ------- | --------------- | -------- |
| `Microsoft.Net.Compilers.Toolset` | 4.5.0 | `MicrosoftNetCompilersToolset_Version` |
| `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` | 3.6.0 | `MicrosoftCodeDomProvidersDotNetCompilerPlatform_Version` |

## Items

### Common Items

| Item Name | Default | Description |
| --------- | ------- | ----------- |
| `RazorAppConfigFiles` | `Views/web.config`<br>`Areas/**/web.config` | List of config files that will be updated with binding redirects in addition to $(AppConfig) |
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<Copyright>Copyright © 2022</Copyright>
<RazorGeneratorNamespace>ASP</RazorGeneratorNamespace>
<MicrosoftAspNetMvc_Version>5.2.9</MicrosoftAspNetMvc_Version>
<GeneratedBindingRedirectsAction>Overwrite</GeneratedBindingRedirectsAction>
</PropertyGroup>
<ItemGroup>
<RazorSrcFiles Include="Views\**\*.cshtml" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,29 @@
Take an action
OverwriteAppConfigWithBindingRedirects -> this is the SystemWeb Sdk Legacy property
GeneratedBindingRedirectsAction (Overwrite) -> this will conditionally change wich config file is written to

https://github.com/CZEMacLeod/MSBuild.SDK.SystemWeb/issues/58
This file is now common to both the MSBuild.SDK.SystemWeb and MSBuild.SDK.SystemWeb.RazorLibrary SDKs
As MVC Views folders often have their own copy of web.config, we need to ensure we add any binding redirects to those files too.
These are picked up by default into the RazorAppConfigFiles item group
====================================================================================================
-->

<Project>

<ItemGroup Condition="'$(BuildingInsideVisualStudio)'=='true'">
<AvailableItemName Include="RazorAppConfigFiles" />
</ItemGroup>
<ItemDefinitionGroup>
<RazorAppConfigFiles>
<Preview>%(RootDir)%(Directory)%(FileName).BindingRedirects%(Extension)</Preview>
</RazorAppConfigFiles>
</ItemDefinitionGroup>
<ItemGroup Condition="$(EnableDefaultItems)">
<RazorAppConfigFiles Include="Views/web.config" Condition="EXISTS('Views/web.config')" />
<RazorAppConfigFiles Include="Areas/**/web.config" />
</ItemGroup>

<PropertyGroup Label="Change the default BindingRedirects behavior for projects of this SDK type">
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<AutoGenerateBindingRedirects Condition=" '$(AutoGenerateBindingRedirects)' == '' ">true</AutoGenerateBindingRedirects>
Expand Down Expand Up @@ -59,7 +77,9 @@
-->
<Target Name="SystemWebProject_ChooseConfigFileForGenerateBindingRedirects"
BeforeTargets="GenerateBindingRedirects"
Condition="'$(AutoGenerateBindingRedirects)' == 'true' and '$(GenerateBindingRedirectsOutputType)' == 'true' and @(SuggestedBindingRedirects->Count()) > 0 ">
Condition="'$(AutoGenerateBindingRedirects)' == 'true' and
'$(GenerateBindingRedirectsOutputType)' == 'true' and
@(SuggestedBindingRedirects->Count()) > 0 ">

<PropertyGroup Label="Set the location of the file to which the suggestedBindingRedirects should be written during the GenerateBindingRedirects Target">
<_GenerateBindingRedirectsIntermediateAppConfig Condition="'$(GeneratedBindingRedirectsAction)' == 'Preview' " >Web.BindingRedirects.config</_GenerateBindingRedirectsIntermediateAppConfig>
Expand All @@ -68,15 +88,48 @@

<Warning Condition="'$(GeneratedBindingRedirectsAction)' != 'Preview' and '$(GeneratedBindingRedirectsAction)' != 'Overwrite'"
File="$(TargetFileName).config"
Text="Generated Binding Redirects have been applied only to the $(TargetFileName).config. You should incorporate them into the web.config. Consider setting &lt;GeneratedBindingRedirectsAction&gt;Preview&lt;/GeneratedBindingRedirectsAction&gt; to create a file containing the proposals. Consider setting &lt;GeneratedBindingRedirectsAction&gt;Overwrite&lt;/GeneratedBindingRedirectsAction&gt; to automatically update web.config with proposals." />
Text="Generated Binding Redirects have been applied only to the $(TargetFileName).config.
You should incorporate them into the web.config.
Consider setting &lt;GeneratedBindingRedirectsAction&gt;Preview&lt;/GeneratedBindingRedirectsAction&gt; to create a file containing the proposals. Consider setting &lt;GeneratedBindingRedirectsAction&gt;Overwrite&lt;/GeneratedBindingRedirectsAction&gt; to automatically update web.config with proposals." />

<Warning Condition="'$(GeneratedBindingRedirectsAction)' == 'Preview'"
File="Web.BindingRedirects.config"
Text="Generated Binding Redirects have been applied only to the Web.BindingRedirects.config. You should incorporate them into the web.config. Consider setting &lt;GeneratedBindingRedirectsAction&gt;Overwrite&lt;/GeneratedBindingRedirectsAction&gt; to automatically update web.config with proposals." />
Text="Generated Binding Redirects have been applied only to the Web.BindingRedirects.config.
You should incorporate them into the web.config.
Consider setting &lt;GeneratedBindingRedirectsAction&gt;Overwrite&lt;/GeneratedBindingRedirectsAction&gt; to automatically update web.config with proposals." />

<Warning Condition="'$(GeneratedBindingRedirectsAction)' == 'Overwrite'"
File="Web.config"
Text="Generated Binding Redirects have been applied automatically to the Web.config. This warning will disappear on the next build." />
</Target>

<Target Name="SystemWebProject_RazorAppConfigFilesBindingRedirects"
AfterTargets="GenerateBindingRedirects"
Condition="'$(AutoGenerateBindingRedirects)' == 'true' and
'$(GenerateBindingRedirectsOutputType)' == 'true' and
@(RazorAppConfigFiles-&gt;Count()) &gt; 0 and
'$(GeneratedBindingRedirectsAction)' != 'None' and
@(SuggestedBindingRedirects-&gt;Count()) &gt; 0 ">

<GenerateBindingRedirects SuggestedRedirects="@(SuggestedBindingRedirects)"
AppConfigFile="%(RazorAppConfigFiles.Identity)"
OutputAppConfigFile="%(RazorAppConfigFiles.Preview)"
Condition="'$(GeneratedBindingRedirectsAction)' == 'Preview'" />

<Warning Condition="'$(GeneratedBindingRedirectsAction)' == 'Preview'"
File="%(RazorAppConfigFiles.Preview)"
Text="Generated Binding Redirects have been applied only to %(RazorAppConfigFiles.Preview).
You should incorporate them into the %(RazorAppConfigFiles.Identity).
Consider setting &lt;GeneratedBindingRedirectsAction&gt;Overwrite&lt;/GeneratedBindingRedirectsAction&gt; to automatically update the file with proposals." />

<GenerateBindingRedirects SuggestedRedirects="@(SuggestedBindingRedirects)"
AppConfigFile="%(RazorAppConfigFiles.Identity)"
OutputAppConfigFile="%(RazorAppConfigFiles.Identity)"
Condition="'$(GeneratedBindingRedirectsAction)' == 'Overwrite'" />

<Warning Condition="'$(GeneratedBindingRedirectsAction)' == 'Overwrite'"
File="%(RazorAppConfigFiles.Identity)"
Text="Generated Binding Redirects have been applied automatically to %(RazorAppConfigFiles.Identity). This warning will disappear on the next build." />

</Target>
</Project>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

<ItemGroup Condition="$(EnableDefaultItems)">
<None Include="web.config" />
<RazorAppConfigFiles Include="Views/web.config" Condition="EXISTS('Views/web.config')" />
<RazorAppConfigFiles Include="Areas/**/web.config" />
</ItemGroup>

</Project>
5 changes: 0 additions & 5 deletions src/MSBuild.SDK.SystemWeb.RazorLibrary/Sdk/Sdk.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />

<Import Project="MSBuild.SDK.SystemWeb.RazorLibrary.DefaultPackages.props"/>
<Import Project="MSBuild.SDK.SystemWeb.RazorLibrary.BindingRedirects.props"/>
<Import Project="MSBuild.SDK.SystemWeb.RazorLibrary.DefaultItems.props"/>
<Import Project="MSBuild.SDK.SystemWeb.RazorLibrary.RazorGeneratorDirectives.props" />

Expand All @@ -16,8 +15,4 @@
<ProjectCapability Include="SupportsSystemWeb" />
<ProjectCapability Include="LegacyRazorEditor" />
</ItemGroup>

<ItemGroup Condition="'$(BuildingInsideVisualStudio)'=='true'">
<AvailableItemName Include="RazorAppConfigFiles" />
</ItemGroup>
</Project>
5 changes: 3 additions & 2 deletions src/MSBuild.SDK.SystemWeb.RazorLibrary/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
<Reference Include="System.Web.Extensions" />
</ItemGroup>

<Import Project="MSBuild.SDK.SystemWeb.RazorLibrary.BindingRedirects.targets"/>
<Import Project="MSBuild.SDK.SystemWeb.RazorLibrary.DefaultPackages.targets"/>
<Import Project="MSBuild.SDK.SystemWeb.RazorLibrary.RazorGeneratorDirectives.targets" />
<Import Project="MSBuild.SDK.SystemWeb.RazorLibrary.DefaultItems.targets"/>

<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />


<!-- Inject BindingRedirects targets after Microsoft.NET.Sdk is injected -->
<Import Project="MSBuild.SDK.SystemWeb.Common.BindingRedirects.targets" />
</Project>
2 changes: 1 addition & 1 deletion src/MSBuild.SDK.SystemWeb/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

<!-- Inject BindingRedirects targets after Microsoft.NET.Sdk is injected -->
<Import Project="MSBuild.SDK.SystemWeb.BindingRedirects.targets" />
<Import Project="MSBuild.SDK.SystemWeb.Common.BindingRedirects.targets" />

<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" />
</Project>