我需要在asp.net mvc 3中进行可变长度路由映射.根据我的理解,我可以映射这样的路由:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
Run Code Online (Sandbox Code Playgroud)
但是如何添加可变数量的参数.我的应用程序中的路由具有从基本地址开始的目录结构,如下所示:www.mywebsite.com/root/
root后面跟的东西有一个像这样的目录结构:
www.mywebsite.com/root/directory1/directory2/image.jpg
这将对应于这样的映射:
routes.MapRoute(
"Default", // Route name
"root/{path}", // URL with parameters
new { controller = "DirectoryBrowser", action = "Browse" } );
Run Code Online (Sandbox Code Playgroud)
但我不知道如何设置路径参数.
我使用假的很容易为我的对象生成假货.现在我测试假对象的某些方法是否被调用.我希望每个单元测试都能将伪造的状态重置回原始状态.
我需要这个,因为我为每个单元测试类生成一次假动作,所以我需要以某种方式重置调用状态.
这是我的代码的样子:
[Test]
public void Test_Process_SampleMessageNEW_Should_Call_ISampleMessageConverter_GetRouteObject()
{
//Arrange
//should reset the state of the fakes, _sampleRouteConvertor and _orderDalcMock are fakes
_sampleRouteConvertor.BackToRecord();
_orderDalcMock.BackToRecord();
A.CallTo(() => _orderDalcMock.GetOrder(A<IRQOrderIDCriteria>.Ignored))
.Returns(new OrderDTO() {Id = nextId++});
SampleMessage sampleMessage = SampleMessageTestHelpers.GetSavedSampleMessage();
sampleMessage.MaterialId = CacheMaterials.GetMaterials()[0].Code;
sampleMessage.MessageType = "NEW";
//Act
SampleMessageProcessor sampleMessageProcessor = new SampleMessageProcessor(sampleMessage);
sampleMessageProcessor.Process();
//Assert
A.CallTo(() => _sampleRouteConvertor.GetRouteObject(A<RoutineSample>.Ignored, A<List<ICacheTrackArea>>.Ignored))
.MustHaveHappened(Repeated.Exactly.Once);
}
Run Code Online (Sandbox Code Playgroud) 我是具有相同类型对象的列表列表,我正在尝试将其转换为使用linq的所有对象的单个列表.
我怎样才能做到这一点?
这是我目前的代码:
var allTrackAreas =
_routables.Select(
routable =>
_centrifugeHelper.GetTrackAreasFromRoutable(routable, _trackAreaCutterParameters,
_allowedDestination.MatchedAreas[routable]))
.Where(area => area != null).ToList();
foreach (var testAction in _trackAreaCutterParameters.ConcurrentBagTestActions)
{
if (allTrackAreas.Any(areas => areas.Any(area => area.Id == testAction.TrackAreaId)))
{
currentActions.Add(testAction);
}
}
Run Code Online (Sandbox Code Playgroud)
变量allTrackAreas是一个列表列表,我正在使用两次Any,它对效率非常不利.如果它是一个简单的列表会好得多.
我试图使用Linq计算可空int的总和.我的查询如下所示:
int? surveysPromotersFromChatsGroup = (from surveyResult in surveyResultsFromChats
where surveyResult.FirstScoreNPS.HasValue && surveyResult.GroupID == gr && surveyResult.FirstScoreNPS >= 9
select surveyResult.FirstScoreNPS).Sum();
Run Code Online (Sandbox Code Playgroud)
FirstScoreNPs字段是可空的int.我的问题是总和无论如何总是返回null.我看不出这背后的逻辑.我检查过,我肯定知道某些实体确实在该字段中有值,而sum方法应该总结这些值.
我尝试的另一种方法是这样的:
int surveysPromotersFromChatsGroup = (from surveyResult in surveyResultsFromChats
where surveyResult.FirstScoreNPS.HasValue && surveyResult.GroupID == gr && surveyResult.FirstScoreNPS >= 9
select surveyResult.FirstScoreNPS).Sum(score => score.Value);
Run Code Online (Sandbox Code Playgroud)
但我得到一个例外"linq无法将null值赋给具有类型的成员".
有没有办法使用linq总结该字段的非空值?
surveyResultsFromChat实际上来自另一个查询:
var surveyResultsFromChats = from surveyResult in db.SurveyResults
join survey in db.Surveys on surveyResult.SurveyID equals survey.SurveyID
join userGroup in db.UserGroupMaps on surveyResult.UserID equals userGroup.UserID
where surveyResult.CreatedDate >= startDate && surveyResult.CreatedDate <= endDate && surveyResult.UserID.HasValue && …Run Code Online (Sandbox Code Playgroud) 我正在尝试为Windows Phone或Windows 8开发一个C#xaml游戏.我主要担心的是围绕DirectX创建包装器.
根据我的理解,我必须使用C++/Cx来创建这些包装器.我想在c#中创建一个接口,然后在C++/Cx中实现它,然后在C#中引用包装类并通过我声明的接口使用它.这可能吗?
此外,这种实现不会影响性能吗?
我在一个普通的 .net 项目中使用实体框架核心,版本 4.7。我知道我可以做到这一点。问题是我似乎无法将实体映射到表,因为“ToTable”方法不存在。我无法编辑 poco 或实体类,因为它们是预定义和生成的。所以我不能使用该属性。我在互联网上查看,每个人似乎都使用这种方法将实体映射到表。
这是我的代码:
public class FactsDbContext : DbContext
{
public DbSet<TblIncident> TblIncidents { get; set; }
public DbSet<TblAction> TblActions { get; set; }
public DbSet<TblAddressTypeAlias> TblAddressTypeAliases { get; set; }
public DbSet<TblCountry> TblCountries { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//these methods don't exist in my case
modelBuilder.Entity<TblIncident>(entity => entity.ToTable("Incident"));
modelBuilder.Entity<TblIncident>().ToTable("Incident");
}
}
Run Code Online (Sandbox Code Playgroud)
我还尝试将 IEntityTypeConfiguration 与 EntityTypeBuilder 一起使用,但它仍然无法访问映射到表方法:
public class IncidentConfig : IEntityTypeConfiguration<TblIncident>
{
public void Configure(EntityTypeBuilder<TblIncident> builder)
{
builder.ToTable("Incident");
}
}
Run Code Online (Sandbox Code Playgroud) 我一直想知道在请求与基于配置的路由的操作匹配后,如何在 Asp .Net Core 中调用控制器操作。
它们是通过反射调用的吗?如果以这种方式调用它们,将会对性能产生相当大的影响。
或者路由和相应的控制器是否以某种方式动态编译,以便直接调用控制器的操作。