private void CreateResponseBlocks() { CreateWriteStatusBlock(); _blocks.AddRange(PropertyCookieSetter.GetPropertyCookieSetters(_type, _handler, _context)); var redirectBehaviorInfo = new ResponseBehaviorInfo(typeof(object), typeof(Redirect2), Priority.High); AddBehaviorBlock(redirectBehaviorInfo); foreach (var behaviorInfo in ResponseBehaviorInfo.GetInPriorityOrder()) { AddBehaviorBlock(behaviorInfo); } // Don't write response for HEAD requests if ("HEAD".Equals(_httpMethod, StringComparison.OrdinalIgnoreCase)) { return; } foreach (var behaviorInfo in OutputBehaviorInfo.GetInPriorityOrder()) { // Only one Output block is going to be relevant if (AddBehaviorBlock(behaviorInfo)) { return; } } // If we didn't trigger an Output, try writing a View. _blocks.Add(Expression.Call(_methodLookup.WriteView, _handler, _context)); }
/// <summary> /// Generates a compiled method to run a Handler. /// </summary> /// <returns>A compiled delegate to run the Handler asynchronously.</returns> public Func <IContext, HandlerInfo, Task> BuildAsyncRunMethod(string httpMethod) { var blocks = new List <object>(); blocks.AddRange(CreateBlocks(GetSetupBehaviorInfos())); var setCookieProperties = CookiePropertySetter.GetCookiePropertySetters(_handlerType, _handler, _context); blocks.AddRange(setCookieProperties); var second = new HandlerBlock(_handlerType, GetRunMethod(httpMethod)); blocks.Add(second); var setPropertyCookies = PropertyCookieSetter.GetPropertyCookieSetters(_handlerType, _handler, _context); blocks.AddRange(setPropertyCookies); var redirectBehavior = new ResponseBehaviorInfo(typeof(object), typeof(Redirect2), Priority.High) { Universal = true }; blocks.AddRange(CreateBlocks(GetResponseBehaviorInfos(redirectBehavior))); var outputs = GetOutputBehaviorInfos().ToList(); if (outputs.Count > 0) { blocks.AddRange(CreateBlocks(outputs)); } else { var writeViewBlock = new PipelineBlock(); writeViewBlock.Add(typeof(WriteView).GetMethod("Impl", BindingFlags.Static | BindingFlags.Public)); blocks.Add(writeViewBlock); } var call = BuildCallExpression(blocks); var createHandler = BuildCreateHandlerExpression(); var lambdaBlock = Expression.Block(new[] { _handler }, new[] { createHandler, call }); var lambda = Expression.Lambda(lambdaBlock, _context, _handlerInfoVariable); return((Func <IContext, HandlerInfo, Task>)lambda.Compile()); }