• WebForm与MVC混用


    步骤一:添加引用 -> 程序集 -> 扩展 ->

    • System.Web.Mvc ;
    • System.Web.Razor;
    • System.Web.WebPages;
    • System.Web.Abstractions;

    步骤二:mvc默认目录结构 

      新建一个空的mvc项目, 将Controllers和Views文件夹全部拷贝过来(文件结构和web.config这个很重要)

    步骤三:修改配置文件web.config(不是Views文件夹下的)

      1.在compilation节点下加入以下几个assemblies :

    <compilation debug="true" targetFramework="xx">  // 框架版本不用动
          <assemblies>  
            <add assembly="System.Web.Razor, Version=2.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>  

      2. 还有一个需要注意的: 在configuration节点下加入   <modules runAllManagedModulesForAllRequests="true"/>  

         -- 不过我把这个去掉也调试成功了,  这个貌似只是设置每个请求都会执行Global.cs里边的BeginRequest事件而已

    <configuration>
      <system.webServer>
            <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>

    步骤四:路由设置

      1. 可以按照mvc惯例 在App_Start文件夹中创建路由配置静态方法;RouteConfig.cs文件代码如下

    using System;
    using System.Collections.Generic;
    using System.Linq;  
    using System.Web;
    using System.Web.Mvc;   // 引入命名空间 才能注册mvc的路由
    using System.Web.Routing;  // 引入命名空间
    
    namespace webform
    {
        public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");  // web form访问方式
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
    
            }
        }
    }

      2. 然后在Global.cs中注册

        public class Global : System.Web.HttpApplication
        {
    
            protected void Application_Start(object sender, EventArgs e)
            {
                RouteConfig.RegisterRoutes(RouteTable.Routes);  

    接着就可以写你的controller和View

    按照以上步骤调试成功 ╮(╯▽╰)╭ 

     示例代码:Asp.netMigrateToMVC.zip

    ps: 个人对配置文件里的各个节点作用不是很理解, 就新建了一个空mvc项目然后把Views里边的东西全部拷贝过来

  • 相关阅读:
    vue嵌套路由
    不同的网络出现的报错
    yarn 创建react项目时出现错误
    vue-awsome-swiper竖向滚动出现空白
    SpringBoot路径映射
    ApplicationRunner接口
    CommandLineRunner接口
    springboot创建拦截器
    Cors跨域请求
    springboot自定义异常视图
  • 原文地址:https://www.cnblogs.com/mushishi/p/4084365.html
Copyright © 2020-2023  润新知