webform 的路由

webform是怎么通过url找到对应handler的呢?

mvc 和webapi的路由都是通过注册到RouteTable.Routes中,然后在urlroutingmodule中路由到对应routehander,那以前webform程序没有注册路由又是怎么找到对应的handler的呢?

在httpapplication中注册事件中有一个MapHandlerExecutionStep

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请求后都会从缓存中获取工厂类然后获取对应的handler

如何实现webform友好访问呢?

现在webform新建程序都会自动添加像mvc路由一样的路由注册文件

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);
        }
    }

 

posted @ 2019-06-11 22:00  不坑不舒服司机  阅读(596)  评论(0编辑  收藏  举报