小编Key*_*iya的帖子

如何在.NET Framework 4.6.1中使用IActionResult?

在过去的几个小时里,我一直在寻找答案,没有任何成功.我对编程比较陌生,所以请放轻松我:-)

我的问题是,我的解决方案中的一个项目似乎无法使用IActionResult,该解决方案的目标是.NET Framework 4.6.1,但在针对.NET Standard 2.0的相同解决方案(不同项目)中我可以使用它.

我试图安装完全相同的Nuget包,除了目标框架外,一切看起来完全一样.

我只想使用IActionResults编写Azure Functions,如:

 public static async Task<IActionResult> MyFuntcion([HttpTrigger(AuthorizationLevel.Function, "get", Route = null")]HttpRequest req, TraceWriter log)
    {

      //Do some stuff then redirect

    }
Run Code Online (Sandbox Code Playgroud)

我猜我的目标框架是什么导致我无法使用IActionResult,但我不明白为什么.

任何帮助都将非常感激,特别是如果有人知道如何使这项工作.

谢谢

.net c# asp.net azure

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

模板模板参数导致Clang下的编译器错误,但不会导致GCC

在帮助解决模板模板参数太多模板参数中出现的问题时,我脑子里出现了一个问题:在这种情况下,哪个编译器对编译是正确的:

template <template <typename, typename> class Op>
class Function
{
};

template <typename A, typename B, bool is_f = std::is_floating_point<A>::value || std::is_floating_point<B>::value > struct Operator;

template <typename A, typename B>
struct Operator<A, B, false>
{};


template <typename A, typename B>
struct Operator<A, B, true>
{};

using FunctionOperator = Function<Operator>;


int main(int argc, char * argv[]){
    std::cout << "hi!\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

GCC 7+编译它没有错误.Clang 6及更高版本给出错误,表明Operator模板作为模板参数传递时出现问题:

tmp.cpp:19:35: error: template argument has different template parameters than its …
Run Code Online (Sandbox Code Playgroud)

c++ templates language-lawyer

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

iframe 中的 Safari 和 cookie

Safari 浏览器默认阻止第 3 方 cookie。

我有站点 A(主站点),它在 iframe 中加载站点 B(框架站点)。站点 B 设置一些 cookie(当用户单击按钮时),以便正常运行。这些 cookie 称为第 3 方 cookie,因为它们不是由站点 A 设置的。

所以我找到了这个解决方案:https ://github.com/vitr/safari-cookie-in-iframe

但就我而言,它不起作用,因为要设置 cookie,用户首先必须单击按钮。

Framed.html 实际上是这样的:

<!DOCTYPE html>
<html>
<head>
    <title>Framed Page</title>
</head>
<body style="background-color: lightcoral;">
<h2>This is the framed site</h2>

<div border="0" class="cookie_button" id="continue_button" onclick="SetCookie('cookieAcceptanceCookie','accepted',9999);">accept</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的问题有解决办法吗?

javascript cookies

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

在ASP.NET核心模型中使用Required和JsonRequired与JSON主体绑定

我正在使用ASP.NET Core 2.0,我有一个请求对象注释如下:

public class MyRequest
{
    [Required]
    public Guid Id { get; set; }

    [Required]
    public DateTime EndDateTimeUtc { get; set; }

    [Required]
    public DateTime StartDateTimeUtc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中:

public async Task<IActionResult> HandleRequest([FromBody] MyRequest request)
{ /* ... */ }
Run Code Online (Sandbox Code Playgroud)

我注意到一个问题与模型绑定:当我发送包含该头的请求Content-Type设置为application/json和一个空的身体,我想到,request在我的控制器是nullModelState.IsValidfalse.

但是当我有这样的身体时:

{
  "hello": "a-string-value!"
}
Run Code Online (Sandbox Code Playgroud)

request不是null,它有一切的默认值,而且ModelState.IsValidtrue

这当然发生在我缺少所有Required属性的时候,并且唯一存在的名称与那里的属性不匹配(即使是这个单个参数的类型string,也与我的模型上的任何类型都不匹配).

所以在某种程度上,Required如果我的请求中没有任何内容,那些属性似乎正在起作用,但如果我的请求不为空,它们就不会做任何事情!

当我准备这个问题时,我注意到还有一个JsonRequired …

c# model-binding json.net asp.net-core-2.0

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

如何在类图VS2017中获得自动关系?

我需要为我的 .net 核心项目创建一个 UML 类图。如果我转到类视图 - 查看类图,我会得到我所有的类,但没有关系,手工完成它们是很多工作。如何自动完成?

在此处输入图片说明

.net c# diagram class visual-studio-2017

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

Entity Framework Core 2.1,使用代码优先迁移重命名表,而无需删除和创建新表

我正在将netcoreapp 2.1与EF Core 2.1一起使用,并通过数据迁移来更新数据库,但是重命名表时遇到了问题。我在这里的主要问题是一个表(以及以后可能的列)可能会多次使用数据重命名,并且我希望能够重命名它们,同时保持数据完整,但据我看来,这些迁移似乎只涉及保持最新的架构。

此问题类似于使用Entity Framework Core 2.0更改或重命名列名而不会丢失数据,但是由于我的过程是自动化的,因此我需要能够使用dotnet.exe在命令行上使用迁移本身来做到这一点。

为此,我将下面的参数传递给dotnet.exe,构建解决方案,从DLL获取数据库上下文,然后使用下面的代码行进行迁移。

ef migrations add "someMigrationName"
Run Code Online (Sandbox Code Playgroud)

...并更新数据库

var migrator = dbContext.Database.GetService<IMigrator>();
migrator.Migrate();
Run Code Online (Sandbox Code Playgroud)

例如,如果名为“ Courases”的表开始收集数据,我需要能够将其重命名为“ Courses”,而不会影响数据,但是当前,以下是迁移中生成的Up函数。

protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "Courases");

        migrationBuilder.CreateTable(
            name: "Courses",
            columns: table => new
            {
                Id = table.Column<Guid>(nullable: false),
                ConcurrencyCheck = table.Column<byte[]>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Courses", x => x.Id);
            });

    }
Run Code Online (Sandbox Code Playgroud)

根据我的阅读,似乎没有办法用重命名的表而不是删除并重新创建表来生成迁移,但这似乎很疯狂,有没有办法做到这一点/是否有可以传递给dotnet.exe的标志,错过了吗?

c# ef-migrations .net-core ef-core-2.1

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

使用 JavaScript 进行递归回文检查

我试图通过使用 javascript 的递归来找出字符串是否是回文。但我无法弄清楚代码中缺少什么。

var firstCharacter = function(str) {
    return str.slice(0, 1);
};

var lastCharacter = function(str) {
    return str.slice(-1);
};

var middleCharacters = function(str) {
    return str.slice(1, -1);
};

var isPalindrome = function(str) {
    if(str.length < 2) {
        return true;
    } else {
        if(firstCharacter(str) == lastCharacter(str)) {
            isPalindrome(middleCharacters(str));
        } else return false;
    }
};

var checkPalindrome = function(str) {
    console.log("Is this word a palindrome? " + str);
    console.log(isPalindrome(str));
};


checkPalindrome("a");
//Program.assertEqual(isPalindrome("a"), true);
checkPalindrome("matom");
//Program.assertEqual(isPalindrome("motor"), false);
checkPalindrome("rotor");
//Program.assertEqual(isPalindrome("rotor"), true);
Run Code Online (Sandbox Code Playgroud)

递归调用肯定有问题。我很想得到你的帮助。谢谢。我附上了我的代码的输出。

在此处输入图片说明

javascript recursion palindrome

5
推荐指数
3
解决办法
5260
查看次数

使用依赖注入替换ASP.NET Core中的JWT承载选项

在Web API中,我使用Jwt Auth进行保护,我具有以下内容ConfigureServices Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(this.Configuration.GetConnectionString("DefaultConnection")));
    // Some additional application dependencies here with AddTransient()...
    services.AddMvc();
    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
           options.Authority = "xxxxx";
           options.RequireHttpsMetadata = false;
           options.Audience = "xxxxx";
           options.BackchannelHttpHandler = BackChannelHandler;
        });
}
Run Code Online (Sandbox Code Playgroud)

BackChannelHandler是的公共财产Startup.cs

public static HttpMessageHandler BackChannelHandler { get; set; }
Run Code Online (Sandbox Code Playgroud)

我使用此属性的原因是,当我使用xUnit进行集成测试时,我正在使用TestServerfrom 中的内存Microsoft.AspNetCore.TestHost。所以,我需要在TestFixture类中注册反向通道处理程序

testIdentityServer = new TestServer(identityServerBuilder);
My.Project.Startup.BackChannelHandler = testIdentityServer.CreateHandler();
testApiServer = new TestServer(apiServerBuilder);
Run Code Online (Sandbox Code Playgroud)

尽管此方法运行良好,但我想覆盖DI容器中的services.AddAuthentication()AddJwtBearer(),因此我可以出于测试目的注入身份验证中间件配置,而不是像使用其他依赖项那样使用公共静态属性。当我尝试使用以下方法进行操作时:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options …
Run Code Online (Sandbox Code Playgroud)

c# integration-testing dependency-injection identityserver4 asp.net-core-2.0

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

C#不可变HashSet锁定

假设存储连接数据的ImmutableHashSet的服务器

ImmutableHashSet<ConnectionData> connections = new ...
Run Code Online (Sandbox Code Playgroud)

然后我有各种调用添加/删除/读取,即:

OnConnected(connectionData) => connections = connections.Add(connectionData);
OnDisconnected(connectionData) => connections = connections.Remove(connectionData);
Run Code Online (Sandbox Code Playgroud)

我的问题是,在上面只对HashSet(Add/Remove)执行单个操作的调用中,我应该锁定连接吗?或者是ImmutableHashSet操作线程安全吗?

c# c#-7.0

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

dotnet 核心应用程序,如何使用 appsettings 文件构建到输出文件夹

我有这个错误:

Unhandled Exception: System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The physical path is '/ONEDRIVE/OneDrive - SE/LuloTV/Azure Search/DemoSearchIndexer/bin/Debug/netcoreapp2.2/appsettings.json'.
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
   at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at DemoSearchIndexer.Program.Main(String[] args) in /ONEDRIVE/OneDrive - SE/LuloTV/Azure Search/DemoSearchIndexer/Program.cs:line 16
Run Code Online (Sandbox Code Playgroud)

JSON 文件存在于源代码目录中,但是,它看起来需要在二进制文件夹的某处找到该文件。

在使用 dotnet run 运行应用程序之前,我执行了 dotnet build,假设它将 JSON 文件复制到输出构建文件夹,但它没有。

在此处输入图片说明

代码是这样的:

using System;
using System.Linq;
using System.Threading;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Spatial;

namespace DemoSearchIndexer
{
    class Program
    {
        static void Main(string[] args)
        {
            IConfigurationBuilder builder = new …
Run Code Online (Sandbox Code Playgroud)

.net c# .net-core

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