p.s*_*w.g 7 c# asp.net asp.net-mvc iis-express asp.net-mvc-3
我有一个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);
Run Code Online (Sandbox Code Playgroud)
此控制器操作隐藏了大量复杂逻辑,因为它ListViewResult是ActionResult专门为格式化JSON列表而设计的自定义结果.
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,似乎偶尔NullReferenceException的被在该行抛出vm => Url.Content(...)和vm => Url.Action(...).这并不总是发生,但如果我刷新几次,我可以可靠地重现它.
更新
在源代码中挖掘了一段时间之后,我相信我已经发现了有问题的代码.问题似乎与UrlRewriterHelper.WasThisRequestRewritten调用的方法有关ServerVariables.Get("IIS_WasUrlRewritten").看来,方法使用名为私有字段的_request是null在代码中的这点(出于某种原因).我最好的猜测是,在操作返回和执行结果之间的某个时间,ServerVariables集合要么丢失它的内部引用_request- 或者 - ServerVariables正在重新创建集合而没有有效的引用_request.
我也意识到这可能是IIS Express的一个问题.在Visual Studio开发服务器下运行时,我无法复制该问题.我已经更新了标签,以反映出最可能的原因.
小智 3
我刚刚在 IIS Express 上遇到了同样的问题,发现这是因为我安装了 IIS 的 URL 重写扩展,然后将其卸载,但它在 IIS Express 的 applicationhost.config 文件中留下了一些配置。(C:\Users\\Documents\IISExpress\config\applicationhost.config)
我只是注释掉了该文件中的相关行,终止了 IIS Express 并重新运行,但我没有再次看到该问题。
我注释掉的两行在下面的代码片段中进行了注释。
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1174 次 |
| 最近记录: |