我是ASP/EF的新手.我在我的个人项目中使用ASP 5和Entity Framework 7.
所以我能够使用代码优先方法创建数据库和表,但是所有表名都是单数的,默认情况下不会复数.
在我的ServerMatrixDemoDB DbContext文件中,我在DBSets下面创建了:
public class ServerMatrixDemoDB : DbContext
{
public ServerMatrixDemoDB()
{
Database.EnsureCreated();
}
public DbSet<Domain> Domains { get; set; }
public DbSet<Environment> Environments { get; set; }
public DbSet<Network> Networks { get; set; }
public DbSet<OsVersion> OsVersions { get; set; }
public DbSet<Tier> Tiers { get; set; }
public DbSet<Service> Services { get; set; }
public DbSet<HardwareType> HardwareTypes { get; set; }
public DbSet<Powershell> Powershell { get; set; }
public DbSet<DotNetVersion> DotNetVersions { get; set; …Run Code Online (Sandbox Code Playgroud) 我正在用ASP.NET CORE 1.1. 我想弄清楚如何返回带有不可接受状态代码的错误消息406。
这是我在其中一个 Api 控制器中编写的方法:
[HttpPost]
public async Task<IActionResult> PostStoreCoolerData([FromBody] StoreCoolerData storeCoolerData)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_context.StoreCoolerData.Add(storeCoolerData);
var addResponse = await _context.SaveChangesAsync();
if (addResponse > 0)
{
var getItemInfo = _context.StoreCoolerData.Include(c => c.CoolerBrand)
.Include(c => c.CoolerType)
.Include(c => c.DrinkType)
.Where(i => i.Id == storeCoolerData.Id);
return CreatedAtAction("GetStoreCoolerData", new { getItemInfo });
}
return StatusCode(406, "There was a problem saving record in the database. Please try again.");
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我return StatusCode(406, "There was …
我正在处理一个项目,我想在代码优先方法中将一些属性设置为可选。在我的类文件中,我已添加[Required]到属性中,我将在 SQL 中设置为 not null,而没有的属性[Required]应设置为在 SQL 中允许为 null,但对于其中包含 ID 的某些属性,它在 SQL 中设置为 not null。下面是我的示例类,其中包含在数据库中生成表时应使用的属性。我想EnvironmentLastDeployedId在 MSSQL 中创建新数据库时将属性设置为可选。
public class Version
{
public int Id { get; set; }
[Required]
public string PackageName { get; set; }
public string VersionName { get; set; }
public string OriginalPackageName { get; set; }
[Required]
public string CommitID { get; set; }
public string CommitMessage { get; set; }
public int ApplicationId { get; set; }
public Application Application …Run Code Online (Sandbox Code Playgroud) asp.net entity-framework entity-framework-6 asp.net-core-mvc
我正在创建策略库授权,并希望允许一个策略中的多个用户访问网页。
我在启动文件中创建了如下所示的策略。问题,如何在一个策略中使用多个用户名?我查看了 的方法.RequireUserName,它只接受字符串用户名。
策略名称AdminServiceAccount主要是我对添加多个用户感兴趣。如果我使用 param.RequireUserName("DOMAIN\\USER1,DOMAIN\\USER2")会起作用吗?我不这么认为,但想检查是否有替代方法。
services.AddAuthorization(
option =>
{
option.AddPolicy("Admin", policy => policy.RequireRole("Domain\\GroupName"));
option.AddPolicy("SuperAdminUser", policy => policy.RequireUserName("DOMAIN\\SuperAdminUser"));
option.AddPolicy("AdminServiceAccount", policy => policy.RequireUserName("DOMAIN\\USER1"));
}
);
Run Code Online (Sandbox Code Playgroud)
更新 2:
所以在我的控制器中,我添加[Authorize(Policy = "UserNamesPolicy")]如下所示:
[Authorize(Policy = "UserNamesPolicy")]
public class ServersController : Controller
{
private readonly ServerMatrixDbContext _context;
public ServersController(ServerMatrixDbContext context)
{
_context = context;
}
// GET: Servers
public async Task<IActionResult> Index()
{
// Some code here
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的启动文件:
services.AddAuthorization(
option =>
{
option.AddPolicy("UserNamesPolicy", …Run Code Online (Sandbox Code Playgroud) 我正在编写一个方法,该方法将通过向 RestApi URL 发出 GET 请求来返回 JSON 数据。当我在调试模式下运行应用程序时,我得到以下数据作为响应。
Id = 337, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
这是我的代码:
public class Methods
{
public static async Task<JObject> Get(string url, string username, string password)
{
var credentials = new NetworkCredential(username, password);
HttpClientHandler handler = new HttpClientHandler { Credentials = credentials };
HttpClient client = new HttpClient(handler);
// client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
return JObject.Parse(await response.Content.ReadAsStringAsync());
}
return …Run Code Online (Sandbox Code Playgroud) 我Aurelia-Validation在我的项目中使用并尝试验证电子邮件地址.当我添加电子邮件时example@g,它会通过验证.不该发电子邮件确认已.com,.net等扩展在年底通过验证?以plunker链接为例.
这是截图,显示我的意思:
