首页 > 其他分享 >WebForm混合MVC

WebForm混合MVC

时间:2022-10-31 18:11:14浏览次数:46  
标签:Web viewBag 混合 MVC WebForm action aspx

 WebForm混合MVC

WebForm模拟MVC

参考文章地址:https://blog.csdn.net/cownew/article/details/50400933

1、创建WebForm项目

2、新建AddView.aspx页面

删除AddView.aspx.cs和AddView.aspx.designer.cs两个文件

 

AddView.aspx页面内容如下:

 

<%@ Page Language="C#" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <%dynamic viewBag = Context.Items["ViewBag"]; %>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>加法计算器</title>
</head>
<body>
    <form id="form1">
        <input type="hidden" name="action" value="addSubmit" />
        <input type="text" name="i1" value="<%=viewBag.i1 %>" />+<input type="text" name="i2" value="<%=viewBag.i2%>" />
        <input type="submit" value="=" /><input type="text" value="<%=viewBag.i3 %>" />
    </form>
</body>
</html>

 

 

3、创建AddHandler.ashx一般处理程序

用来充当控制器的作用,C#代码如下:

 

public void ProcessRequest(HttpContext context)
        {
            string action = context.Request["action"];
            dynamic viewBag = new System.Dynamic.ExpandoObject();
            if (string.IsNullOrEmpty(action))
            {
                viewBag.i1 = "";
                viewBag.i2 = "";
                viewBag.i3 = "";
            }
            else if (action == "addSubmit")
            {
                int i1 = Convert.ToInt32(context.Request["i1"]);
                int i2 = Convert.ToInt32(context.Request["i2"]);
                int i3 = i1 + i2;
                viewBag.i1 = i1;
                viewBag.i2 = i2;
                viewBag.i3 = i3;
            }
            context.Items["ViewBag"] = viewBag;
            context.Server.Transfer("AddView.aspx");
            //---------------------
            //作者:cownew
            //来源:CSDN
            //原文:https://blog.csdn.net/cownew/article/details/50400933 
            //版权声明:本文为博主原创文章,转载请附上博文链接!
        }

 

4、运行项目

 

在浏览器端输入

http://localhost:6951/AddHandler.ashx?action=addSubmit

或者http://localhost:6951/AddHandler.ashx?action=addSubmit&i1=3&i2=5

 

这样就实现了MVC页面加载功能啦。

 

=====================================================================

 

WebForm项目嵌套MVC项目

参考文章地址:http://www.cnblogs.com/encoding/articles/3556046.html

1、为WebForm项目添加引用

 

System.Web.Abstractions;

System.Web.DynamicData;

System.Web.Mvc;

System.Web.Optimization;

System.Web.Razor;

System.Web.WebPages;

 

MVC有的文件夹:Controllers,Views , Models,Scripts,Content一个都不能少

 

 

 

2、配置Web.config文件

 

 

<?xml version="1.0" encoding="utf-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

    <!--====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>

  <!--====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>


  <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>

  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.2" />

    <!--====MVC新增(这里可以注释掉,不会报错)====-->
    <!--
    <assemblies>
      <add assembly="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </assemblies>
    </compilation>-->

    <httpRuntime targetFramework="4.5.2" />
    <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.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>
    <!--====MVC新增====-->
    <modules runAllManagedModulesForAllRequests="true">

      <remove name="FormsAuthentication" />
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" culture="neutral" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" culture="neutral" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" culture="neutral" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <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.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>

 

 

3、注册路由

 

            #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、配置好了,开始写控制器和页面

 

 

 

5、排除报错原因

运行Index.cshtml发现报错:

 

 

搜索关键字:

找到该文件,把相关代码注释掉

 

6、最后,运行成功!

 

 

 

 

 

搜索

复制

标签:Web,viewBag,混合,MVC,WebForm,action,aspx
From: https://www.cnblogs.com/jankie1122/p/11124538.html

相关文章

  • day13-(事务&mvc&反射补充)
    回顾:jsp:java服务器页面jsp的脚本jsp的注释html注释java注释jsp注释<%----%>jsp的指令page:声明页面一些属性重要的属性:im......
  • SpringMVC入门(详解1)
    SpringMVC介绍:Springwebmvc属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来:springMVC详解: 使用范围: 执行流程:架构:文字描述:用户发送请求......
  • SpringMVC详解
     SpringMVC的介绍 【1】SpringWebMVC是基于ServletAPI构建的原始Web框架,从一开始就已包含在Spring框架中。正式名称“SpringWebMVC”来自其源模块的名称(spring-we......
  • MVVM与MVC
      MVVM与MVC的区别有:1、mvvm各部分的通信是双向的,而mvc各部分通信是单向的;2、mvvm是真正将页面与数据逻辑分离放到js里去实现,而mvc里面未分离MVC:包括view视图层、......
  • SSM框架(spring+springmvc+mybatis)+Mysql实现的社区物业管理系统(功能包含分角色,报修
    (博客目录)SSM框架(spring+springmvc+mybatis)+Mysql实现的社区物业管理系统本系统为了解决线下物业管理小区的冗余繁杂的难题,通过整合业主和物业,将平时的业务在线化,节省......
  • SSM框架(spring+springmvc+mybatis)+layui+Mysql实现的停车位租赁系统(功能包含注册登
    (博客目录)SSM框架(spring+springmvc+mybatis)+layui+Mysql实现的停车位租赁系统本系统为了解决停车位租赁不透明的难题,提供了一个连接租客和车位主的一个平台,通过不同角......
  • SSM框架(spring+springmvc+mybatis)+layui+Mysql实现的医院在线预约挂号管理系统(功能
    (博客目录)SSM框架(spring+springmvc+mybatis)+layui+Mysql实现的医院在线预约挂号管理系统本系统为了患者线下预约挂号难的问题,打通医院和患者之间的一个渠道,通过在线透......
  • SpringMVC源码-DispatcherServlet初始化
    web容器启动后会实例化Servlet,会执行Servlet的init方法且只会执行一次。后续调用doService处理客户请求。DispatcherServlet的构造方法publicDispatcherServlet(){ su......
  • mvvm与mvc模型
    什么是mvcmvc模式是modelviewcontrol通过控制器control分层开发是一种通过将复杂的业务逻辑抽离为智能单一的小模块,保证了模块的单一性,方便程序的开发维护,耦合度低什......
  • Spring源码-SpringMVC-搭建springmvc环境
    一、新建模块myself-web新建gradle的web项目,右键项目名,选择NEW-Moudle.左边选择Gradle,右下选择web即可。build.gradleplugins{id'java'id'war'id"com.bmuschko......