ASP.NET MVC Helpers, MVC Independent Version

I would like to create some “extension-based HTML helpers” in a separate (C #) project. These html helpers are described, for example, at www.asp.net .

Since the methods are extensions of the HtmlHelper class, and the HtmlHelper class is located inside the System.Web.Mvc assembly , the project needs a link to this assembly. But then the project depends on the specific version of MVC (3, 4, 5, etc.).

I would like to write MVC helpers that are not dependent on the version of MVC (but based on the extension). Is it possible?

I am aware of the configuration of <bindingRedirect /> , but it would be nice if the client (the project using MVC helpers) were not forced to use this <bindingRedirect /> element.

+5
source share
2 answers

You should not worry that you need to worry that the client has to add binding redirects. This was a problem a few years ago that Nuget solved.

So instead of sharing the dll, share the nuget package. You will probably need several versions, as binding redirects do not work from MVC 3 to MVC 5 (due to removal of trust changes in MVC 5). This way you will receive a total of two nuget packages (search AutoFac MVC on nuget.org to see what they have done), or just support MVC 5 and above, it’s been over a year now.

Put the nuget package either in nuget.org (or you can use the private nuget chip), and make sure your package has MVC dependency with the correct versions as major and minor. Perhaps some of them may correspond to MVC versions.

The good thing about nuget is that for compatible versions (e.g. MVC 5, 5.1, 5.2, 5.2.2, etc.) you only need one dll, and Nuget will automatically add the binding redirects without the end user having to enter them.

How to create a nuget package

+1
source

I'm not sure if there is a better way to make "conditional dependencies", but the way I do it is to manually edit the .csproj file. For example, here is an example MvcSiteMapProvider project:

 <ItemGroup Condition=" $(DefineConstants.Contains('MVC2')) "> <Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> </ItemGroup> <ItemGroup Condition=" $(DefineConstants.Contains('MVC3')) "> <!-- Due to the windows update MS14-059, we need this hack to ensure we can build MVC3 both on machines that have the update and those that don't --> <Reference Condition=" Exists('$(windir)\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll') " Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> <Reference Condition=" !Exists('$(windir)\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll') " Include="System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Private>True</Private> <HintPath>..\packages\Microsoft.AspNet.Mvc.3.0.20105.1\lib\net40\System.Web.Mvc.dll</HintPath> </Reference> <Reference Include="System.Web.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Private>True</Private> <HintPath>..\packages\Microsoft.AspNet.Razor.1.0.20105.408\lib\net40\System.Web.Razor.dll</HintPath> </Reference> <Reference Include="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Private>True</Private> <HintPath>..\packages\Microsoft.AspNet.WebPages.1.0.20105.408\lib\net40\System.Web.WebPages.Razor.dll</HintPath> </Reference> </ItemGroup> <ItemGroup Condition=" $(DefineConstants.Contains('MVC4')) "> <Reference Include="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Private>True</Private> <HintPath>..\packages\Microsoft.AspNet.Mvc.4.0.20710.0\lib\net40\System.Web.Mvc.dll</HintPath> </Reference> <Reference Include="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Private>True</Private> <HintPath>..\packages\Microsoft.AspNet.Razor.4.0.20715.0\lib\net40\System.Web.Razor.dll</HintPath> </Reference> <Reference Include="System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Private>True</Private> <HintPath>..\packages\Microsoft.AspNet.WebPages.4.0.20710.0\lib\net40\System.Web.WebPages.Razor.dll</HintPath> </Reference> </ItemGroup> <ItemGroup Condition=" $(DefineConstants.Contains('MVC5')) "> <Reference Include="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Private>True</Private> <HintPath>..\packages\Microsoft.AspNet.Mvc.5.0.0\lib\net45\System.Web.Mvc.dll</HintPath> </Reference> <Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Private>True</Private> <HintPath>..\packages\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll</HintPath> </Reference> <Reference Include="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Private>True</Private> <HintPath>..\packages\Microsoft.AspNet.WebPages.3.0.0\lib\net45\System.Web.WebPages.dll</HintPath> </Reference> <Reference Include="System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Private>True</Private> <HintPath>..\packages\Microsoft.AspNet.WebPages.3.0.0\lib\net45\System.Web.WebPages.Razor.dll</HintPath> </Reference> </ItemGroup> 

And then, of course, there are constants for MVC2, MVC3, MVC4 and MVC5 used throughout the code, as well as for reusing the same code base for each version. When the project is built, the build script passes the MVC version as a parameter, and a separate DLL is created for each version of MVC.

Direct link to csproj file

NOTE. Although this works great, Visual Studio shows unselected links in the link list with a yellow icon, which can be somewhat discouraging. I have not found a way to make this work in such a way that it displays well in Visual Studio or which can be edited using Visual Studio tools.

Full disclosure: I am the main contributor to MvcSiteMapProvider.

+1
source

Source: https://habr.com/ru/post/1209168/


All Articles