我正在MVC 6中构建一次性应用程序,并尝试使用不同的依赖架构.
我面临的问题是如何创建MyAppContext
特定于应用程序的自定义对象.这将需要HttpContext
来自数据库的一些信息和来自数据库的一些信息,并且将是针对特定于应用程序的属性的请求范围的存储库.我想将实例传递HttpContext
给' MyAppContext
' 的构造函数.
我已成功使用DI 创建了一个DataService
带有IDataService
接口的对象,这可以正常工作.与'MyAppContext'类的不同之处在于它在构造函数中有两个参数 - DataService
'和' Microsoft.AspNet.Http.HttpContext
.这是MyAppContext类:
public class MyAppContext : IMyAppContext
{
public MyAppContext(IDataService dataService, HttpContext httpContext)
{
//do stuff here with the httpContext
}
}
Run Code Online (Sandbox Code Playgroud)
在启动代码中,我注册了DataService实例和MyAppContext实例:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//adds a singleton instance of the DataService using DI
services.AddSingleton<IDataService, DataService>();
services.AddScoped<IMyAppContext, MyAppContext>();
}
public void Configure(IApplicationBuilder app)
{
app.UseErrorPage();
app.UseRequestServices();
app.UseMvc(routes => /* routes stuff */);
}
Run Code Online (Sandbox Code Playgroud)
我希望HttpContext …
c# asp.net-mvc dependency-injection asp.net-core-mvc asp.net-core
我一直在追踪Url重写应用程序的错误.该错误显示为查询字符串中某些变音字符的编码问题.
基本上,问题是基本上是/search.aspx?search=heřmánek的请求被重写了"search = he%c5%99m%c3%a1nek"的查询字符串
正确的值(使用一些不同的工作代码)是将查询字符串重写为"search = he%u0159m%u00e1nek"
注意两个字符串之间的区别.但是,如果您同时发布,则会看到Url Encoding会重现相同的字符串.直到你使用编码中断的context.Rewrite函数.断开的字符串返回'heÅmánek'(使用Request.QueryString ["Search"],工作字符串返回'heřmánek'.这个改变发生在调用重写函数之后.
我使用Request.QueryString(工作)跟踪到一组代码,另一组使用Request.Url.Query(request.Url返回一个Uri实例).
虽然我已经解决了这个问题,但我的理解还有一个漏洞,所以如果有人知道这个差异,我就准备好了.
我有一个对象列表,其中我无法知道编译时的类型.
我需要识别存在'Count'属性的任何这些对象,如果存在则获取值.
此代码适用于简单的Collection类型:
PropertyInfo countProperty = objectValue.GetType().GetProperty("Count");
if (countProperty != null)
{
int count = (int)countProperty.GetValue(objectValue, null);
}
Run Code Online (Sandbox Code Playgroud)
问题是这不适用于泛型类型,例如IDictionary<TKey,TValue>
.在这些情况下,即使实例化对象中存在"Count"属性,'countProperty'值也会返回null.
我想要做的就是识别任何基于集合/字典的对象并找到它的大小(如果有的话).
编辑:根据要求,这是完整的代码列表,不起作用
private static void GetCacheCollectionValues(ref CacheItemInfo item, object cacheItemValue)
{
try
{
//look for a count property using reflection
PropertyInfo countProperty = cacheItemValue.GetType().GetProperty("Count");
if (countProperty != null)
{
int count = (int)countProperty.GetValue(cacheItemValue, null);
item.Count = count;
}
else
{
//poke around for a 'values' property
PropertyInfo valuesProperty = cacheItemValue.GetType().GetProperty("Values");
int valuesCount = -1;
if (valuesProperty != null)
{ …
Run Code Online (Sandbox Code Playgroud)