Rewriting the Url with POST data in the Applicatio

2019-05-08 07:14发布

I have a code which registers the route in Application_AcquireRequestState event of the application. Once the routes are registered, I set a flag in the Http runtime cache so that I don't execute the route registration code again. There is a specific reason to register the route in this event Application_AcquireRequestState.

Once the app pool is restarted and if a valid(matching the route) request comes in, the route registration code kicks in but that request is not served by IIS/ASP.Net and it returns 404. The subsequent valid requests are all working fine.

I want to make sure even the first request is also served correctly.

Is it possible to rewrite the request so that after the route registration is done we can somehow try to replay the request if the url matches with one of the route that are registered? Is there any solution to solve this problem?

2条回答
Rolldiameter
2楼-- · 2019-05-08 08:11

As per below

events order

and below

events

And the below SO thread

When does routing take place in the pipeline?

You may need to target something between AuthenticateRequest or PostAuthorizeRequest event to do your URL registration as the routing happens after that

The Url routing happens just after PostAuthorizeRequest event and since the routes will already be registered, the first request will also be served fine.

查看更多
放我归山
3楼-- · 2019-05-08 08:14

This is a kind of pseudocodewhich you can use in Global.asax.

private bool RootIsRegistered = false; //register Application level var

void Application_BeginRequest(object sender,EventArgs e){
   if(!RootIsRegistered)
      RegisterRoots();
}

This way you can be sure that your roots are registered even at the first request.

查看更多
登录 后发表回答