Introducing SkipStatusCodePagesAttribute attribute to disable StatusCodepages
This commit is contained in:
parent
0549769fd0
commit
3fc61eeac9
|
|
@ -40,6 +40,7 @@ namespace MvcSample.Web
|
||||||
return View();
|
return View();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[SkipStatusCodePages]
|
||||||
public ActionResult NotFound()
|
public ActionResult NotFound()
|
||||||
{
|
{
|
||||||
return HttpNotFound();
|
return HttpNotFound();
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,8 @@ namespace MvcSample.Web
|
||||||
{
|
{
|
||||||
public void Configure(IApplicationBuilder app)
|
public void Configure(IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
|
app.UseStatusCodePages();
|
||||||
|
|
||||||
app.UseFileServer();
|
app.UseFileServer();
|
||||||
#if ASPNET50
|
#if ASPNET50
|
||||||
// Set up configuration sources.
|
// Set up configuration sources.
|
||||||
|
|
@ -129,4 +131,4 @@ namespace MvcSample.Web
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"Kestrel": "1.0.0-*",
|
"Kestrel": "1.0.0-*",
|
||||||
|
"Microsoft.AspNet.Diagnostics": "1.0.0-*",
|
||||||
"Microsoft.AspNet.Mvc": "6.0.0-*",
|
"Microsoft.AspNet.Mvc": "6.0.0-*",
|
||||||
"Microsoft.AspNet.Mvc.Xml": "6.0.0-*",
|
"Microsoft.AspNet.Mvc.Xml": "6.0.0-*",
|
||||||
"Microsoft.AspNet.Mvc.WebApiCompatShim": "6.0.0-*",
|
"Microsoft.AspNet.Mvc.WebApiCompatShim": "6.0.0-*",
|
||||||
|
|
@ -27,4 +28,4 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"webroot": "wwwroot"
|
"webroot": "wwwroot"
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using Microsoft.AspNet.Diagnostics;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Filter to prevent StatusCodePages middleware to handle responses.
|
||||||
|
/// </summary>
|
||||||
|
public class SkipStatusCodePagesAttribute : ResultFilterAttribute
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void OnResultExecuted(ResultExecutedContext context)
|
||||||
|
{
|
||||||
|
var statusCodeFeature = context.HttpContext.GetFeature<IStatusCodePagesFeature>();
|
||||||
|
if (statusCodeFeature != null)
|
||||||
|
{
|
||||||
|
// Turn off the StatusCodePages feature.
|
||||||
|
statusCodeFeature.Enabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
base.OnResultExecuted(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
"warningsAsErrors": true
|
"warningsAsErrors": true
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"Microsoft.AspNet.Diagnostics.Interfaces": "1.0.0-*",
|
||||||
"Microsoft.AspNet.FileProviders": "1.0.0-*",
|
"Microsoft.AspNet.FileProviders": "1.0.0-*",
|
||||||
"Microsoft.AspNet.Hosting": "1.0.0-*",
|
"Microsoft.AspNet.Hosting": "1.0.0-*",
|
||||||
"Microsoft.AspNet.Http.Extensions": "1.0.0-*",
|
"Microsoft.AspNet.Http.Extensions": "1.0.0-*",
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,64 @@
|
||||||
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using Microsoft.AspNet.Diagnostics;
|
||||||
|
using Microsoft.AspNet.Http.Core;
|
||||||
|
using Microsoft.AspNet.Routing;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc.Core.Test
|
||||||
|
{
|
||||||
|
public class SkipStatusCodePagesAttributeTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void SkipStatusCodePagesAttribute_TurnsOfStatusCodePages()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var skipStatusCodeAttribute = new SkipStatusCodePagesAttribute();
|
||||||
|
var resultExecutingContext = CreateResultExecutingContext(new IFilter[] { skipStatusCodeAttribute });
|
||||||
|
var statusCodePagesFeature = new TestStatusCodeFeature();
|
||||||
|
resultExecutingContext.HttpContext.SetFeature<IStatusCodePagesFeature>(statusCodePagesFeature);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
skipStatusCodeAttribute.OnResultExecuted(CreateResultExecutedContext(resultExecutingContext));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.False(statusCodePagesFeature.Enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SkipStatusCodePagesAttribute_Does_Not_Throw_If_Feature_Missing()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var skipStatusCodeAttribute = new SkipStatusCodePagesAttribute();
|
||||||
|
var resultExecutingContext = CreateResultExecutingContext(new IFilter[] { skipStatusCodeAttribute });
|
||||||
|
|
||||||
|
// Act
|
||||||
|
skipStatusCodeAttribute.OnResultExecuted(CreateResultExecutedContext(resultExecutingContext));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ResultExecutedContext CreateResultExecutedContext(ResultExecutingContext context)
|
||||||
|
{
|
||||||
|
return new ResultExecutedContext(context, context.Filters, context.Result, context.Controller);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ResultExecutingContext CreateResultExecutingContext(IFilter[] filters)
|
||||||
|
{
|
||||||
|
return new ResultExecutingContext(
|
||||||
|
CreateActionContext(),
|
||||||
|
filters,
|
||||||
|
new ObjectResult("Some Value"),
|
||||||
|
controller: new object());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ActionContext CreateActionContext()
|
||||||
|
{
|
||||||
|
return new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TestStatusCodeFeature : IStatusCodePagesFeature
|
||||||
|
{
|
||||||
|
public bool Enabled { get; set; } = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue