我将我的项目从 ASP.NET Core 2.2 迁移到 ASP.NET Core 3.0。现在我得到了这个例外。在 ASP.NET Core 2.2 中,它使用FromSql(); 现在它正在使用FromSqlRaw(). 我正在使用 Entity Framework Core 调用我的过程。
SqlParameter Username = new SqlParameter
{
ParameterName = "USERNAME",
SqlDbType = SqlDbType.NVarChar,
Value = user.Username,
Direction = ParameterDirection.Input,
Size = 50
};
SqlParameter Password = new SqlParameter
{
ParameterName = "PASSWORD",
SqlDbType = SqlDbType.NVarChar,
Value = user.Password,
Direction = ParameterDirection.Input,
Size = 50
};
SqlParameter msgOut = new SqlParameter
{
ParameterName = "MSG",
SqlDbType = SqlDbType.NVarChar,
Direction = …Run Code Online (Sandbox Code Playgroud) 我正在使用一个返回 JSON 的 API,其中它的值之一可以是 或false一个对象。为了解决这个问题,我创建了一个自定义JsonConverter<T>.
internal class JsonFalseOrObjectConverter<T> : JsonConverter<T> where T : class
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.False)
{
return null;
}
else
{
return JsonSerializer.Deserialize<T>(ref reader);
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我收到以下编译器错误:
可能返回空引用。
我可以将返回类型设置为 aT?但随后我会收到错误:
返回类型中引用类型的可为空性与重写的成员不匹配。
我该如何解决这个问题?
我已将实体映射到数据库视图(用于查询数据)以及表(用于插入、更新数据)。
要将实体映射到数据库视图,我使用以下代码(来自 EF 5.0 文档):
modelBuilder
.Entity<Blog>()
.ToTable("Blogs")
.ToView("BlogsView");
Run Code Online (Sandbox Code Playgroud)
在数据库视图中,我有由数据库计算的附加列(它们不存在于表中)。我在每个选择查询中都需要这些附加列,但在保存和更新时需要忽略它们。
数据[NotMapped]注释不是解决方案,因为它忽略所有命令(选择、更新等)的属性。但是,当我省略[NotMapped]数据注释时,EF 会在插入时引发错误,因为 EF 尝试将值插入到表中不存在的列中。
该[DatabaseGenerated(DatabaseGeneratedOption.Computed)]属性也不是解决方案,因为在 SQL 插入语句中,EF 生成一个从表而不是数据库视图中选择列的查询。
是否有任何解决方案可以使用 Fluent API 实现此目的,而无需将不同的实体单独映射到视图(带有附加列)和表(没有附加列)?
我有一个对象列表,其中又包含其他对象的嵌套列表。我想将对象图展平为DataTable.
我找到了接受对象集合并将它们映射到 a DataTable(下面引用)的代码,但它假设属性是可以可靠地转换为字符串值的简单类型。
我认为这只能通过递归来实现,但也许有更好的方法来做到这一点。
想象一下我们有一个ListofCustomer对象:
public class Item
{
public string SKU { get; set; }
public string Description { get; set; }
public double Price { get; set; }
}
public class Order
{
public string ID { get; set; }
public List<Item> Items { get; set; }
}
public class Customer
{
public string Name { get; set; }
public string Email { get; set; }
public List<Order> Orders …Run Code Online (Sandbox Code Playgroud) 我能够成功对 Azure 应用服务执行 Web 部署,但随后找不到 Web 应用程序。
我部署了一个在本地运行良好的 ASP.NET Core 2.2 Web 应用程序。在门户网站上,没有任何问题显示出来。当我通过例如调用它时,
https://xxxxxxxxx.azurewebsites.net/api/values
Run Code Online (Sandbox Code Playgroud)
我收到错误:
您正在查找的资源已被删除、更名或暂时不可用。(404)
从Visual Studio的"Web API"项目模板开始,我试图将自定义声明添加到/Api/Account/ExternalLogin端点创建的令牌.我通过FacebookAuthenticationProvider.OnAuthenticated回调添加这些,但它们不会持续到OAuthAuthorizationServerProvider.AuthorizationEndpointResponse().
注意:我使用Rahul Nath在他的文章ASP.NET Web API和外部登录中记录的类似方法- 使用社交网络进行身份验证
在我的Startup.Auth.cs类的ConfigureAuth()方法(从OwinStartup类的Configuration()方法调用)中,我向OnAuthenticated属性添加了一个回调函数,以便设置单个声明,foo其值为bar:
var facebookAuthenticationProvider = new FacebookAuthenticationProvider() {
OnAuthenticated = (context) => {
context.Identity.AddClaim(new Claim("foo", "bar"));
return Task.FromResult(0);
}
};
Run Code Online (Sandbox Code Playgroud)
然后我将FacebookAuthenticationProvider实例添加到一个新FacebookAuthenticationOptions对象:
var facebookAuthenticationOptions = new FacebookAuthenticationOptions() {
AppId = "XXXX",
AppSecret = "YYYY",
Provider = facebookAuthenticationProvider
};
Run Code Online (Sandbox Code Playgroud)
并将其传递给OWIN的UseFacebookAuthentication()方法:
app.UseFacebookAuthentication(facebookAuthenticationOptions);
Run Code Online (Sandbox Code Playgroud)
如果我在OnAuthenticated回调中放置一个断点,我可以看到我的自定义声明被添加,以及许多其他声明(包括 …
facebook asp.net-web-api asp.net-identity asp.net-web-api2 owin-middleware
ASP.NET Core 1.1 中引入了将视图组件作为标记帮助程序调用。(请参阅“将视图组件作为标签助手调用”)。但以下仅返回视图的VC部分的测试。似乎该<vc:annual-orders>…</vc:annual-orders>部分根本没有被调用。
视图\共享\组件\AnnualOrders\Default.cshtml:
@{
Layout = "";
}
<div>Test for VC</div>
<div>
<vc:annual-orders>
</vc:annual-orders>
</div>
Run Code Online (Sandbox Code Playgroud)
myProj\ViewComponents\AnnualOrdersViewComponent.cs:
public class AnnualOrdersViewComponent : ViewComponent
{
private readonly OrdersContext _context;
public AnnualOrdersViewComponent(OrdersContext context)
{
_context = context;
}
public async Task<IViewComponentResult> InvokeAsync()
{
var lastOrders = _context.Where(t => t.orderType == "new");
return View(await lastOrders);
}
}
Run Code Online (Sandbox Code Playgroud)
笔记:
c# asp.net-core asp.net-core-1.1 asp.net-core-tag-helpers asp.net-core-viewcomponent
我正在尝试创建一个应用程序,该应用程序使用 RaspberryPi 摄像头录制视频并将该视频流式传输到 Web 浏览器。
我已经设法使用JavaScript SignalR 库建立了单独的Client->Server流和Server->Client流连接,现在我需要以某种方式连接服务器上的这两个函数,以便将一大块数据发送到浏览器一旦从 RaspberryPi 收到它。
这些是 Microsoft 文档中的两个示例:
public async Task UploadStream(IAsyncEnumerable<string> stream)
{
await foreach (var item in stream)
{
// Here I need to send the chunk (item) to the browser
Debug.WriteLine(item);
}
}
Run Code Online (Sandbox Code Playgroud)
public async IAsyncEnumerable<string> DownloadStream(CancellationToken cancellationToken)
{
while (true) // While there is data to stream
{
yield return "data"; // Return chunks to the browser
}
}
Run Code Online (Sandbox Code Playgroud)
这些示例是使用 C# 8.0 编写的IAsyncEnumerable,我想以这种方式使用它们。 …
我想将模型表达式(例如属性)绑定到视图组件——就像我使用 HTML 助手(例如,@Html.EditorFor())或标签助手(例如,<partial for />)一样——并在带有嵌套 HTML 的视图中重用此模型和/或标签助手。我能够将 a 定义ModelExpression为视图组件上的参数,并从中检索许多有用的元数据。除此之外,我开始遇到障碍:
asp-for标签助手?ViewData.ModelMetadata得到尊重?HtmlFieldPrefix的字段name属性?我在下面提供了一个(简化的)场景,其中包含代码和结果——但代码暴露的未知数比答案更多。已知大部分代码是不正确的,但我将其包含在内,以便我们可以有一个具体的基线来评估和讨论替代方案。
<select>需要通过数据存储库填充列表的值。假设将可能的值填充为例如原始视图模型的一部分是不切实际或不可取的(参见下面的“替代选项”)。
/Components/SelectListViewComponent.csusing system;
using Microsoft.AspNetCore.Mvc.Rendering;
public class SelectViewComponent
{
private readonly IRepository _repository;
public SelectViewComponent(IRepository repository)
{
_repository = repository?? throw new ArgumentNullException(nameof(repository));
}
public IViewComponentResult Invoke(ModelExpression aspFor)
{
var sourceList = _repository.Get($"{aspFor.Metadata.Name}Model");
var model = new SelectViewModel()
{
Options = new SelectList(sourceList, "Id", "Name")
}; …Run Code Online (Sandbox Code Playgroud) c# asp.net-core asp.net-core-tag-helpers asp.net-core-viewcomponent asp.net-core-3.1
我已经读到NodeServices 已被 ASP.NET Core 3.1 弃用。是否仍然可以使用 Node.js 将我的 JavaScript 文件构建到 中的dist文件夹中wwwroot?
asp.net-core ×6
c# ×6
asp.net ×2
c#-8.0 ×2
.net ×1
.net-core ×1
azure ×1
collections ×1
datatable ×1
facebook ×1
node.js ×1
nodeservices ×1
signalr ×1
sqlclient ×1
webdeploy ×1