ASP.NET MVC 路由進階(之一)

1.  MVC框架下的WebForm頁面。web

 

咱們在MVC項目下新建一個WebForm頁面。瀏覽器

而後右鍵瀏覽,打開頁面,以下圖:架構

發現頁面可以正常訪問。ok,咱們嘗試改一下Global.asax.cs中的代碼,以下app

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MvcMobileDMS
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            //GlobalFilters.Filters.Add(new MvcMobileDMS.App_Start.ActionAttributeFilter());
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            RouteTable.Routes.RouteExistingFiles = true;
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

咱們加入了一句代碼RouteTable.Routes.RouteExistingFiles = true;如今,咱們再看看剛纔的頁面效果:框架

這時,就能夠看出端倪了,沒錯,就是RouteTable.Routes.RouteExistingFiles 屬性值決定了WebForm頁面的路由監測,默認是false,當值爲true時ui

MVC的路由監測則屏蔽WebForm頁面,並提示錯誤。this

 

2.  WebForm中使用路由。spa

在webform項目中,若是咱們要改寫地址,能夠採用地址重寫組件方法,可是,如今在MVC下,咱們能夠嘗試下面的方法:code

 

添加 類文件WebFormRouteHandler.cs,代碼以下:orm

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Compilation;
using System.Web.UI;

namespace DMSWebApplication.App_Start
{
    public class WebFormRouteHandler:IRouteHandler
    {
        public WebFormRouteHandler(string virtualPath)
        {
            this.VirtualPath = virtualPath;
        }

        public string VirtualPath { get;private set; }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var page = BuildManager.CreateInstanceFromVirtualPath(VirtualPath,typeof(Page)) as IHttpHandler;
            return page;
        }
    }
}

在全局應用程序類Global.asax.cs中添加以下代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using DMSWebApplication;

namespace DMSWebApplication
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterOpenAuth();
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.Add("index", new Route("foo/bar", new DMSWebApplication.App_Start.WebFormRouteHandler("~/foo/haha.aspx")));
            routes.Add("next", new Route("ha/yt", new DMSWebApplication.App_Start.WebFormRouteHandler("~/app/ye.aspx")));
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }
    }
}

當我在瀏覽器地址欄輸入以下地址時,http://localhost:34365/ha/yt,訪問的是http://localhost:34365/app/ye.aspx,以下圖:

當我在瀏覽器地址欄輸入以下地址時,http://localhost:34365/foo/bar,訪問的是http://localhost:34365/foo/haha.aspx,以下圖:

有了這個特性,咱們在傳統的WebForm過渡到MVC架構時,提供了極大的方便,並慢慢過渡到MVC框架。

 

好了,今天寫到這裏。但願能對你有所幫助O(∩_∩)O哈哈~。

相關文章
相關標籤/搜索