我的页面上有一些"静态"HTML:
<div id="DIVISIONS">
<ul class="nav nav-tabs" id="DIVISIONTABS">
@* <li> nodes will be injected here by javascript *@
</ul>
<div class="tab-content" id="DIVISIONTABPANES">
@* <div class="tab-pane"> nodes will be injected here by javascript *@
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
在页面加载时,我创建一个选项卡'框架',即创建引导选项卡和选项卡内容容器.
我通过以下方式触发流程:
$(window).bind("load", prepareDivisionTabs);
Run Code Online (Sandbox Code Playgroud)
而"prepareDivisionTabs"就是这样做的:
function prepareDivisionTabs() {
// Retrieve basic data for creating tabs
$.ajax({
url: "@Url.Action("GetDivisionDataJson", "League")",
cache: false
}).done(function (data) {
var $tabs = $('#DIVISIONTABS').empty();
var $panes = $('#DIVISIONTABPANES').empty();
for (var i = 0; i < data.length; i++) {
var d = data[i]; …Run Code Online (Sandbox Code Playgroud) 我有一个显示公司和员工列表的剃刀页面:
@page "/admin/companies/editor/{id}"
@model EditorModel
@{
}
<h1>admin . companies . editor</h1>
<h4>Id = @Model.Id</h4>
<h4>Name = @Model.OrgStructure.Organisation.Name</h4>
<h4>Staff Count = @Model.OrgStructure.Staff.Count</h4>
<table>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
@if (Model.OrgStructure.Staff.Any())
{
foreach (Staff s in Model.OrgStructure.Staff)
{
<tr>
<td>@s.Id</td>
<td>@s.Username</td>
</tr>
}
}
else
{
<tr>
<td colspan="2">No Staff</td>
</tr>
}
</table>
<a class="btn btn-primary" asp-page="/admin/companies/staff/create" asp-route-orgId="@Model.OrgStructure.Organisation.Id">Create Staff</a>
@functions {
public class EditorModel : PageModel
{
public string Id { get; set; }
public OrganisationStructure OrgStructure { get; set; }
public …Run Code Online (Sandbox Code Playgroud) 我创建了一个带有3个实体类的C#类库和一个用于代码优先生成数据库的DbContext.所有版本都很顺利.我创建了一个单独的测试库,带有DbContext类的类库已按预期运行.
现在,我想使其中一个字段成为必需字段并遵循代码优先约定,我在实体类中为属性添加了[Required]属性.下一步是启用迁移.
我进入了软件包管理器控制台,输入了"enable-migrations"并且... bang ..." 无法加载指定的元数据资源 ".
作为参考,我的DbContext类包括:
public OrganisationsContext()
: base("Leegz_Entities_Organisations")
{
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
}
public DbSet<Organisation> Organisations { get; set; }
public DbSet<Member> Members { get; set; }
public DbSet<LeegzUser> LeegzUsers { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的app.config包含:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider …Run Code Online (Sandbox Code Playgroud) entity-framework code-first ef-migrations entity-framework-6
我有一项包含项目目录的服务。这些项目取决于服务的活跃公司。
// Tell Service to refresh in context of Company 1
await Service.SetActiveCompany(1);
// Get Catalog (which will be in the context of Company 1)
Catalog catalog = Service.Catalog;
// Tell Service to refresh in context of Company 2
await Service.SetActiveCompany(2);
// Get Catalog (which will be in the context of Company 2)
catalog = Service.Catalog;
Run Code Online (Sandbox Code Playgroud)
上面的方法单独运行效果很好。目录将更新为来自相应公司的项目。
在我的应用程序中,我有一个布局,其中包括用于选择活动公司的下拉列表。当这种情况发生变化时,我打电话:
await Service.SetActiveCompany(dropdown.selectedCompanyId);
Run Code Online (Sandbox Code Playgroud)
在服务中,我刷新目录并引发事件
public class CompanyChangedEventArgs : EventArgs { }
public class Service
{
public event EventHandle<CompanyChangedEventArgs> CompanyChanged;
protected void OnCompanyChanged()
=> …Run Code Online (Sandbox Code Playgroud) 我有以下库:
EntityMODEL.dll (包含POCO类)
EntityDAL.dll [引用EntityMODEL.dll]
EntitySERVICE.dll [同时引用EntityMODEL.dll和EntityDAL.dll]
EntityTEST.dll [与EntitySERVICE.dll和EntityMODEL.dll]
该EntitySERVICE.dll和EntityMODEL.dll都需要由外界所引用的(例如,从EntityTEST.dll),这意味着外界不需要参考EntityDAL.dll或实体框架。
这是我来自EntityDAL.dll的DbContext ...
EntityDAL.dll | DbContext
public class FooContext : DbContext
{
public FooContext()
{
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
}
public DbSet<Bars> Bars{ get; set; }
// NESTED DbConfiguration
public class ModelConfiguration : DbConfiguration
{
public ModelConfiguration()
{
this.SetHistoryContext( .... )
}
}
}
Run Code Online (Sandbox Code Playgroud)
从EntityTEST.dll运行单元测试时,一切正常。
我的解决方案中有几个这样的“程序包”(都遵循相同的MODEL / DAL / SERVICE结构),每个程序包都处理基础实体的不同相关组。
为了协调跨多个“实体”包的活动,我具有一个“编排”(或任务)层,其中包含以下库:
TaskMODEL.dll [包含POCO类]
TaskSERVICE.dll [引用TaskMODEL.dll,EntitySERVICE.dll和EntityMODEL.dll]
-还提供TaskMODEL.dll类和EntityMODEL.dll类之间的转换
TaskTEST.dll [引用TaskSERVICE.dll和TaskMODEL .dll] …
我正在使用 Blazor(托管)并希望在将结果发送回客户端时保留引用。下面的示例实际上并不需要引用保存,但它是我针对更复杂的结构的测试场景。
该有效负载如下所示:
[
{
"id":"a583baf9-8990-484f-9dc6-e8ea822f49c6",
"name":"Neil",
"themeName":"Blue Gray"
},
{
"id":"a7a8e753-c7af-4b29-9242-7b2f5bdac830",
"name":"Caroline",
"themeName":"Yellow"
}
]
Run Code Online (Sandbox Code Playgroud)
使用
var result = await response.Content.ReadFromJsonAsync<IEnumerable<Staff>>();
Run Code Online (Sandbox Code Playgroud)
我能够在客户端中获取我的 Staff 对象。
我更新了服务器上的 StartUp.cs 以包括:
services.AddControllersWithViews()
.AddJsonOptions(o =>
o.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve
);
Run Code Online (Sandbox Code Playgroud)
结果是返回负载现在看起来像这样:
{
"$id":"1",
"$values":
[
{
"$id":"2",
"id":"a583baf9-8990-484f-9dc6-e8ea822f49c6",
"name":"Neil",
"themeName":"Blue Gray"
},
{
"$id":"3",
"id":"a7a8e753-c7af-4b29-9242-7b2f5bdac830",
"name":"Caroline",
"themeName":"Yellow"
}
]
}
Run Code Online (Sandbox Code Playgroud)
看来是正确的。
这导致 JSON 反序列化异常:
var result = await response.Content.ReadFromJsonAsync<IEnumerable<Staff>>();
Run Code Online (Sandbox Code Playgroud)
因此,我认为在客户端反序列化时可能也需要包含引用处理选项。于是,改为:
JsonSerializerOptions options = new JsonSerializerOptions();
options.ReferenceHandler = ReferenceHandler.Preserve;
var result …Run Code Online (Sandbox Code Playgroud) json asp.net-web-api asp.net-core asp.net-core-webapi blazor-webassembly
我的应用程序的核心由一套微服务组成,每个微服务主要使用 DDD 架构。
在标准 MVC 解决方案中,客户端将访问我的控制器,控制器将与我的微服务交互。
在 Blazor 服务器设置中,方法类似。
但是,通过 Blazor WebAssembly(托管)设置,我有机会直接从 Blazor WebAssembly 客户端引用我的微服务。
这明智吗?
或者我最好在 Blazor 服务器上创建一个外观(该服务器进而访问微服务)并仅从 Blazor WebAssembly 客户端与该外观通信?
我的 Blazor 服务器有自己的引用微服务的需求,我正要在客户端上注册微服务,然后想知道这是否明智。
我有一个具有子部分视图的视图(在主_Layout.cshtml视图中).
我在主视图上有按钮(class = get-widgets),它应该调用控制器并检索一些数据以填充到子部分视图(_WidgetListPartial).这工作正常......一次.点击事件注册似乎在第一次点击后丢失,第二次和后续点击什么都不做.
我还注意到,在调用运行时(没有执行延迟的thread.sleep(2000)),数据加载文本没有出现.
主视图中的代码:
Index.cshtml
@model MyApp.Models.WidgetViewModels.MainWidgetViewModel
@{ ViewBag.Title = "Widget Control Panel"; }
<div class="jumbotron">
<h1>@ViewBag.Title.</h1>
</div>
<div class="row">
<div class="col-md-6">
<h2>Widgets</h2>
<button class="btn btn-default pull-right get-widgets" data-loading-text="Loading ...">Refresh</button><br />
<div class="widgets">
@{Html.RenderPartial("_WidgetListPartial", Model.Widgets);}
</div>
<p>@Html.ActionLink("Create Widget", "Create", "Widget")</p>
</div>
</div>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>
<script>
$(document).ready(function () {
$(document).on('click', '.get-widgets', function (e) {
var $btn = $(this).prop('disabled', true);
$.ajax('@Url.Action("GetWidgets", "Desktop")').done(function (html) {
$('.widgets').html(html);
}).always(function () {
$btn.prop('disabled', false);
alert('You pressed refresh ...');
});
});
}); …Run Code Online (Sandbox Code Playgroud) asp.net-core ×2
blazor ×2
jquery ×2
ajax ×1
architecture ×1
c# ×1
code-first ×1
javascript ×1
json ×1
razor ×1
razor-pages ×1
tabs ×1