我已经浏览了多个博客等,试图找出如何使用Web API 2配置StructureMap,但没有一个实现对我有用.混淆似乎是围绕MVC使用的不同IDependency Resolver和Web API使用的IDependency Resolver.
首先,哪个是正确的Nuget包,其次,如何使用纯Web API 2项目进行配置?
谢谢
这就是我到目前为止所做的工作.它是否正确?
public class StructureMapControllerActivator : IHttpControllerActivator
{
private readonly IContainer _container;
public StructureMapControllerActivator(IContainer container)
{
if (container == null) throw new ArgumentNullException("container");
_container = container;
}
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
try
{
var scopedContainer = _container.GetNestedContainer();
scopedContainer.Inject(typeof(HttpRequestMessage), request);
request.RegisterForDispose(scopedContainer);
return (IHttpController)scopedContainer.GetInstance(controllerType);
}
catch (Exception e)
{
// TODO : Logging
throw e;
}
}
}
Run Code Online (Sandbox Code Playgroud) 鉴于这个json?
[
{
"CompanyId":20,
"CompanyName":"Walmart",
"CompanyContacts":[
{
"CompanyId":20,
"FirstName":"Bob",
"LastName":"Green",
"Email":"bob@test.com",
"Phone":"1234567",
"IsActive":false
}
]
}
]
Run Code Online (Sandbox Code Playgroud) 我使用Angular前端连接到WEB API 2后端.失败的用例如下.当用户注册成功注册时,他们必须登录系统并重定向到新页面以收集更多信息.我正在使用TOKENS进行身份验证.
我在WebAPI配置中启用了CORS:
var cors = new EnableCorsAttribute("http://localhost:7812", "*", "*");
config.EnableCors(cors);
Run Code Online (Sandbox Code Playgroud)
注册请求成功,响应头具有所需的CORS头:
**Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:7812**
Content-Length:0
Date:Sun, 24 Aug 2014 09:31:55 GMT
Server:Microsoft-IIS/8.0
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?QzpcUHJvamVjdHNcVGVzdGluZ1xNYWx0QXBhcnRtZW50c1xNYWx0YXBhcnRtZW50cy5BUElcTWFsdGFwYXJ0bWVudHMuQVBJXGFwaVxhY2NvdW50XHJlZ2lzdGVy?=
Run Code Online (Sandbox Code Playgroud)
在下一步中,我尝试将用户登录到系统.作为登录的一部分,前端从服务器请求TOKEN Request URL:http://localhost:7802/token.请求标头再次发送一个Origin标头,Origin:http://localhost:7812但这次我收到错误:XMLHttpRequest cannot load http://localhost:7802/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:7812' is therefore not allowed access.
有人有主意吗?
总 NOOB 问题。我已经使用新的 linqpad 大约 20 分钟了。伟大的!
但现在我想删除数据库中的一行。我使用的是 EF5.0 连接。我似乎无法在帮助文件或网络上找到任何内容。我唯一能找到的是 DeleteOnSubmit,它不适用于 EF(据我所知)。我也试过 DeleteObject 也不起作用。这是我尝试过的。
var co = Companies.First();
co.Dump();
Companies.DeleteObject(co);
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Web API 2设置结构图ver 3.0.5.0.
我已经遵循了这个实现:使用ASP.NET Web API 2.1配置依赖注入
但是,在针对ComplexesController进行攻击时出现此错误:
尝试创建"ComplexesController"类型的控制器时发生错误.确保控制器具有无参数的公共构造函数.
谁能看到我的structuremap配置有什么问题?永远不会调用Create方法.
这是我的实施:
public class StructureMapControllerActivator : IHttpControllerActivator
{
private readonly IContainer _container;
public StructureMapControllerActivator(IContainer container)
{
if (container == null) throw new ArgumentNullException("container");
_container = container;
}
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
try
{
var scopedContainer = _container.GetNestedContainer();
scopedContainer.Inject(typeof(HttpRequestMessage), request);
request.RegisterForDispose(scopedContainer);
return (IHttpController)scopedContainer.GetInstance(controllerType);
}
catch (Exception e)
{
// TODO : Logging
throw e;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个方法在我的创业公司......
public void InitializeContainer()
{
// STRUCTURE MAP …Run Code Online (Sandbox Code Playgroud) 我使用Kendo Grid进行内联编辑.当我单击"更新"按钮时,将使用此签名对我的控制器方法进行POST.控制器操作被命中,因此POST正在运行.
[HttpPost]
public HttpResponseMessage SaveAccountAdmin(string jsonCompanyContacts)
Run Code Online (Sandbox Code Playgroud)
但是,更新操作中的POST数据永远不会到达 - 它始终为null.
update: {
url: "/Company/SaveAccountAdmin",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json",
data: {
jsonCompanyContacts: "John Doe"
}
},
Run Code Online (Sandbox Code Playgroud)
这是完整的数据源代码.
var dataSource = new kendo.data.DataSource(
{
batch: false,
pageSize: 10,
transport: {
create: {
url: "/Company/SaveAccountAdmin",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json"
},
read: {
url: "/Company/ReadAccountAdmin"
},
update: {
url: "/Company/SaveAccountAdmin",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json",
data: {
jsonCompanyContacts: "John Doe"
}
},
//destroy: {},
parameterMap: function …Run Code Online (Sandbox Code Playgroud) 我有2个阵列,我想要一个笛卡尔积.举个例子:
客户数组:
[10,A]
[11,B]
Run Code Online (Sandbox Code Playgroud)
债务人数组:
[88,W]
[99,X]
Run Code Online (Sandbox Code Playgroud)
我想生成一个新的customerDebtor数组:
[10,A,88,W]
[10,A,99,X]
[11,B,88,W]
[11,B,99,X]
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用此代码:
for (var i = 0; i < customerArray.length; i++) {
for (var l = 0; l < debtorArray.length; l++) {
$.each(customerArray[i], function (ndx, val) {
//???? push values into customerDebtorMatrix array
});
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Angular UI typeahead.我的问题是下拉列表没有显示.正确调用远程数据并返回数据...但下拉列表拒绝显示...
<td colspan="5">
<pre>Model: {{selected| json}}</pre>
<input id="AutoCompleteDebtor"
type="text"
data-ng-model="selected"
data-typeahead="debtor for debtor in Debtors($viewValue)"
class="form-control input-sm"
placeholder="Enter 3 letters of Debtor Name" />
</td>
Run Code Online (Sandbox Code Playgroud)
更新:好的 - 它与字符串名称数组配合良好,但我如何使用对象?
<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
<title>Angular Typeahead</title>
<link href="Content/bootstrap/bootstrap.min.css" rel="stylesheet" />
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/ui-bootstrap-tpls-0.6.0.min.js"></script>
<script>
angular.module('myApp', ['ui.bootstrap'])
.controller('SimpleController', function ($scope) {
$scope.people = [
{ name: 'Alan', age: 23 },
{ name: 'Bruce', age: 24 },
{ name: 'Celine', age: 53 },
{ name: 'Dan', age: 43 },
{ …Run Code Online (Sandbox Code Playgroud) 我想我可能有配置问题.循环遍历一个对象数组时出错.这是错误.正在将对象数组加载到视图中,它们看起来是正确的.它只是爆炸的"repeat.for".
Unhandled promise rejection Error: Incorrect syntax for "for". The form is: "$local of $items" or "[$key, $value] of $items".
at SyntaxInterpreter.for (http://localhost:8080/jspm_packages/github/aurelia/templating-binding@0.13.0/index.js:142:13)
at SyntaxInterpreter.interpret (http://localhost:8080/jspm_packages/github/aurelia/templating-binding@0.13.0/index.js:27:34)
at TemplatingBindingLanguage.createAttributeInstruction (http://localhost:8080/jspm_packages/github/aurelia/templating-binding@0.13.0/index.js:267:46)
at ViewCompiler.compileElement (http://localhost:8080/jspm_packages/github/aurelia/templating@0.13.2/index.js:1442:41)
at ViewCompiler.compileNode (http://localhost:8080/jspm_packages/github/aurelia/templating@0.13.2/index.js:1331:23)
at ViewCompiler.compileElement (http://localhost:8080/jspm_packages/github/aurelia/templating@0.13.2/index.js:1536:31)
at ViewCompiler.compileNode (http://localhost:8080/jspm_packages/github/aurelia/templating@0.13.2/index.js:1331:23)
at ViewCompiler.compileElement (http://localhost:8080/jspm_packages/github/aurelia/templating@0.13.2/index.js:1536:31)
at ViewCompiler.compileNode (http://localhost:8080/jspm_packages/github/aurelia/templating@0.13.2/index.js:1331:23)
at ViewCompiler.compileElement (http://localhost:8080/jspm_packages/github/aurelia/templating@0.13.2/index.js:1536:31)
at ViewCompiler.compileNode (http://localhost:8080/jspm_packages/github/aurelia/templating@0.13.2/index.js:1331:23)
at ViewCompiler.compileNode (http://localhost:8080/jspm_packages/github/aurelia/templating@0.13.2/index.js:1353:33)
at ViewCompiler.compile (http://localhost:8080/jspm_packages/github/aurelia/templating@0.13.2/index.js:1314:12)
at http://localhost:8080/jspm_packages/github/aurelia/templating@0.13.2/index.js:1583:49
at run (http://localhost:8080/jspm_packages/npm/core-js@0.9.18/modules/es6.promise.js:91:43)
at http://localhost:8080/jspm_packages/npm/core-js@0.9.18/modules/es6.promise.js:105:11
at module.exports (http://localhost:8080/jspm_packages/npm/core-js@0.9.18/modules/$.invoke.js:6:25)
at queue.(anonymous function) (http://localhost:8080/jspm_packages/npm/core-js@0.9.18/modules/$.task.js:40:9)
at Number.run (http://localhost:8080/jspm_packages/npm/core-js@0.9.18/modules/$.task.js:27:7)
at listner (http://localhost:8080/jspm_packages/npm/core-js@0.9.18/modules/$.task.js:31:9)
Run Code Online (Sandbox Code Playgroud)
视图 …
如在标题中,我如何防止2个列表之间的重复.我试图在#selected列表中停止重复.我怀疑它在接收事件中完成但我没有运气.
$(function () {
$("#options").sortable({
connectWith: $("#selected"),
helper: 'original',
revert: true,
tolerance: "pointer",
});
$("#selected").sortable({
connectWith: $("#options"),
helper: 'original',
receive: function (event, ui) {
},
});
$("#options,#selected").disableSelection();
$("#SkillGroups").change(function () {
$.ajax({
type: "POST",
url: "/Contractor/Contractor/GetSkillsBySkillGroup",
data: { skillGroupId: $("#SkillGroups").val() },
success: function (result) {
$loadList(result);
}
})
});
});
Run Code Online (Sandbox Code Playgroud) 我刚决定尝试Automapper.
我在我的项目中写了几个单元测试,当我"全部运行"时,它们都按预期通过.但是,当我运行单独的测试时,它们会失败...所以,是否有一些特殊的设置我错过了?
这是代码.当我全部运行时,这通过了测试.
[TestMethod]
public void Mappings_ConfigureMappings_pass()
{
Mapper.CreateMap<Address, AddressDTO>();
Mapper.AssertConfigurationIsValid();
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行实际的映射测试时,测试失败了.
[TestMethod]
public void Mappings_ViewModel_Address_ToDTO_pass()
{
var address = new Address()
{
Address1 = "Line1",
Address2 = "Line2",
Address3 = "Line3",
Country = "ROI",
County = "Co Dublin",
PostCode = "ABC",
TownOrCity = "Dublin"
};
AddressDTO addressDTO = Mapper.Map<Address, AddressDTO>(address);
Assert.IsNotNull(addressDTO);
Assert.AreEqual("ROI", addressDTO.Country);
Assert.AreEqual("Dublin", addressDTO.TownOrCity);
}
Run Code Online (Sandbox Code Playgroud)
这里是相关的类......你可以看到它们是相同的,为什么测试失败?这是我在设置中遗漏的东西吗?
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 …Run Code Online (Sandbox Code Playgroud) angularjs ×2
jquery ×2
kendo-grid ×2
kendo-ui ×2
structuremap ×2
angular-ui ×1
asp.net ×1
aurelia ×1
automapper ×1
c# ×1
cors ×1
javascript ×1
json ×1
linqpad ×1