asp.net mvc 4 项目升级到 asp.net mvc5

时间:2022-12-10 23:49:19

一、开始

1、打开或新建asp.net mvc 4项目

2、修改 global.asax文件

原:

WebApiConfig.Register(GlobalConfiguration.Configuration);

新:

GlobalConfiguration.Configure(WebApiConfig.Register);

3、删除以下ASP.NET NuGet packages ,使用 Package Manager Console (PMC)进行移除

Uninstall-Package -Id Microsoft.AspNet.WebPages.Administration

Uninstall-Package -Id Microsoft-Web-Helpers

Uninstall-Package -Id Microsoft.AspNet.Mvc.FixedDisplayModes

4、更新ASP.NET NuGet packages 

Update-Package

 

二、更新Application web.config

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <!--Two elements removed for Clarity -->
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    </dependentAssembly>
    <!--WebGrease element removed for Clarity -->
  </assemblyBinding

  

修改<appSettings>

<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

 修改securityPolicy

<securityPolicy>
<!--<trustLevel name="Medium" policyFile="web_mediumtrust.config"/>-->
</securityPolicy>

 三、更新 Views 文件夹中的web.config 

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<!--Elements removed for Clarity.-->
</namespaces>
</pages>
</system.web.webPages.razor>

  

 <pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>

  

<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>

安装Microsoft-Web-Helpers

Install-Package -Id Microsoft.AspNet.WebHelpers

如果有用到 User.IsInRole() ,添加以下代码

<system.webServer>
<modules>
<remove name="RoleManager" />
</modules>
</system.webServer>

四、最后

修改ProjectName.csproj

找到 ProjectTypeGuids 元素并移除: MVC 4 project GUID, {E3E379DF-F4C6-4180-9B81-6769533ABE47}

重新加载项目、编译、成功。