在WebForm中使用MVC

摘自:http://kb.cnblogs.com/page/93085/

解决思路:

不改变ASP.NET管线的请求处理流程,请求还是正常到达一个.aspx页面,然后再转手给ASP.NET MVC。也就是把WebForm作为一个中转站。

好处:

在现有项目中,以最小的代价用上ASP.NET MVC。先品尝一下Razor的美味。

解决方法:

用System.Web.Mvc.MvcHandler可以轻松搞定,请看下面Home.aspx的代码:

 
 
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
    protected override void OnInit(EventArgs e)
    {
        System.Web.Routing.RequestContext requestContext = new System.Web.Routing.RequestContext();
        requestContext.HttpContext = new HttpContextWrapper(Context);
        requestContext.RouteData = new System.Web.Routing.RouteData();
        requestContext.RouteData.Values.Add("controller", "Home");
        requestContext.RouteData.Values.Add("action", "Index");
        IHttpHandler handler = new System.Web.Mvc.MvcHandler(requestContext);
        handler.ProcessRequest(Context);
    }
</script>

运行结果:

项目结构:

看,没有Global.asax。

演示代码下载:

CnblogsMvcDemo0305.rar

更新:

不走寻常路,是为了找到正确的路,感谢老赵指出了正确的路:

在Global.asax.cs的Application_Start中使用下面的代码:

RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.MapRoute(
    "Test.aspx",
    "Test.aspx",
    new { controller = "Test", action = "Index" }
);

转载于:https://www.cnblogs.com/kevin-wang/archive/2011/08/04/2127054.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值