我有一个MVC3应用程序,它突然给了我一些奇怪的行为。首先是一些背景(虽然我会尽量使它尽可能简短)。
在我的控制器操作中,我有以下代码:
public ActionResult Grid(ApplicationViewModel search = null)
{
return this.ListView(
this.Find<Entity>(),
this.CreateViewModel,
mixins: new Dictionary<string, Func<EntityViewModel, object>>()
{
{ "Icon", vm => Url.Content("~\Images\entityType.png") },
{ "Link", vm => Url.Action("Details", vm.ControllerId) }
});
}
EntityViewModel CreateViewModel(Entity entity);
// Inherited base class methods
protected IQueryable<T> Find<T>(); // Find T entities in DB
protected ListViewResult<TModel, TViewModel> ListView<TModel, TViewModel>(
IQueriable<TModel> entityQuery,
Func<TModel, TViewModel> materializeViewModel,
IDictionary<string, Func<TViewModel, object>> mixins);此控制器操作隐藏了相当多的复杂逻辑,因为ListViewResult是专门为格式化JSON列表而设计的自定义结果ActionResult。
public class ListViewResult<TModel, TViewModel> :
ActionResult
{
public IQueryable<TModel> ViewData { get; set; }
public Func<TModel, TViewModel> Materialize { get; set; }
public Dictionary<string, Func<TViewModel, object>> Mixins { get; private set; }
...
public override void ExecuteResult(ControllerContext context)
{
// Perform sorting / paging / formatting on IQueryable
...
var viewModels = this.ViewData.Select(this.Materialize);
try
{
// another custom ActionResult for formatting JSON responses
new JsonNetResult()
{
Data = viewModels.ToArray(),
SerializerSettings = new JsonSerializerSettings()
{
ContractResolver = new MixinContractResolver()
{
Mixins = this.Mixins
}
}
}.ExecuteResult(context);
}
catch (Exception e)
{
context.HttpContext.Response.StatusCode = 500;
context.HttpContext.Response.StatusDescription = e.Message;
}
}
private class MixinContractResolver :
CamelCasePropertyNamesContractResolver
{
public Dictionary<string, Func<TViewModel, object>> Mixins { get; set; }
private List<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
List<JsonProperty> props = // get base properties
foreach (var pair in this.Mixins)
{
props.Add(new JsonProperty()
{
Ignored = false,
NullValueHandling = NullValueHandling.Include,
Readable = true,
PropertyName = Inflector.Camelize(pair.Key),
PropertyType = typeof(object),
ValueProvider = new DelegateValueProvider<TViewModel, object>(pair.Value),
Writable = false,
});
}
}
}
private class DelegateValueProvider<T, R> :
Newtonsoft.Json.Serialization.IValueProvider
{
private readonly Func<T, R> func;
public DelegateValueProvider(Func<T, R> func)
{
this.func = func;
}
public object GetValue(object target)
{
return (R)this.func((T)target);
}
public void SetValue(object target, object value)
{
throw new NotSupportedException();
}
}
}现在,似乎偶尔会有一个NullReferenceException被抛到vm => Url.Content(...)和vm => Url.Action(...)行中。这种情况并不总是发生,但是如果我刷新几次,就可以可靠地再现它。
更新
在对源代码进行了一段时间的研究之后,我相信我已经发现了违规的代码。问题似乎与UrlRewriterHelper.WasThisRequestRewritten方法有关,该方法调用ServerVariables.Get("IIS_WasUrlRewritten")。该方法似乎使用了一个名为_request的私有字段,该字段在代码中此时是null (出于某种原因)。我的最佳猜测是,在动作返回和结果执行之间的一段时间内,ServerVariables集合要么会放松对_request - OR的内部引用,要么就是在没有对_request的有效引用的情况下重新创建ServerVariables集合。
我还意识到这可能是IIS Express的一个问题。在下运行时,我无法复制此问题。我更新了标签,以反映出最有可能的原因。
发布于 2013-03-28 01:18:09
我只是在IIS中遇到了同样的问题,发现这是因为我将URL重写扩展名安装到IIS,然后将其卸载,但它在IIS的applicationhost.config文件中留下了一些配置。(C:\Users\Documents\IISExpress\config\applicationhost.config)
我只是注释掉了那个文件中的相关行,杀死了issue和reran,我没有再次看到这个问题。
我注释掉的两行在下面的片段中进行了注释。
<configuration>
...
<system.webServer>
...
<globalModules>
<!--<add name="RewriteModule" image="%IIS_BIN%\rewrite.dll" />-->
...
</globalModules>
</system.webServer>
<location path="" overrideMode="Allow">
<system.webServer>
<modules>
<!--<add name="RewriteModule" />-->
...
</modules>
</system.webServer>
</location>
</configuration>发布于 2013-02-28 23:37:07
签入未签出的visual studio,以便在调试时显示所有异常。由于调试->异常设置,这可能只是内部处理的异常。
检查是否只选中了“我的代码”,以及现在是否在“调试->异常”窗口中显示了两行复选框。
https://stackoverflow.com/questions/15147093
复制相似问题