我决定将我的 ASP.NET MVC 5 应用程序之一迁移到 ASP.NET Core 2,但我希望两个版本都能正常工作。我已经将所有类库迁移到 .NET Standard(以便我可以在两个项目中使用它们)并希望使用 Microsoft.Extensions.Configuration 标准库来管理应用程序设置。
虽然在 ASP.NET Core 2.0 中使用这个配置库不是问题,但我找不到在 ASP.NET MVC 5 应用程序中正确加载 web.config 文件的方法。这是我当前用于加载 web.config 文件的代码:
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddXmlFile("web.config");
_configuration = configurationBuilder.Build();
Run Code Online (Sandbox Code Playgroud)
但是,每次运行该应用程序时,都会收到异常:System.FormatException: A duplicate key 'appSettings:add:key' was found'appSettings:add:key'. 我仔细检查了文件,没有重复的密钥,所以我猜这是兼容性问题。有没有人在 ASP.NET MVC 5 中使用 MS 配置库,并找到了一种正确使用 stadnard web.config 文件的方法?
我正在使用Angular-Messages与Angular-Material v1.0.9.我有一个md-dialog内部,我有一个表单与字段创建ng-repeat:
<form name="modal.dynamicForm" flex="80" layout="column">
<md-input-container ng-repeat="field in modal.model.fields">
<ng-form name="innerForm">
<label>{{ field.label }}*</label>
<input type="text" ng-model="field.value" name="{{ field.name }}" ng-pattern="field.pattern" required />
<div ng-messages="innerForm[field.name].$error">
<div ng-message="required">{{ 'fieldRequired' | translate }}</div>
<div ng-message="pattern">{{ field.patternError }}</div>
</div>
</ng-form>
</md-input-container>
</form>
Run Code Online (Sandbox Code Playgroud)
Angular-Messages用于显示表单错误.问题是,当我同时使用required和pattern消息时,它们有时会显示,有时则不显示.没有正则表达式检查时它正常工作.该场景例如:
required应该显示错误,但事实并非如此.但是,有一个红色边框通知错误.上面的场景可以重复并且始终为真 - 我需要在再次显示错误之前输入正确的值.
我找到了一个解决方法,即添加md-auto-hide到容器中ng-messages,但现在错误显示在打开模式窗口之后,这不是我想要的.有没有人遇到类似的问题,并找到任何解决方案?我会感激任何帮助.
编辑
我创造了一个提出问题的小提琴.
我正在尝试为服务层编写单元测试(使用NUnit),该服务层使用:
我还使用Effort.EF6在单元测试中模拟DbContext.不幸的是,我找不到让DbContextScope与Effort兼容的方法,以便我可以正确测试所有案例.
代码概述
服务层由执行某些业务逻辑的类(服务)组成.每个方法都被视为一个完整的事务,以context.SaveChanges().结尾.例:
private IDbContextScopeFactory _dbContextScopeFactory;
public DepartmentsService(IDbContextScopeFactory dbContextScopeFactory)
{
_dbContextScopeFactory = dbContextScopeFactory;
}
public BusinessModel.Department Insert(BusinessModel.Department department)
{
using (var dbContextScope = _dbContextScopeFactory.Create())
{
// Validation
ValidateAndThrowOnFailure(department, new DepartmentAddValidator());
// Operation
DBModel.Department newDepartment = Mapper.Map<DBModel.Department>(department);
newDepartment.InsertDateUTC = DateTime.UtcNow;
dbContextScope.DbContexts.Get<DPSContext>().Departments.Add(newDepartment);
dbContextScope.SaveChanges();
return Mapper.Map<BusinessModel.Department>(newDepartment);
}
}
Run Code Online (Sandbox Code Playgroud)
为了对这种方法进行单元测试,我在每次测试前做了一些准备:
private IDepartmentsService _departmentsService;
private IDbContextScopeFactory _dbContextScopeFactory;
private IDbContextFactory _dbContextFactory;
private DBModel.DPSContext _dbEntities;
[SetUp]
public void ReInitializeTest()
{
// Setup DbContext with Effort.EF6
string connStr = ConfigurationManager.ConnectionStrings["DPSContext"].ConnectionString; …Run Code Online (Sandbox Code Playgroud) 介绍
我正在使用 Enity Framework 创建一个 ASP.NET WebAPI 应用程序。我需要做的是根据用户角色为一个 URI 返回相同资源的不同表示。例如,api/employees/1将为管理员和标准用户返回两个不同的对象:
标准用户
public class EmployeeBasic {
public string FirstName { get; set; }
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
行政
public class EmployeeExtended : EmployeeBasic {
public decimal Salary { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
想法和尝试
对于每个资源表示,我需要提供一些相关的类,例如排序模型。我想知道是否可以使用泛型类型和继承来为相关表示创建泛型存储库方法。我想到了以下方法:
1)为排序模型创建一些基本接口:
public interface ISortModel<out TBusinessEntity> {
}
Run Code Online (Sandbox Code Playgroud)
2) 创建通用 SortModel 作为所有排序模型的基本类型
public abstract class SortModel<TDBEntity, TBusinessEntity> : ISortModel<TBusinessEntity>
{
// Database sorting
public abstract IQueryable<TDBEntity> ApplyToQuery(IQueryable<TDBEntity> query);
// Local sorting
public …Run Code Online (Sandbox Code Playgroud) c# ×3
angularjs ×1
asp.net ×1
asp.net-mvc ×1
dbcontext ×1
effort ×1
javascript ×1
unit-testing ×1