ASP.NET WebForms and MVC together in one project

混合WebForms与MVC
本文介绍了一种在单一项目中同时使用ASP.NET Web Forms和MVC的方法,通过使用区域来隔离MVC部分,实现URL路由及视图引擎的定制,并解决了Visual Studio MVC项目的识别问题。

ASP.NET WebForms and MVC together in one project

http://blogs.taiga.nl/martijn/2009/06/03/aspnet-webforms-and-mvc-together-in-one-project/

 

This post briefly describes a solution to mix ASP.NET WebForms and MVC in one project. You can download a sample project that might be more useful than my ramblings. Download the sample here , unzip, open with VS 2008 SP1 and hit F5.

There are lots of ‘legacy’ ASP.NET WebForms applications out there in the wild. What if you want to create new functionality or rebuild an existing part with ASP.NET MVC, but you can’t (or won’t) create a separate new project?

The simple solution is to add the default MVC folders (Content, Controlllers, Scripts, Views etc.) to the root of the web project, add references to the MVC binaries, modify web.config and add the MVC GUID to the ProjectTypeGuids in the project file. A major drawback however is that it pollutes the root of your carefully structured application. This post will show you how you can logically separate the new MVC pieces from the existing WebForms app in a single project.

Isolate the MVC bits

webforms-mvc-solution   A simple requirement: I want that all my MVC folders reside in the folder /MyMvcApp and I also want that all urls of the MVC app start with /mymvcapp. No interference with my old WebForms app please.

Area’s

About a year ago, Phil Haacked showed a concept called area’s to partition an ASP.NET MVC application. After that, Steve Sanderson came up with an improved version of the concept.

Although designed to partition an MVC application, area’s are also useful when we want to isolate our MVC application from our existing WebForms app. The solution from Steve Sanderson almost completely fits our needs, follow the link for more technical details. The only thing I removed was the usage of an Areas subfolder in the project where all the individual MVC sub-applications. Simply didn’t need the extra subfolder and url of the MVC app is closer to the physical structure.

So, I implemented area’s with a specific AreaViewEngine that can lookup views based on the area name and added the extension to the RouteCollection class to add area information when mapping routes. This is how Global.asax.cs looks:

protected

 void

 Application_Start(object

 sender, EventArgs e)
{
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new

 AreaViewEngine());
 
    // Routes

    RegisterRoutes(RouteTable.Routes);
}
 
public

 static

 void

 RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"

);
    routes.IgnoreRoute("{*favicon}"

, new

 { favicon = @"(.*/)?favicon.ico(/.*)?"

 });
 
    routes.CreateArea("mymvcapp"

, "WebFormsMVCDemo.MyMvcApp.Controllers"

,
        routes.MapRoute("mymvcapp-defaultroute"

, "mymvcapp/{controller}/{action}/{id}"

, new

 { action = "Index"

, controller = "Home"

, id = ""

 })
    );
}

With the ViewEngine and routes in place, we’re able to run the MVC app from the url /mymvcapp. To make sure the WebForms url’s still work, we just have to add some ignore statements in Global.asax.cs (in our case, exclude .aspx and .ashx from routing):

public

 static

 void

 RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"

);
    routes.IgnoreRoute("{*allaspx}"

, new

 { allaspx = @".*/.aspx(/.*)?"

 });
    routes.IgnoreRoute("{*allashx}"

, new

 { allashx = @".*/.ashx(/.*)?"

 });
    routes.IgnoreRoute("{*favicon}"

, new

 { favicon = @"(.*/)?favicon.ico(/.*)?"

 });
 
    routes.CreateArea("mymvcapp"

, "WebFormsMVCDemo.MyMvcApp.Controllers"

,
        routes.MapRoute("mymvcapp-defaultroute"

, "mymvcapp/{controller}/{action}/{id}"

, new

 { action = "Index"

, controller = "Home"

, id = ""

 })
    );
}

One caveat: when performing the request for /mymvcapp without controller name (and action), the Default.aspx that comes with the MVC projects was loaded, but area’s seem so cause a problem with the default one. Instead of rewriting the url from ‘default.aspx’ to ‘/’, I had to rewrite to url to ‘Home/Index’:

public

 partial

 class

 _Default : Page
{
    public

 void

 Page_Load(object

 sender, System.EventArgs e)
    {
        string

 pathToRewriteTo = Request.Path.ToLowerInvariant().Replace("default.aspx"

, "Home/Index"

);
        HttpContext.Current.RewritePath(pathToRewriteTo, false

);
        IHttpHandler httpHandler = new

 MvcHttpHandler();
        httpHandler.ProcessRequest(HttpContext.Current);
    }
}

I’m under the impression that I’m missing something very simple, but couldn’t get my fingers behind it, so if you know it, please comment.

 

That’s it. I created a sample project with both WebForms and an MVC app in a subfolder. You can download it here .

【事件触发一致性】研究多智能体网络如何通过分布式事件驱动控制实现有限时间内的共识(Matlab代码实现)内容概要:本文围绕多智能体网络中的事件触发一致性问题,研究如何通过分布式事件驱动控制实现有限时间内的共识,并提供了相应的Matlab代码实现方案。文中探讨了事件触发机制在降低通信负担、提升系统效率方面的优势,重点分析了多智能体系统在有限时间收敛的一致性控制策略,涉及系统模型构建、触发条件设计、稳定性与收敛性分析等核心技术环节。此外,文档还展示了该技术在航空航天、电力系统、机器人协同、无人机编队等多个前沿领域的潜在应用,体现了其跨学科的研究价值和工程实用性。; 适合人群:具备一定控制理论基础和Matlab编程能力的研究生、科研人员及从事自动化、智能系统、多智能体协同控制等相关领域的工程技术人员。; 使用场景及目标:①用于理解和实现多智能体系统在有限时间内达成一致的分布式控制方法;②为事件触发控制、分布式优化、协同控制等课题提供算法设计与仿真验证的技术参考;③支撑科研项目开发、学术论文复现及工程原型系统搭建; 阅读建议:建议结合文中提供的Matlab代码进行实践操作,重点关注事件触发条件的设计逻辑与系统收敛性证明之间的关系,同时可延伸至其他应用场景进行二次开发与性能优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值