webapi和mvc的区别,Asp.net MVC4 与 Web Form 并存

 2023-09-19 阅读 19 评论 0

摘要:Web Forms 与 MVC 的asp.net 基础架构是相同的。MVC 的路由机制并不只MVC 特有的,它与WebForm 也是共享相同的路由机制。Web Forms 的Http请求针对的是物理文件,每个页面都实现IhttpHandler,MVC 的Http 请求是针对Controller的Action方法,最终依

      Web Forms 与 MVC 的asp.net 基础架构是相同的。MVC 的路由机制并不只MVC 特有的,它与WebForm 也是共享相同的路由机制。Web Forms 的Http请求针对的是物理文件,每个页面都实现IhttpHandler,MVC 的Http 请求是针对Controller的Action方法,最终依靠MvcHandler 实现对请求的响应。由于Web Forms 与MVC 的基础架构相同,所以Web Forms 与 MVC 可以并存在同一个站点下。

现实情况中存在很多这种需求。历史维护的工程中的老页面使用Web Forms ,重写所有的页面为MVC 方式,成本过大,老工程有添加了新的模块,新的模块完全可以使用MVC 方式来写。
如何演示MVC 与 Web Forms 并存在同一个站点下?
实现目标
    •       能响应Web Forms 的请求,并能在Page 页面使用 高大上的GridView
    •       能响应MVC 请求,并能在View 页面使用 Razor引擎

webapi和mvc的区别、实现过程

 1.    创建空的Asp.net Web Application
      2.  添加 MVC 与Razor 相关的 dll

     

3.  配置Web.config
       
    • Form
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyWebForm.aspx.cs" Inherits="MvcWithWebForm.WebForm.MyWebForm" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvTest" runat="server" >

        </asp:GridView>
    </div>
    </form>
</body>
</html>


    • Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace MvcWithWebForm.WebForm
{
    public partial class MyWebForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            this.BindData();
        }

        private void BindData()
        {
            this.gvTest.DataSource = this.GetCustomerList();
            this.gvTest.DataBind();
        }

        public List<Customer> GetCustomerList()
        {
            List<Customer> list = new List<Customer>();
            for (int i = 0; i < 10; i++)
            {
                Customer c = new Customer() { No = 1000 * i, Name = string.Format("b0b0-{0}",i.ToString()) };
                list.Add(c);
            }
            return list;
        }
    }

 

    public class Customer
    {
        public int No
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }


    }
}
5.  MVC 
    • Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcWithWebForm.Controllers
{
    public class CustomerController:Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }

}
   Controller 代码的位置:必须放到App_Code目录下  

  • View
@inherits System.Web.Mvc.WebViewPage
@{
    ViewBag.Title = "Index";
    
}

<h2>MVC Index</h2>
<div>
    @for (int i = 0; i < 10; i++)
    {
        @Html.Raw(string.Format("<div style=\"font-size:{0}pt\"> Hello,Mvc Razor</div>", (5*i).ToString()));
       
    }
    

</div>


View的位置,必须放到 ~/Views/[Controller]/[ViewName]
6.   Global 配置 路由规则

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Mvc;
using RouteDebug;
namespace MvcWithWebForm
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            //全局路由表 忽略掉MVC 对asp.net Web Forms 请求
            RouteTable.Routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");

            //MVC 路由规则
            RouteTable.Routes.MapRoute(
                "Customer",
                "{controller}/{action}/{id}",
                new { controller = "Customer", action = "Index", id = UrlParameter.Optional } // 参数默认值  
            );
        
        }

      项目结构


 测试
        如果已引用dll,在运行时找不到dll 请设置dll的复制属性为复制到本地。
      
       
        1 WebForm
          
         
        2 MVC 
          

转载于:https://www.cnblogs.com/hbb0b0/p/5090301.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/79934.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息