How to set up project of htmlHelpers so that it pr

2019-04-08 22:05发布

问题:

I have a library of html helpers as a separate project. Right now it references system.web.mvc of version 4.

If I try to use this library in a project that uses mvc3 it throws an error when a helper is attempted to be used:

Compiler Error Message: CS1705: Assembly 'MyHtmlHelpers, Version=3.7.4.0, Culture=neutral, PublicKeyToken=null' uses 'System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

I understand that I need to build my helpers project with system.web.mvc of version 3 referenced in order for it to be compatible with mvc3 project. I do not want to have two projects with the same code with just the difference of system.web.mvc referenced.

Question is in the title. Any help is appreciated.

回答1:

I do not want to have two projects with the same code with just the difference of system.web.mvc referenced.

You don't necessarily need to physically duplicate the source code. You could just have another .csproj for your class library which will add the source code as Linked Files and only replace the version of the System.Web.Mvc reference in the .csproj.

Take a look at how standard projects such as JSON.NET have their source code organized. They support multiple versions of .NET from 2.0 up to 4.0 and yet the source code is the same. It's the project files that are doing the job.