在Asp.net Web Api中,如何使用int或字符串设置响应的状态代码,而不是StatusCode枚举?
在我的情况下,我想返回状态代码422,"Unprocessable Entity"的验证错误,但是没有枚举器.
HttpResponseMessage response = Request.CreateResponse();
response.StatusCode = HttpStatusCode.UnprocessableEntity; //error, not in enum
Run Code Online (Sandbox Code Playgroud) 我正在开发一个MVC WebAPI,它使用EF和POCO类进行存储.我想要做的是从XML中删除命名空间,以便端点返回并接受没有它的xml对象.(json工作得很好)
<ACCOUNT xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Platform.Services.AccountService.Data">
<id>22</id>
<City i:nil="true"/>
<Country i:nil="true"/>
<Email>testas@email.com</Email>
<Phone i:nil="true"/> ...
Run Code Online (Sandbox Code Playgroud)
我希望这能奏效
<ACCOUNT>
<id>22</id>
<City i:nil="true"/>
<Country i:nil="true"/>
<Email>testas@email.com</Email>
<Phone i:nil="true"/> ...
Run Code Online (Sandbox Code Playgroud)
希望无需用一堆属性来装饰POCO.
我为此设置了一个测试解决方案,实际上,这些方法正在受到欢迎(必须是我系统中的其他一些问题).无论如何 - 我使用这个解决方案的结果是:
<ArrayOfAccount>
<Account>
<id>22</id>
<name>TestAcc</name>
<parentid xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:nil="true"/>
<status_id xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:nil="true"/>
<Email>Test@Test.com</Email>
</Account>
</ArrayOfAccount>
Run Code Online (Sandbox Code Playgroud)
摆脱了顶部的架构,但现在搞砸了属性:(这是一个示例项目的链接
使用FakeItEasy,如何检查我的对象的方法是否在同一个对象上调用另一个方法?
考试:
[TestMethod]
public void EatBanana_CallsWillEat()
{
var banana = new Banana();
var myMonkey = new Monkey();
myMonkey.EatBanana(banana);
//this throws an ArgumentException, because myMonkey is a real instance, not a fake
A.CallTo(() => myMonkey.WillEat(banana)
.MustHaveHappened();
}
Run Code Online (Sandbox Code Playgroud)
班级:
public class MyMonkey {
private readonly IMonkeyRepo _monkeyRepo;
public MyMonkey(IMonkeyRepo monkeyRepo) {
_monkeyRepo = monkeyRepo;
}
public void EatBanana(Banana banana) {
//make sure the monkey will eat the banana
if (!this.WillEat(banana)) {
return;
}
//do things here
}
public bool WillEat(Banana banana) { …Run Code Online (Sandbox Code Playgroud) 编辑:为了清楚起见,我不是在质疑jQuery的精彩之处.我到处都用它.我只是不明白使用Widgets优于常规js对象的优势,这些对象利用了jQuery的功能.
我最近不得不在我正在使用的应用程序中使用一些jQuery Widgets,我发现它们非常不直观/丑陋.似乎有许多习惯要学习哪些并没有真正添加任何值,并且语法不适用于智能感知或简单重构.
不要误会我的意思,我经常使用jQuery,如果没有它,我会讨厌生活,只是没有看到小部件约定中的值.我确定我错过了这一点,因为它们如此普遍,所以请告诉我.为什么人们创建jQuery小部件而不是常规的js对象?
过度简化的例子:
常规Javascript对象:
//instantiate & render
var foo = new MyDomain.MyObject('#mySpecialInput', options)
foo.render();
//assign to property
foo.value = 5;
//call method
foo.multiplyBy(2);
//bind event
foo.change(function(){
alert('it changed');
}
Run Code Online (Sandbox Code Playgroud)
Jquery小部件:
//instantiate & render
var $foo = $('#mySpecialInput');
$foo.myWidget(options);
//assign to property
$foo.myWidget('option', 'value', 5);
//call method
$foo.myWidget('multiplyBy', 2);
//bind event
$foo.bind('myWidgetChange', function(){
alert('it changed');
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Ajax/Request将元素加载到div容器中.默认情况下我隐藏了一个输入框.如果用户单击该div上的编辑图标,我想显示输入框.这是我的代码:
HTML代码
<div class='container'>
<input type = 'text' onkeydown='saveFn(event,this)' name = 'editemail' class = 'editemail' style='display:none; height:20px;' />
</div>
Run Code Online (Sandbox Code Playgroud)
JS代码
$(".container").click(function(){
console.log($(this).find(".editemail").show()); //Note it works fine If i didn't load any new elements into the div.
});
Run Code Online (Sandbox Code Playgroud)
控制台日志输出在将新元素加载到容器之前.
<input type="text" onkeydown="saveFn(event,this)" name="editemail" class="editemail" style="height: 20px; " value="hyther@zohocorp.com" title="press enter to save">
Run Code Online (Sandbox Code Playgroud)
将元素加载到容器后的控制台日志输出.
<input type="text" onkeydown="saveFn(event,this)" name="editemail" class="editemail" style="height: 20px; display: none; " value="saravanan@zohocorp.com" title="press enter to save">
Run Code Online (Sandbox Code Playgroud)
尽管我也试图从这个元素中删除"style"属性并添加一个新的样式元素,但它仍然无效.
使用ASP.Net Web Api,您的GET端点返回与POST端点接受的不同类型是否正常/良好实践?
在许多情况下,我们想要在GET中返回的字段不能在POST中设置,例如"LastUpdated","Total"等,需要不同的类型?
例如,GET返回ReservationForGetModel,而POST接受ReservationForCreateModel:
public class ReservationController : ApiController {
...
public HttpResponseMessage Get(int id) {
Reservation reservation = _reservationService.Get(id);
//map Reservation to ReservationForGetModel
//return ReservationForGetModel
}
public HttpResponseMessage Post(ReservationForCreateModel reservation) {
//create reservation using ReservationForCreateModel here
//return 201 with location header set
}
}
Run Code Online (Sandbox Code Playgroud) c# ×4
javascript ×2
jquery ×2
asp.net-mvc ×1
fakeiteasy ×1
http ×1
jquery-ui ×1
rest ×1
unit-testing ×1
web-services ×1
widget ×1
xml ×1