我正在构建 .Net 5 Web API,并且仅当我在本地(也称为本地主机)运行时才会遇到 CORS 问题。当我将应用程序部署到 Azure 后,我可以从 Blazor 应用程序正常访问我的 API。
类Startup.cs
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
Run Code Online (Sandbox Code Playgroud)
类:Startup.cs
方法:ConfigureServices
services.AddCors(options =>
{
//didn't work
//options.AddDefaultPolicy(builder =>
//{
// builder.SetIsOriginAllowed(origin => new Uri(origin).Host == "localhost");
//});
options.AddPolicy(name: MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins("https://localhost:44373/", "https://myawesomesite")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
Run Code Online (Sandbox Code Playgroud)
类:Startup.cs
方法:Configure
app.UseCors(MyAllowSpecificOrigins);
Run Code Online (Sandbox Code Playgroud) 当我在传递查询字符串时向 API 端点发送请求时,我遇到参数未绑定的问题,如果我展平提交,则参数会绑定,但这不是我想要的解决方案。我无法提供在第三方控件处理请求时发送请求的客户端代码。
我提供了随请求发送的查询字符串的示例,可以在开发人员工具中看到该示例,但是 Person 和 Property 返回为 null。有谁有任何建议,特别是我可以做的服务器端更改来绑定这个复杂的对象?谢谢。
public class Submission {
public Person Person {get; set; }
public Property Property { get; set; }
}
public class Person {
public int Age { get; set; }
}
public class Property {
public string Address { get; set; }
}
Query string parameters
'Person[Age]': 18
'Property[Address]': test
[HttpGet("action")]
public async Task<IActionResult> Submit([FromQuery] Submission model)
{
}
Run Code Online (Sandbox Code Playgroud) 这是我的启动代码:
services.Configure<FooBarOptions>(Configuration.GetSection("FooBarOptions"));
Run Code Online (Sandbox Code Playgroud)
这是一个错误,正如我想要的
services.Configure<FooBarOptions>(Configuration.GetSection("FooBar"));
Run Code Online (Sandbox Code Playgroud)
显然,拼写错误导致配置无法加载。当所需的配置部分不存在时,有没有办法在启动过程中触发错误?
我有一个自定义且有些复杂的 SQL 查询,我需要通过 EF Core DbContext 执行该查询。
我意识到下面的示例代码可以使用简单的 .Select() 来解决,但不幸的是我的实际表格是通过魔术键连接的并且没有导航属性。
无论如何,我可以在不选择导航属性的情况下实现以下功能吗?
namespace ClassLibrary1
{
public class MyService
{
private readonly MyContext _context;
public void MyMethod()
{
var mySql = @"SELECT Foo.FooName, Bar.BarValue FROM Foo JOIN Bar ON Foo.Key = Bar.Key";
List<Dto> dtoList = _context.Database.MethodImLookingFor<Dto>(mySql); // Any ideas?
}
}
public class MyContext : DbContext
{
public DbSet<Foo> Foo { get; set; }
public DbSet<Bar> Bar { get; set; }
}
// My models are only records for brevity.
public record …Run Code Online (Sandbox Code Playgroud) c# sql-server entity-framework entity-framework-core .net-core