我正在尝试将XML发布到asp.net核心2:
$.ajax({
type: "POST",
url: 'api/Test',
data: "<test>hello<test>",
contentType: "application/xml",
success: function (response) { alert(response); },
});
Run Code Online (Sandbox Code Playgroud)
我应该如何编写动作,使其接受xml作为参数?
IActionResult Post([FromBody]string xml) -> xml为空IActionResult Post([FromBody]XElement xml) -> xml为空IActionResult Post(XElement xml)-> InvalidOperationException:无法创建类型为'System.Xml.Linq.XElement'的实例。模型绑定的复杂类型不能为抽象或值类型,并且必须具有无参数构造函数。IActionResult Post(string xml) -> xml为空在Startup.ConfigureServices中:
services.AddMvc()
.AddXmlSerializerFormatters();
Run Code Online (Sandbox Code Playgroud)
如何编写控制器以使其接受XML作为参数?我知道我可以从HttpContext.Request中读取它,但是我希望它可以作为参数
我想在每个单元测试中创建干净的内存数据库。当我运行多个测试时,以前测试中的数据仍保留在数据库中。如何处理现有的内存数据库?
我使用以下代码初始化每个测试:
[TestInitialize]
public void TestInitialize()
{
Services = new ServiceCollection();
Services.AddScoped<DbContextOptions<MyDbContext>>(sp => new DbContextOptionsBuilder<TacsDbContext>()
.UseInMemoryDatabase("MyTestDbContext")
.Options);
Services.AddTransient<InMemoryMyDbContext>();
Services.AddTransient<MyDbContext>(sp => sp.GetService<InMemoryTacsDbContext>());
ServiceProvider = Services.BuildServiceProvider();
}
[TestMethod]
public void Test1()
{
using (var dbContext = ServiceProvider.GetService<MyDbContext>()) ...
}
[TestMethod]
public void Test2()
{
using (var dbContext = ServiceProvider.GetService<MyDbContext>()) ...
}
Run Code Online (Sandbox Code Playgroud)
我使用.NET Core 2.0和Entity Framework Core 2.0
编辑
我无法使用标准注册:Services.AddDbContext<InMemoryMyDbContext>(...),因为
public class InMemoryMyDbContext : MyDbContext
{
public InMemoryMyDbContext(DbContextOptions<InMemoryMyDbContext> options)
: base(options) { } //compiler error
public InMemoryMyDbContext(DbContextOptions<MyDbContext> options)
: base(options) { …Run Code Online (Sandbox Code Playgroud) unit-testing entity-framework dependency-injection entity-framework-core .net-core
我正在尝试自动化 SQL 部署,包括用户和角色。当我在 Sql Management Studio 中创建用户时它可以工作,但是当我部署 dacpack 时我得到 SqlException: Login Failed for user 'MyUser'
我在本地主机上安装了 SQL Server 2016,同时启用了 Sql Server 和 Windows 身份验证。
我在 Visual Studio 中有包含这些文件的 Sql 数据库项目:
MyUser.sql [构建操作=构建]
CREATE USER [MyUser] WITH Password='$(MyUserPassword)';
Run Code Online (Sandbox Code Playgroud)RolesMembership.sql [构建动作=构建]
ALTER ROLE [db_datareader] ADD MEMBER [MyUser]
GO
ALTER ROLE [db_datawriter] ADD MEMBER [MyUser]
Run Code Online (Sandbox Code Playgroud)我构建它并使用命令行部署创建的 dacpac:
SqlPackage.exe /Action:Publish
/SourceFile:"MyDatabase.dacpac"
/Profile:"MyDatabase.publish.xml"
/v:MyUserPassword=c2Aaaaaaaaaaaa`
Run Code Online (Sandbox Code Playgroud)当我在 sql management studio 中执行此脚本时,它会起作用。但是当我使用 SqlPackage 部署它时,它会导致
SqlException: 用户“MyUser”登录失败
编辑:
我调查了生成的 sql 脚本,其中包括REVOKE CONNECT:
CREATE USER [MyUser] …Run Code Online (Sandbox Code Playgroud) sql-server database-project dacpac sqlpackage sql-server-2016
我正在使用 webclient 从 sharepoint 在线下载 XML 文件。
但是,当我使用WebClient.DownloadString(string url)方法时,某些字符未正确解码。
当我使用WebClient.DownloadFile(string url, string file)然后读取文件时,所有字符都是正确的。
xml 本身不包含编码声明。
string wrongXml = webClient.DownloadString(url);
//wrongXml contains Ä™ instead of ?
webClient.DownloadFile(url, @"C:\temp\file1.xml");
string correctXml = File.ReadAllText(@"C:\temp\file1.xml");
//contains ?, like it should.
Run Code Online (Sandbox Code Playgroud)
此外,当在 Internet Explorer 中打开 url 时,它会正确显示。
这是为什么?是因为我的机器上的默认 Windows 编码或 web 客户端在使用 DownloadString 或 DownloadFile 时处理响应的方式不同吗?
我正在使用EF Core 2.1预览本应该减少N + 1查询问题.我正在尝试进行查询,选择论坛帖子与帖子的作者:
dbContext.ForumThreads
.Include(t => t.Posts)
.Take(n)
.Select(t => new
{
t.Id,
t.Title,
PostAuhtors = t.Posts.Select(p => p.Author).Take(5)
}).ToArray();
Run Code Online (Sandbox Code Playgroud)
这会生成n + 1个查询:对于每个ForumThread,它会选择帖子作者
架构很简单:
public class ForumThread
{
public Guid Id {get;set;}
public string Title {get;set;}
public ICollection<ForumPost> Posts {get;set;}
}
public class ForumPost
{
public Guid Id {get;set;}
public string Author {get;set;}
public string Content {get;set;}
}
Run Code Online (Sandbox Code Playgroud) 我有两个网络核心控制台应用程序,我使用visual studio作为Web作业部署到Azure.
<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
Run Code Online (Sandbox Code Playgroud)
当我单独发布它时,它运行良好.
我试过创造webproject/properties/webjob-list.json.路径与webproject/webproject.csproj相关:
{
"$schema": "http://schemastore.org/schemas/json/webjobs-list.json",
"WebJobs": [
{
"filePath": "../PeopleWebJob/PeopleWebJob.csproj"
},
{
"filePath": "../Reminder/ReminderWebJob.csproj"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我也试过安装Microsoft.Web.WebJobs.Publishnuget,但看起来这种方法不适用于.NET Core.
Alternativelly,
我试过dotnet publishWebJob项目并将输出复制到$(Build.ArtifactStagingDirectory)/webproject.zip/App_Data/jobs/Triggered
azure-web-sites azure-webjobs azure-devops asp.net-core asp.net-core-2.0
我目前有一个带有内联编辑的 html 表,它使用普通的 Angular Reactive Forms,包括很多验证规则:
\n\nvar formArray = new FormArray(this.items.map(createItemFormGroup));\n\ncreateItemFormGroup(item){\n return new FormGroup({\n prop1: new FormControl(item.prop1, [Validators.required, Validators.min(1)])\n prop2:...\n })\n}\nRun Code Online (Sandbox Code Playgroud)\n\n有没有关于如何将 ag-grid 与 Angular 验证集成的示例?要求是,我需要突出显示无效的单元格值。我不一定需要使用 FormGroup\xe2\x80\xa6
\n\n我可能会使用完整的行编辑,所以一种方法是仅为正在编辑的行创建 FormGroup......
\n我刚刚安装了这个包:
npm install moment-timezone --save
Run Code Online (Sandbox Code Playgroud)
在角度组件中,我像这样使用它:
import * as moment from 'moment-timezone';
moment().tz('America/New York');
Run Code Online (Sandbox Code Playgroud)
我想这会将所有时区数据 (900+kB) 添加到供应商包中并减慢应用程序启动速度。
如何仅按需异步加载?
我真的很想使用 NPM 解决方案,以便在更新 npm 包时自动更新数据库。没有人会记得手动更新时区数据库。
@Dimanoid 的回答显示了如何在没有数据的情况下从 npm 导入时刻时区:
import * as moment from 'moment-timezone/moment-timezone';
Run Code Online (Sandbox Code Playgroud)
以下是将数据包含为延迟加载资产的方法:
修改angular.json -> projects -> yourprojects -> Architect -> build -> options:
"assets": [
"src/favicon.ico",
"src/assets/i18n",
{ //add this
"glob": "latest.json",
"input": "node_modules/moment-timezone/data/packed",
"output": "assets/moment-timezone"
}
]
Run Code Online (Sandbox Code Playgroud)从资产文件夹中按需请求:
import * as moment from 'moment-timezone/moment-timezone';
import { PlatformLocation } from '@angular/common';
export class MyComponent implements OnInit { …Run Code Online (Sandbox Code Playgroud)我使用 C# 8 可空引用类型。
我有一个泛型类,它可能接受可为空引用类型作为类型参数。
有没有办法根据泛型类型参数声明不可为空的类型,这些参数可能是可为空的引用类型(甚至是可空结构)?
abstract class Selector<T>
{
T SelectedItem;
// how to make item parameter not nullable?
abstract string Format(T! item);
// how to make item parameter not nullable?
Func<T!, string> FormatFunction;
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有行虚拟化的自定义数据网格。
当我通过拖动滚动条向下滚动时,滚动会在呈现新“页面”之前中断。
你知道为什么会发生这种情况吗?
<div class="sg-container" style="height:200px">
<div class="simple-grid" style="grid-template-columns: 3em minmax(5em, auto); ">
<Virtualize Items="_products"
ItemSize="32">
<ItemContent>
<div class="sg-row sg-row ">
<div class="sg-cell ">@context.Id</div>
<div class="sg-cell ">@context.Name</div>
</div>
</ItemContent>
</Virtualize>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
演示 (REPL) 在这里:https : //blazorrepl.com/repl/mvkBuHuB22FVBxY258
c# ×4
asp.net-core ×2
sql-server ×2
.net ×1
.net-core ×1
ag-grid ×1
ajax ×1
angular ×1
azure-devops ×1
blazor ×1
c#-9.0 ×1
dacpac ×1
ecmascript-6 ×1
generics ×1
momentjs ×1
non-nullable ×1
npm ×1
sqlpackage ×1
unit-testing ×1
webclient ×1
xml ×1