嗨,我有这个模型
型号项目
class Inventory::Item < ActiveRecord::Base
has_many :types, :class_name => "ItemType"
attr_accessible :name
end
Run Code Online (Sandbox Code Playgroud)
模型item_type
class Inventory::ItemType < ActiveRecord::Base
belongs_to :item
attr_accessible :number
end
Run Code Online (Sandbox Code Playgroud)
然后在控制器中说我想根据项目名称按升序对类型(具有类ItemType)进行排序.我怎么做?
例如,
所以不是从数字中对它进行排序,而是希望它基于item.name(ASC)进行排序,如下所示:
我想在我的代码上使用simplePagination.(我正在使用MVC4 C#开发)
假设我有这堆代码
<table>
<thead>
<tr>
<td><input type="checkbox" name="select-all" id="select-all" /></td>
<td style="text-align: left">Name</td>
<td style="text-align: left">Created By</td>
<td style="text-align: left">Created Date</td>
</tr>
</thead>
<tbody>
<tr class="post">
<td><p><input Length="0" name="314" type="checkbox" value="true" /><input name="314" type="hidden" value="false" /></p></td>
<td>Window</td>
<td>John</td>
<td>01/01/2014 12:00:00 AM</td>
</tr>
<tr class="post">
<td><p><input Length="0" name="314" type="checkbox" value="true" /><input name="314" type="hidden" value="false" /></p></td>
<td>Door</td>
<td>Chris</td>
<td>01/01/2014 12:00:00 AM</td>
</tr>
<tr class="post">
<td><p><input Length="0" name="314" type="checkbox" value="true" /><input name="314" type="hidden" value="false" /></p></td>
<td>Floor</td>
<td>Michael</td>
<td>01/01/2014 12:00:00 AM</td> …Run Code Online (Sandbox Code Playgroud) 我研究了如何在 .NET Core 中创建 HttpContext。然后我发现有一个类HttpContextFactory,它创建对象并将其分配HttpContext给类HttpContext的属性HttpContextAccessor。为了在我们的代码中使用 HttpContext 对象,我们将 IHttpContextAccessor 注入到需要该对象的类的构造函数中。
当我查看 HttpContextAccessor 的实现时,显然它的 HttpContext 属性从私有AsyncLocal变量获取 HttpContext 对象值,后来 HttpContextAccessor 被注册为Singleton。
https://github.com/aspnet/AspNetCore/blob/master/src/Http/Http/src/HttpContextAccessor.cs
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading;
namespace Microsoft.AspNetCore.Http
{
public class HttpContextAccessor : IHttpContextAccessor
{
private static AsyncLocal<HttpContextHolder> _httpContextCurrent = new AsyncLocal<HttpContextHolder>();
public HttpContext HttpContext
{
get
{ …Run Code Online (Sandbox Code Playgroud) 在我的 .NET Core 应用程序中,我向 appsettings.json 添加了一个数组,如下所示:
{
"SettingsA": {
"PropA": [
"ChildObjectA": {
...
},
"ChildObjectB": {
...
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
如果我想从我的 azure 应用程序服务中的应用程序设置中覆盖该值,以便它具有空数组:
{
"SettingsA": {
"PropA": []
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
我试着把
SettingsA:PropsA -> []
Run Code Online (Sandbox Code Playgroud)
在应用程序设置中,但它似乎没有覆盖 appsettings.json 的值
我正在尝试通过Powershell脚本添加%JAVA_HOME%\bin到Path环境变量。
在JAVA_HOME本身指向变量C:\Program Files\Java\jdk1.8.0_172。
当我%JAVA_HOME%\bin从“环境变量”窗口手动添加时
Then call this line of code from Powershell to get value of Path variable
[Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine)
Run Code Online (Sandbox Code Playgroud)
It seems like the result from executing line above converts %JAVA_HOME%\bin to the actual path that I've defined which is C:\Program Files\Java\jdk1.8.0_172.
The output look like this
...;C:\Program Files\nodejs;C:\Program Files\Java\jdk1.8.0_172\bin;
Run Code Online (Sandbox Code Playgroud)
But when I added %JAVA_HOME%\bin via Powershell script with the code below
[Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine) + "%JAVA_HOME%\bin", [EnvironmentVariableTarget]::Machine)
Run Code Online (Sandbox Code Playgroud)
Then …
如何通过将fstream用于类似的内容来读取.txt将内容复制到另一个.txt.问题是,在文件中有新行.使用ifstream时如何检测?
用户输入"apple"
例如:note.txt =>昨天我买了一个苹果.苹果味道鲜美.
note_new.txt =>我昨天买了一个.味道鲜美.
结果笔记假设在上面,但是:note_new.txt =>我昨天买了一个.味道鲜美.
如何检查源文件中是否有新行,它还将在新文件中创建新行.
这是我目前的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inFile ("note.txt");
string word;
ofstream outFile("note_new.txt");
while(inFile >> word) {
outfile << word << " ";
}
}
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗?实际上,我还会检查检索到的单词与用户指定的单词是否相同,然后我不会在新文件中写入该单词.所以一般来说,它会删除与用户指定的单词相同的单词.
我创建了一个由Apple webhook调用的API来处理订阅.我在Itunes中设置了API网址,由webhook调用.
据苹果开发者网站https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/iTunesConnectInAppPurchase_Guide/Chapters/TestingInAppPurchases.html#//apple_ref/doc/uid/TP40013727-CH4-SW5在问候中Testing Auto-Renewable Subscription,当在测试环境中测试自动更新订阅时,如果我选择1个月的订阅持续时间,Apple webhook将在5分钟后调用API.
但问题是,在第一次购买之后,apple webhook没有跟进我创建的用于续订潜艇的API的调用.即使在我等了5分多钟之后.所以我不能从API进行任何处理来更新数据库中的用户子.
我是否需要进行一些配置以测试自动续订并使apple webhook调用API以更新子?
如何在不更改控制器操作中的任何代码的情况下检查作为OkObjectResult的一部分返回的值是否具有2的计数?
这是我的控制器动作
public IActionResult GetUserNames()
{
var users = _repository.GetUsers();
return Ok(users.Select(u => u.Name));
}
Run Code Online (Sandbox Code Playgroud)
我的单元测试看起来像这样
[Fact]
public void GetUserNames_ValidRequest_ShouldReturnOk()
{
_repository
.Setup(r => r.GetUsers())
.Return(new List<User>
{
new User { Name = "SomeRandomName" },
new User { Name = "SomeRandomName2" }
});
var result = _controller.GetUserNames();
result.Should().BeOfType<OkObjectResult>();
// Code to check if 2 names are returned
}
Run Code Online (Sandbox Code Playgroud)
我在单元测试中使用了Mock和FluentAssertions.
我有这个字符串"Date.2014.07.04"
然后如果我想使用正则表达式从上面的字符串得到"07".
我怎么做?
我不想使用拆分.
为什么我不想使用拆分?因为当我们拆分时,结果将是字符串数组.通常我们会尝试获取我们想要的数组索引.在我的情况下,它将是
var date = "Date.2014.07.04";
date.Split('.')[2];
Run Code Online (Sandbox Code Playgroud)
但是,我们假设我们将日期更新为新字符串(全部删除'.').
var date = "Date20140704";
date.Split('.')[2];
Run Code Online (Sandbox Code Playgroud)
这将抛出一个错误,因为它找不到索引号2.
通过使用正则表达式,这个错误不会发生,如果在字符串中找不到我们想要的模式,它将只返回空字符串.:)
c# ×4
asp.net-core ×3
.net-core ×1
azure ×1
c++ ×1
file-io ×1
fstream ×1
html ×1
httpcontext ×1
ios ×1
iphone ×1
javascript ×1
jquery ×1
powershell ×1
regex ×1
rest ×1
sorting ×1
subscription ×1
unit-testing ×1
view ×1
windows ×1