2016年12月7日水曜日

ASP.NET で、Request Routing

aspx ではなく、ashx を使いたかった。

Global.asax を追加。SimpleRoute クラスを定義。そして、IHttpHandler の ashx クラスを Callback の中で new します。

    public class Global : System.Web.HttpApplication {

        protected void Application_Start(object sender, EventArgs e) {
            RegisterRoutes(RouteTable.Routes);
        }

        class SimpleRouter : IRouteHandler {
            public Func<RequestContext, IHttpHandler> Callback;

            public IHttpHandler GetHttpHandler(RequestContext requestContext) {
                return Callback(requestContext);
            }
        }

        public static void RegisterRoutes(RouteCollection routes) {
            routes.Add(new Route("newToken", new SimpleRouter { Callback = (e) => { return new newToken(); } }));
            routes.Add(new Route("uploadPicture", new SimpleRouter { Callback = (e) => { return new uploadPicture(); } }));
            routes.Add(new Route("uploadText", new SimpleRouter { Callback = (e) => { return new uploadText(); } }));
        }

0 件のコメント:

コメントを投稿