Upgrading from MVC 4 to MVC 5

if you ‘ve attempted an upgrade from MVC 4 under .Net 4 to MVC 5, under .Net 4.5.1 and get these errors:

Server Error in ‘/’ Application.

Attempt by security transparent method ‘WebMatrix.WebData.PreApplicationStartCode.Start()’ to access security critical method ‘System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)’ failed.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MethodAccessException: Attempt by security transparent method ‘WebMatrix.WebData.PreApplicationStartCode.Start()’ to access security critical method ‘System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)’ failed.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[MethodAccessException: Attempt by security transparent method 'WebMatrix.WebData.PreApplicationStartCode.Start()' to access security critical method 'System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)' failed.]
   WebMatrix.WebData.PreApplicationStartCode.Start() +112

[InvalidOperationException: The pre-application start initialization method Start on type WebMatrix.WebData.PreApplicationStartCode threw an exception with the following error message: Attempt by security transparent method 'WebMatrix.WebData.PreApplicationStartCode.Start()' to access security critical method 'System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)' failed..]
   System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +556
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +132
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +102
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +153
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +516

[HttpException (0x80004005): The pre-application start initialization method Start on type WebMatrix.WebData.PreApplicationStartCode threw an exception with the following error message: Attempt by security transparent method 'WebMatrix.WebData.PreApplicationStartCode.Start()' to access security critical method 'System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)' failed..]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9885060
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34009

in my case I fixed it using Package Manager in VS 2013 install web helpers:

install-package -Id Microsoft.AspNet.WebHelpers

next I continued to update the backend MVC, web API and ASP.Net assemblies using NuGet package manager at the solution level.

Had to manually edit a number of references to version numbers in web.config .. while these versions may change with newer releases, for me they were:
Razor from version 2.0.0.0 to 3.0.0.0,
MVC from version 4.0.0.0 to 5.1.0.0,
WebMatrix.WebData from 2.0.0.0 to 3.0.0.0
WebMatrix.Data from 1.0.0.0 to 2.0.0.0
here’s an exceprt from the root web.config, don’t forget too to check if you have app.config in test projects

<assemblyBindingxmlns=urn:schemas-microsoft-com:asm.v1>
<dependentAssembly>
<assemblyIdentityname=System.Web.HelperspublicKeyToken=31bf3856ad364e35 />
<bindingRedirectoldVersion=0.0.0.0-3.0.0.0newVersion=3.0.0.0 />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentityname=System.Web.WebPagespublicKeyToken=31bf3856ad364e35 />
<bindingRedirectoldVersion=0.0.0.0-3.0.0.0newVersion=3.0.0.0 />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentityname=System.Web.WebPages.RazorpublicKeyToken=31bf3856ad364e35culture=neutral />
<bindingRedirectoldVersion=0.0.0.0-3.0.0.0newVersion=3.0.0.0 />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentityname=System.Web.RazorpublicKeyToken=31bf3856ad364e35culture=neutral />
<bindingRedirectoldVersion=0.0.0.0-3.0.0.0newVersion=3.0.0.0 />
</dependentAssembly>
<assemblyIdentityname=System.Web.MvcpublicKeyToken=31bf3856ad364e35 />
<bindingRedirectoldVersion=0.0.0.0-5.1.0.0newVersion=5.1.0.0 />
</dependentAssembly>
 
<assemblyIdentityname=WebMatrix.WebDatapublicKeyToken=31bf3856ad364e35culture=neutral />
<bindingRedirectoldVersion=0.0.0.0-3.0.0.0newVersion=3.0.0.0 />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentityname=WebMatrix.DatapublicKeyToken=31bf3856ad364e35culture=neutral />
<bindingRedirectoldVersion=0.0.0.0-2.0.0.0newVersion=2.0.0.0 />
</dependentAssembly>
<dependentAssembly>
 …

check too in your web.config under your views:

<configSections>
<sectionGroupname=system.web.webPages.razortype=System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35>
<sectionname=hosttype=System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35requirePermission=false />
<sectionname=pagestype=System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35requirePermission=false />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<hostfactoryType=System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 />

<pages
validateRequest=false
pageParserFilterType=System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35
pageBaseType=System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35
userControlBaseType=System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35>
<controls>
<addassembly=System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35namespace=System.Web.MvctagPrefix=mvc />
</controls>
</pages>

Next after a successful build I had an error at run time in the browser

Cannot re-establish the relationships: UserProfile UsersInRoles and webpages_Membership

this link gave the rather obvious clue:

http://forums.asp.net/t/1856131.aspx?Cannot+re+establish+the+relationships+UserProfile+UsersInRoles+and+webpages_Membership

with the script here: http://forums.asp.net/post/5200488.aspx

CREATE TABLE [webpages_UsersInRoles](
[UserId]int NOT NULL ,
[RoleId]int NOT NULL );
GO 
ALTER TABLE [webpages_UsersInRoles] ADD CONSTRAINT [PK__webpages_UsersInRoles] PRIMARY KEY ([UserId],[RoleId]);
GO 
ALTER TABLE [webpages_UsersInRoles] ADD CONSTRAINT [fk_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [webpages_Roles]([RoleId]) ON DELETE NO ACTION ON UPDATE NO ACTION;
GO 
ALTER TABLE [webpages_UsersInRoles] ADD CONSTRAINT [fk_UserId] FOREIGN KEY ([UserId]) REFERENCES [UserProfile]([UserId]) ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

 

 

Advertisements