Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 4baa291

Browse files
author
YishaiGalatzer
committed
Add compatibility items for controller back from MVC 5
1 parent 6fe6639 commit 4baa291

File tree

2 files changed

+72
-18
lines changed

2 files changed

+72
-18
lines changed

src/Microsoft.AspNet.Mvc.Core/Controller.cs

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,83 @@
88
using System.Threading.Tasks;
99
using Microsoft.AspNet.Http;
1010
using Microsoft.AspNet.Mvc.Core;
11+
using Microsoft.Framework.DependencyInjection;
1112
using Microsoft.AspNet.Mvc.ModelBinding;
1213
using Microsoft.AspNet.Mvc.Rendering;
14+
using Microsoft.AspNet.Routing;
1315

1416
namespace Microsoft.AspNet.Mvc
1517
{
16-
public class Controller : IActionFilter, IAsyncActionFilter, IOrderedFilter
18+
public class Controller : IActionFilter, IAsyncActionFilter, IOrderedFilter, IDisposable
1719
{
1820
private DynamicViewData _viewBag;
21+
private IViewEngine _viewEngine;
22+
23+
public IServiceProvider Resolver
24+
{
25+
get
26+
{
27+
return ActionContext?.HttpContext?.RequestServices;
28+
}
29+
}
1930

2031
public HttpContext Context
2132
{
2233
get
2334
{
24-
if (ActionContext == null)
25-
{
26-
return null;
27-
}
35+
return ActionContext?.HttpContext;
36+
}
37+
}
38+
39+
public HttpRequest Request
40+
{
41+
get
42+
{
43+
return ActionContext?.HttpContext?.Request;
44+
}
45+
}
2846

29-
return ActionContext.HttpContext;
47+
public HttpResponse Response
48+
{
49+
get
50+
{
51+
return ActionContext?.HttpContext?.Response;
3052
}
3153
}
3254

33-
public ModelStateDictionary ModelState
55+
public RouteData RouteData
3456
{
3557
get
3658
{
37-
if (ViewData == null)
59+
return ActionContext?.RouteData;
60+
}
61+
}
62+
63+
public IViewEngine ViewEngine
64+
{
65+
get
66+
{
67+
if (_viewEngine == null)
3868
{
39-
return null;
69+
_viewEngine = ActionContext?.
70+
HttpContext?.
71+
RequestServices.GetService<ICompositeViewEngine>();
4072
}
4173

42-
return ViewData.ModelState;
74+
return _viewEngine;
75+
}
76+
77+
set
78+
{
79+
_viewEngine = value;
80+
}
81+
}
82+
83+
public ModelStateDictionary ModelState
84+
{
85+
get
86+
{
87+
return ViewData?.ModelState;
4388
}
4489
}
4590

@@ -56,12 +101,7 @@ public IPrincipal User
56101
{
57102
get
58103
{
59-
if (Context == null)
60-
{
61-
return null;
62-
}
63-
64-
return Context.User;
104+
return Context?.User;
65105
}
66106
}
67107

@@ -143,6 +183,7 @@ public virtual ViewResult View(string viewName, object model)
143183
{
144184
ViewName = viewName,
145185
ViewData = ViewData,
186+
ViewEngine = _viewEngine,
146187
};
147188
}
148189

@@ -622,5 +663,17 @@ public virtual async Task<bool> TryUpdateModelAsync<TModel>([NotNull] TModel mod
622663
valueProvider,
623664
bindingContext.ValidatorProvider);
624665
}
666+
667+
[NonAction]
668+
public void Dispose()
669+
{
670+
Dispose(disposing: true);
671+
GC.SuppressFinalize(this);
672+
}
673+
674+
protected virtual void Dispose(bool disposing)
675+
{
676+
}
677+
625678
}
626679
}

test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerFactoryTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ public void DefaultControllerFactory_ReleasesNonIDisposableController()
4444
Assert.DoesNotThrow(() => factory.ReleaseController(controller));
4545
}
4646

47-
private class MyController : Controller, IDisposable
47+
private class MyController : Controller
4848
{
4949
public bool Disposed { get; set; }
50-
public void Dispose()
50+
51+
protected override void Dispose(bool disposing)
5152
{
5253
Disposed = true;
5354
}

0 commit comments

Comments
 (0)