-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMvcContainer.cs
45 lines (37 loc) · 1.56 KB
/
MvcContainer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace WebForms.Mvc.Container
{
public class MvcContainer
{
private Controller _controller;
private ViewPage _viewPage;
public class WebFormsController : Controller { }
public HtmlHelper Html { get; private set; }
public UrlHelper Url { get; private set; }
public dynamic ViewBag { get; private set; }
[Obsolete("Please use an HttpContextWrapper or HttpContextBase to construct the MvcContainer")]
public MvcContainer(HttpContext context) : this(new HttpContextWrapper(context)) { }
public MvcContainer(HttpContextBase context)
{
this._controller = new WebFormsController();
this._viewPage = new ViewPage();
InitializeHtmlHelper(context);
}
private void InitializeHtmlHelper(HttpContextBase context)
{
var controllerContext = new ControllerContext(context, new RouteData(), _controller);
var viewContext = new ViewContext(controllerContext, new WebFormView(controllerContext, "Views"), new ViewDataDictionary(), new TempDataDictionary(), TextWriter.Null);
Html = new HtmlHelper(viewContext, _viewPage);
Url = new UrlHelper(new RequestContext(context, RouteTable.Routes.GetRouteData(context) ?? new RouteData()));
ViewBag = viewContext.ViewBag;
}
public void Dispose()
{
_controller.Dispose();
_viewPage.Dispose();
}
}
}