编译时出现ASP.Net 4.5错误
本文关键字:错误 Net ASP 编译 | 更新日期: 2023-09-27 18:28:33
下面是我的代码,编译时出现3个错误,如所示
任何帮助都将不胜感激。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;
namespace Build01
{
public static class ModelBindingExtensions
{
public static ModelBindingExecutionContext GetModelBindingExecutionContext(this Page page)
{
return new ModelBindingExecutionContext
{
HttpContext = new HttpContextWrapper(HttpContext.Current),
ModelState = page.ModelState
};
}
}
}
错误1"System.Web.ModelBinding.ModelBindingExecutionContext"不存在不包含接受0个参数的构造函数错误2属性或索引器"HttpContext"无法分配给--它是只读的
错误3无法将属性或索引器"ModelState"分配给--it是只读
ModelBindingExecutionContext
没有接受0个参数的构造函数。但是它确实有一个同时接受HttpContext
和ModelStateDictionary
的,所以您需要将它们传递给构造函数:
return new ModelBindingExecutionContext(
new HttpContextWrapper(HttpContext.Current), page.ModelState);
使用类构造函数的语法似乎不正确。与其使用{ }
,不如按原样使用( )
:
public static ModelBindingExecutionContext GetModelBindingExecutionContext(this Page page)
{
return new ModelBindingExecutionContext
( // note ( not {
new HttpContextWrapper(HttpContext.Current),
page.ModelState
);
}
{ }
的语法可以用于设置类属性,但前提是它们具有公共setter。在这种情况下,HttpContext
和ModelState
需要通过对象构造函数进行设置,并且没有公共setter。