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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
namespace System.Web.Mvc {
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Web.Mvc.Resources;
using System.Web.UI;
[FileLevelControlBuilder(typeof(ViewUserControlControlBuilder))]
public class ViewUserControl : UserControl, IViewDataContainer {
private AjaxHelper<object> _ajaxHelper;
private DynamicViewDataDictionary _dynamicViewData;
private HtmlHelper<object> _htmlHelper;
private ViewContext _viewContext;
private ViewDataDictionary _viewData;
private string _viewDataKey;
public AjaxHelper<object> Ajax {
get {
if (_ajaxHelper == null) {
_ajaxHelper = new AjaxHelper<object>(ViewContext, this);
}
return _ajaxHelper;
}
}
public HtmlHelper<object> Html {
get {
if (_htmlHelper == null) {
_htmlHelper = new HtmlHelper<object>(ViewContext, this);
}
return _htmlHelper;
}
}
public object Model {
get {
return ViewData.Model;
}
}
public TempDataDictionary TempData {
get {
return ViewPage.TempData;
}
}
public UrlHelper Url {
get {
return ViewPage.Url;
}
}
public dynamic ViewBag {
get {
if (_dynamicViewData == null) {
_dynamicViewData = new DynamicViewDataDictionary(() => ViewData);
}
return _dynamicViewData;
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ViewContext ViewContext {
get {
return _viewContext ?? ViewPage.ViewContext;
}
set {
_viewContext = value;
}
}
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "This is the mechanism by which the ViewUserControl gets its ViewDataDictionary object.")]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ViewDataDictionary ViewData {
get {
EnsureViewData();
return _viewData;
}
set {
SetViewData(value);
}
}
[DefaultValue("")]
public string ViewDataKey {
get {
return _viewDataKey ?? String.Empty;
}
set {
_viewDataKey = value;
}
}
internal ViewPage ViewPage {
get {
ViewPage viewPage = Page as ViewPage;
if (viewPage == null) {
throw new InvalidOperationException(MvcResources.ViewUserControl_RequiresViewPage);
}
return viewPage;
}
}
public HtmlTextWriter Writer {
get {
return ViewPage.Writer;
}
}
protected virtual void SetViewData(ViewDataDictionary viewData) {
_viewData = viewData;
}
protected void EnsureViewData() {
if (_viewData != null) {
return;
}
// Get the ViewData for this ViewUserControl, optionally using the specified ViewDataKey
IViewDataContainer vdc = GetViewDataContainer(this);
if (vdc == null) {
throw new InvalidOperationException(
String.Format(
CultureInfo.CurrentCulture,
MvcResources.ViewUserControl_RequiresViewDataProvider,
AppRelativeVirtualPath));
}
ViewDataDictionary myViewData = vdc.ViewData;
// If we have a ViewDataKey, try to extract the ViewData from the dictionary, otherwise
// return the container's ViewData.
if (!String.IsNullOrEmpty(ViewDataKey)) {
object target = myViewData.Eval(ViewDataKey);
myViewData = target as ViewDataDictionary ?? new ViewDataDictionary(myViewData) { Model = target };
}
SetViewData(myViewData);
}
private static IViewDataContainer GetViewDataContainer(Control control) {
// Walk up the control hierarchy until we find someone that implements IViewDataContainer
while (control != null) {
control = control.Parent;
IViewDataContainer vdc = control as IViewDataContainer;
if (vdc != null) {
return vdc;
}
}
return null;
}
public virtual void RenderView(ViewContext viewContext) {
using (ViewUserControlContainerPage containerPage = new ViewUserControlContainerPage(this)) {
RenderViewAndRestoreContentType(containerPage, viewContext);
}
}
internal static void RenderViewAndRestoreContentType(ViewPage containerPage, ViewContext viewContext) {
// We need to restore the Content-Type since Page.SetIntrinsics() will reset it. It's not possible
// to work around the call to SetIntrinsics() since the control's render method requires the
// containing page's Response property to be non-null, and SetIntrinsics() is the only way to set
// this.
string savedContentType = viewContext.HttpContext.Response.ContentType;
containerPage.RenderView(viewContext);
viewContext.HttpContext.Response.ContentType = savedContentType;
}
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "textWriter", Justification = "This method existed in MVC 1.0 and has been deprecated.")]
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This method existed in MVC 1.0 and has been deprecated.")]
[Obsolete("The TextWriter is now provided by the ViewContext object passed to the RenderView method.", true /* error */)]
public void SetTextWriter(TextWriter textWriter) {
// this is now a no-op
}
private sealed class ViewUserControlContainerPage : ViewPage {
private readonly ViewUserControl _userControl;
public ViewUserControlContainerPage(ViewUserControl userControl) {
_userControl = userControl;
}
public override void ProcessRequest(HttpContext context) {
_userControl.ID = ViewPage.NextId();
Controls.Add(_userControl);
base.ProcessRequest(context);
}
}
}
}
|