Skip to content

Commit

Permalink
Fix RedirectResult for AspNetCore 2.2. Fixes #3986
Browse files Browse the repository at this point in the history
  • Loading branch information
alrod committed Jan 25, 2019
1 parent afb6042 commit 986f6fe
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Routing;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Logging;
Expand Down Expand Up @@ -62,11 +63,7 @@ public async Task Invoke(HttpContext context)
return;
}

var actionContext = new ActionContext
{
HttpContext = context
};

ActionContext actionContext = new ActionContext(context, new RouteData(), new ActionDescriptor());
await result.ExecuteResultAsync(actionContext);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"bindings": [
{
"type": "httpTrigger",
"name": "req",
"direction": "in",
"methods": [ "get" ],
"authLevel": "anonymous"
},
{
"type": "http",
"name": "$return",
"direction": "out"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
return new RedirectResult("http://bing.com");
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ public async Task VerifyHostHeader()
Assert.Equal($"{actualProtocol}://{actualHost}/{path}", url);
}

[Fact]
public async Task VerifyResultRedirect()
{
const string path = "api/httptrigger-redirect";
var request = new HttpRequestMessage
{
RequestUri = new Uri(string.Format($"http://localhost/{path}")),
Method = HttpMethod.Get
};

var response = await Fixture.Host.HttpClient.SendAsync(request);
Assert.Equal(response.StatusCode, HttpStatusCode.Redirect);
}

[Fact]
public async Task MultipleOutputs()
{
Expand Down Expand Up @@ -406,6 +420,7 @@ public override void ConfigureJobHost(IWebJobsBuilder webJobsBuilder)
"AssembliesFromSharedLocation",
"HttpTrigger-Dynamic",
"HttpTrigger-Scenarios",
"HttpTrigger-Redirect",
"HttpTriggerToBlob",
"FunctionExecutionContext",
"LoadScriptReference",
Expand Down

1 comment on commit 986f6fe

@mmmpie
Copy link

@mmmpie mmmpie commented on 986f6fe Jan 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HomepageMiddleware.cs also incorrectly calls the default constructor for ActionContext.

I don't know how ActionContext changed from 2.1, but the default constructor is not safe anymore.

public class ActionContext
    {
        /// <summary>
        /// Creates an empty <see cref="ActionContext"/>.
        /// </summary>
        /// <remarks>
        /// The default constructor is provided for unit test purposes only.
        /// </remarks>
        public ActionContext()
        {
            ModelState = new ModelStateDictionary();
        } 

Please # to comment.