在WebForm中使用MVC

1.安装如下dll

安装DLL替代DLL(nuget安装)
System.Web.Abstractions项目-添加引用-框架
System.Web.DynamicData项目-添加引用-框架
System.Web.RazorMicrosoft.AspNet.Razor
System.Web.MvcMicrosoft.AspNet.Mvc
System.Web.WebPages Microsoft.AspNet.WebPages 
System.Web.Optimization

Microsoft.AspNet.Optimization

Microsoft.CSharp使用nuget安装

2.配置Web.config文件,将带有MVC新增的部分拷贝到Web.config配置文件中

<?xml version="1.0" encoding="utf-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!--====MVC新增====-->
    <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>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebForm_Razor-20190326015656.mdf;Initial Catalog=aspnet-WebForm_Razor-20190326015656;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <!--====MVC新增====-->
  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>
  <!--====MVC新增====-->
  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc"/>
        <add namespace="System.Web.Mvc.Ajax"/>
        <add namespace="System.Web.Mvc.Html"/>
        <add namespace="System.Web.Routing"/>
        <add namespace="System.Linq"/>
        <add namespace="System.Collections.Generic"/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>
  <system.web>
	 <authentication mode="Forms">
	   <forms name="Ruyi" loginUrl="Login.aspx" protection="None" timeout="216000" path="/" />
	 </authentication>
	 <authorization>
	   <allow users="?" />
	 </authorization>
    <compilation debug="true" targetFramework="4.8" />
    <httpRuntime maxRequestLength="102400" executionTimeout="720" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization" />
        <add namespace="Microsoft.AspNet.Identity" />
        <!--====MVC新增====-->
        <add namespace="System.Web.Mvc"/>
        <add namespace="System.Web.Mvc.Ajax"/>
        <add namespace="System.Web.Mvc.Html"/>
		<add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing"/>
        <add namespace="System.Linq"/>
        <add namespace="System.Collections.Generic"/>
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
      </controls>
    </pages>
    <membership>
      <providers>
        <!--
          已在此模板中禁用 ASP.NET 成员身份。请访问以下链接 http://go.microsoft.com/fwlink/?LinkId=301889,以了解此模板中的 ASP.NET 成员身份支持
        -->
        <clear />
      </providers>
    </membership>
    <profile>
      <providers>
        <!--
          已在此模板中禁用 ASP.NET 成员身份配置文件。请访问以下链接 http://go.microsoft.com/fwlink/?LinkId=301889,以了解此模板中的 ASP.NET 成员身份支持
        -->
        <clear />
      </providers>
    </profile>
    <roleManager>
      <!--
            已在此模板中禁用 ASP.NET 成员身份角色。请访问以下链接 http://go.microsoft.com/fwlink/?LinkId=301889,以了解此模板中的 ASP.NET 成员身份支持
        -->
      <providers>
        <clear />
      </providers>
    </roleManager>
    <!--
            如果要部署到具有多个 Web 服务器实例的云环境,
            则应将会话状态模式从 "InProc" 更改为“自定义”。此外,
            还应将名为 "DefaultConnection" 的连接字符串更改为连接到
            SQL Server (包括 SQL Azure 和 SQL  Compact)实例,而不是连接到 SQL Server Express 实例。
      -->
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
      </providers>
    </sessionState>
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
   <runtime>
	   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
		 <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.WebPages" 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.3.0.0" newVersion="5.3.0.0" />
		 </dependentAssembly>
	   </assemblyBinding>
 </runtime>
</configuration>

3.注册路由 
在Global.asax文件Application_Start中增加以下代码

void Application_Start(object sender, EventArgs e)
{
    // 在应用程序启动时运行的代码
    Ruyi.Utility.SecurityHelper.IsValidUser(); //检查并写入

    #region----====MVC新增====----
    RouteCollection routes = RouteTable.Routes;

    //避免对 Web 资源文件(例如 WebResource.axd 或 ScriptResource.axd)的请求传递给控制器  
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    //避免aspx页面的请求传递给控制器
    routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");

    //路由注册
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    #endregion

}


4.增加控制器和页面
增加Controllers、Views、Views\Shared文件夹,增加_Layout.cshtml模版文件
添加完成后,正常添加页面即可

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值