小编Hyp*_*ate的帖子

突出显示搜索文本 - 角度2

信使根据用户提供的输入显示搜索结果.在显示结果时,需要突出显示已搜索的单词.这些是使用的html和组件.

Component.html

 <div *ngFor = "let result of resultArray">
<div>Id : result.id </div>
<div>Summary : result.summary </div>
<div> Link : result.link </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Component.ts

resultArray : any = [{"id":"1","summary":"These are the results for the searched text","link":"http://www.example.com"}]
Run Code Online (Sandbox Code Playgroud)

通过发送搜索文本作为输入来获取此resultArray来命中后端服务.根据搜索文本,获取结果.需要突出显示搜索到的文字,类似于谷歌搜索.请找截图,

在此输入图像描述

如果我搜索单词"member",则会突出显示单词"member"的出现.如何使用角度2实现相同.请提出一个想法.

javascript html5 typescript angular

21
推荐指数
5
解决办法
2万
查看次数

单元测试控制器模拟 ISession

我想对我的控制器进行单元测试,但我在读取值时遇到问题HttpContext.Session
我想从我的控制器中模拟这一部分:HttpContext.Session.Get<int>(Foo)

使用Mock HttpContext 对 .NET core MVC 控制器进行单元测试?以及 如何在 asp net core 中模拟会话对象我能够得出以下解决方案:

我的测试:

public void GetFoos_AllGood_ReturnList()
{
  //Arrange
  Mock<ISession> sessionMock = new Mock<ISession>();
  var fooSessionValue = new byte[0];

  Web.Controllers.FooController fooController = new Web.Controllers.FooController();
  fooController.ControllerContext.HttpContext = new DefaultHttpContext();
  fooController.ControllerContext.HttpContext.Request.Headers["Foo"] = 0;

  Mock<IServiceCollection> mock = new Mock<IServiceCollection>();
  mock.Object.AddSession(); // Tried this, but failed

  //Setup
  sessionMock.Setup(_ => _.Set("Foo", It.IsAny<byte[]>())).Callback<string, byte[]>((k, v) => fooSessionValue = v);
  sessionMock.Setup(_ => _.TryGetValue("Foo", out fooSessionValue)).Returns(true);
  mockWebDataManager.Setup(b => b.GetFoos(It.IsAny<FooArgs>())).Returns(new FooResult() { …
Run Code Online (Sandbox Code Playgroud)

c# moq mocking xunit asp.net-core

7
推荐指数
1
解决办法
3187
查看次数

使用多个连接字符串

信息
我的解决方案中有多个项目,其中一个是DAL,另一个是ASP.NET MVC6项目.由于MVC6项目也是启动项目,我需要在那里添加我的连接字符串.

我看到了这个解决方案,但它不被接受,也没有用.

我的尝试
appsettings.json

"Data": {
  "DefaultConnection": {
    "ConnectionString": "Server=.\\SQLEXPRESS;Database=Bar;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "FooBar": {
    "ConnectionString": "Server=.\\SQLEXPRESS;Database=Bar;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}
Run Code Online (Sandbox Code Playgroud)

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
             .AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration["Data:FooBar:ConnectionString"]));
}
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试使用FooBar连接字符串访问数据时,我收到以下消息:

"附加信息:在应用程序配置文件中找不到名为'FooBar'的连接字符串."

问题
如何让多个连接字符串工作?

c# json connection-string asp.net-core-mvc asp.net-core

6
推荐指数
1
解决办法
6946
查看次数

闲置财产掉了?

首先,我的班级:

export class FooBar {
 ...
 isFavorite: boolean = false;

 constructor() {
   this.isFavorite = false;
  }
}
Run Code Online (Sandbox Code Playgroud)

使用 Lodash 我对 的列表进行排序FooBar,因此我最喜欢的将在列表的顶部:

this.fooBars = _.orderBy(this.fooBars, ['isFavorite', 'name'], ['desc', 'asc']);
Run Code Online (Sandbox Code Playgroud)

当我收藏一个项目并查看我的 console.log 时,它说明了这一点:

isFavorite 为 false 时不显示

请注意,#3 没有 isFavorite 属性...

每当我从未设置时,isFavorite它都不会显示。这使得 Lodash 排序错误。
有没有办法始终显示此属性,即使它未使用/未设置/错误?

我试过:
- 在类
中将属性设置为 false
-在类的构造函数中将属性设置为 false -this.foobars在我的组件中循环,将它们全部设置为 false
- 添加一个接口到FooBar

javascript json typescript lodash angular

5
推荐指数
1
解决办法
170
查看次数

去抖动异步验证器

我有一个工作异步验证器,它向服务器发出 HTTP 请求以检查用户名是否已被占用。因为我不想在每次击键后调用 API,所以我需要对输入流进行去抖动。

我第一次参加throttleTime了这项服务,但是关于 SO 的另一个主题说这必须是订阅者,但还没有运气!

我的组件:

this.form = this._fb.group(
      {
        username: ['', [Validators.required, Validators.maxLength(50), NoWhitespaceValidator], [IsUserIdFreeValidator.createValidator(this._managementService)]]
      });
Run Code Online (Sandbox Code Playgroud)

我的验证器:

export class IsUserIdFreeValidator {
  static createValidator(_managementService: ManagementService) {
    return (control: AbstractControl) => {
      return _managementService.isUserIdFree(control.value)
        .pipe(
          throttleTime(5000),
          (map(
            (result: boolean) => result === false ? { isUserIdFree: true } : null))
        );
    };
  }
}
Run Code Online (Sandbox Code Playgroud)

我的服务:

  public isUserIdFree(userId: string): Observable<{} | boolean | HttpError> {
    const updateUserCheck: UpdateUserCheck = new UpdateUserCheck();
    updateUserCheck.userID = userId;

    return this._httpClient.post<boolean>('UserManagementUser/IsUserIdFree', updateUserCheck));
  }
Run Code Online (Sandbox Code Playgroud)

rxjs typescript angular-validation angular

5
推荐指数
1
解决办法
2292
查看次数

将字符串从 Angular 传递到 API

我想通过 Angular 在我的 ASP.NET Core MVC API 控制器中填充一个字符串参数。

我有这个工作电话:

应用程序接口

//A class to wrap my string
public class Foo
{
  public string bar { get; set; }
}

[HttpPost]
public JsonResult GetDetails([FromBody] Foo bar) { ... }
Run Code Online (Sandbox Code Playgroud)

角度(服务)

public get() {
  let bar: any = new Object();
  bar.foo = 'Hello World';

  return this._httpClient.post('api/GetDetails', bar).toPromise();
}
Run Code Online (Sandbox Code Playgroud)

但我真正想要的是传递一个字符串而不必将它包装在这样的类中:

应用程序接口

[HttpPost]
public JsonResult GetDetails([FromBody] string bar) { ... }
Run Code Online (Sandbox Code Playgroud)

角度(服务)

public get() {
let bar: string = "Hello World";

return this._httpClient.post('api/GetDetails', bar).toPromise();
} …
Run Code Online (Sandbox Code Playgroud)

javascript asp.net-mvc json typescript angular

3
推荐指数
1
解决办法
3820
查看次数

用于检查 ModelState.IsValid 的 ActionFilter 永远不会被命中

我编写了一个 ActionFilter 来检查我的模型状态是否有效(这样我就可以记录问题):

public class ValidationFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    { ... }
}
Run Code Online (Sandbox Code Playgroud)

我的断点从未被命中,我在 Postman 中得到了输出:

{ "errors": {
"RequiredText": [
"Test - requiredText is required"
]
},
"title": "发生一个或多个验证错误。",
"status": 400,
"traceId": "0HLOBA7E4R7SL:00000002"
}

我假设 .NET Core 预先进行了验证。
有没有办法关闭此功能,以便使用我的属性?

c# .net-core asp.net-core asp.net-core-webapi

1
推荐指数
1
解决办法
673
查看次数