webform 的路由

webform是怎麼經過url找到對應handler的呢?

mvc 和webapi的路由都是經過註冊到RouteTable.Routes中,而後在urlroutingmodule中路由到對應routehander,那之前webform程序沒有註冊路由又是怎麼找到對應的handler的呢?web

在httpapplication中註冊事件中有一個MapHandlerExecutionStepapi

internal override void BuildSteps(WaitCallback stepCallback)
    {
       .......
        arrayList.Add(new HttpApplication.MapHandlerExecutionStep(application));
        ......
      
    }

在這個事件中緩存

void HttpApplication.IExecutionStep.Execute()
    {
        ........
        context.Handler = this._application.MapHttpHandler(context, request.RequestType, request.FilePathObject, request.PhysicalPathInternal, false);
        ......
    }
// System.Web.HttpApplication
internal IHttpHandler MapHttpHandler(HttpContext context, string requestType, VirtualPath path, string pathTranslated, bool useAppConfig)
{
    IHttpHandler httpHandler = (context.ServerExecuteDepth == 0) ? context.RemapHandlerInstance : null;
    using (new ApplicationImpersonationContext())
    {
        if (httpHandler != null)
        {
            return httpHandler;
        }
        HttpHandlerAction handlerMapping = this.GetHandlerMapping(context, requestType, path, useAppConfig);
        ........
      //根據請求信息包裝對應工廠類並放入緩存中,每次從緩存中獲取 IHttpHandlerFactory factory
= this.GetFactory(handlerMapping); try {
        //獲取handler IHttpHandlerFactory2 httpHandlerFactory
= factory as IHttpHandlerFactory2; if (httpHandlerFactory != null) { httpHandler = httpHandlerFactory.GetHandler(context, requestType, path, pathTranslated); } else { httpHandler = factory.GetHandler(context, requestType, path.VirtualPathString, pathTranslated); } } ......... } return httpHandler; }

因此每次webform或者handler請求後都會從緩存中獲取工廠類而後獲取對應的handlermvc

如何實現webform友好訪問呢?

如今webform新建程序都會自動添加像mvc路由同樣的路由註冊文件app

public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // 在應用程序啓動時運行的代碼
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }

    public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings);
        }
    }
相關文章
相關標籤/搜索