我正在使用.net web api获取json并将其返回到前端以获得角度.json可以是对象或数组.我的代码目前只适用于数组而不是对象.我需要找到一种方法来tryparse或确定内容是对象还是数组.
这是我的代码
public HttpResponseMessage Get(string id)
{
string singleFilePath = String.Format("{0}/../Data/phones/{1}.json", AssemblyDirectory, id);
List<Phone> phones = new List<Phone>();
Phone phone = new Phone();
JsonSerializer serailizer = new JsonSerializer();
using (StreamReader json = File.OpenText(singleFilePath))
{
using (JsonTextReader reader = new JsonTextReader(json))
{
//if array do this
phones = serailizer.Deserialize<List<Phone>>(reader);
//if object do this
phone = serailizer.Deserialize<Phone>(reader);
}
}
HttpResponseMessage response = Request.CreateResponse<List<Phone>>(HttpStatusCode.OK, phones);
return response;
}
Run Code Online (Sandbox Code Playgroud)
以上可能不是这样做的最佳方式.它就在我现在的位置.
我希望能够捕捉WebFaultException<string>
到最具体的一个WebFaultException<T>
(T是任何其他类型)作为更一般的情况来处理.这可能吗?
try
{
// throw exception
}
catch (WebFaultException<string> e)
{
// handle more specific type
}
catch (WebFaultException<T> e)
{
// handle more general type
}
catch (Exception e)
{
}
Run Code Online (Sandbox Code Playgroud) 可以push
在Perl中使用数组引用吗?谷歌搜索建议我首先尊重数组,但这并没有真正起作用.它推送到deferenced数组,而不是引用的数组.
例如,
my @a = ();
my $a_ref = [@a];
push(@$a_ref,"hello");
print $a[0];
Run Code Online (Sandbox Code Playgroud)
@a
将不会更新,此代码将失败,因为该数组仍为空
(我还在学习Perl参考文献,所以这可能是一个非常简单的问题.如果有的话,很抱歉)
我正在重新实现一个请求记录器作为Owin Middleware,它记录所有传入请求的请求URL和正文.我能够读取正文,但是如果我在控制器中执行body参数则为null.
我猜它是空的,因为流的位置在最后,因此在尝试反序列化主体时没有什么可读的.我在以前版本的Web API中遇到了类似的问题,但是能够将Stream位置设置回0.这个特定的流引发This stream does not support seek operations
异常.
在最新版本的Web API 2.0中,我可以Request.HttpContent.ReadAsStringAsync()
在我的请求记录器内部调用,并且主体仍然可以直接到达控制器.
如何在阅读后回放流?
要么
如何在不消费的情况下阅读请求体?
public class RequestLoggerMiddleware : OwinMiddleware
{
public RequestLoggerMiddleware(OwinMiddleware next)
: base(next)
{
}
public override Task Invoke(IOwinContext context)
{
return Task.Run(() => {
string body = new StreamReader(context.Request.Body).ReadToEnd();
// log body
context.Request.Body.Position = 0; // cannot set stream position back to 0
Console.WriteLine(context.Request.Body.CanSeek); // prints false
this.Next.Invoke(context);
});
}
}
Run Code Online (Sandbox Code Playgroud)
public class SampleController : ApiController
{
public void Post(ModelClass …
Run Code Online (Sandbox Code Playgroud) 朋友我有很多由DBA创建的表.我希望每个表有实体类进一步查询所以任何人都建议如何使用eclips自动创建?
我试图过滤一个列表,这样就会产生一个只有布里斯班郊区的列表?
C#
Temp t1 = new Temp() { propertyaddress = "1 russel street", suburb = "brisbane" };
Temp t2 = new Temp() { propertyaddress = "12 bret street", suburb = "sydney" };
List<Temp> tlist = new List<Temp>();
tlist.Add(t1);
tlist.Add(t2);
List<Temp> tlistFiltered = new List<Temp>();
//tlistFiltered. how to filter this so the result is just the suburbs from brisbane?
public class Temp
{
public string propertyaddress { get; set; }
public string suburb { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我A
上课了:
public abstract class A
{
}
Run Code Online (Sandbox Code Playgroud)
然后我有B
一个源于它的课程:
public sealed class B : A
{
public void SomeMethod()
{
var method = this.GetType().GetMethod("AddText");
}
private void AddText(string text)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
为什么GetMethod
返回null?
我发现了很多关于SEQUENCE(OF)ASN.1数据类型的标记值的矛盾信息:
维基百科声称它是0x10和0x30:
http://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One - > 0x30
根据微软的说法,它是0x30:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb540799%28v=VS.85%29.aspx
在Bouncycastle的资料中可以找到:
public const int Sequence = 0x10;
Run Code Online (Sandbox Code Playgroud)
所以它基本上是0x10和0x30之间的联系.你知道真正的价值是或者我错过了什么吗?
我是 AWS 新手。我无法清楚了解 ALB 与 NLB 的区别。有人可以简单地解释一下吗?
这是一个糟糕的设计标志,你有很多只有1或2种方法的课程吗?
我正在尝试学习OOP设计并创建了一个小应用程序(微小的).
它最终有很多类实现只包含1或2个方法的接口.
它感觉分离得很好但是这些类的方法很少看起来很糟糕.
我知道每个场景都会有所不同,但从一般的角度来看这是不是很糟糕?
应用程序的一小部分确定喂狗的时间表(我知道跛脚):
所以我试着在这里实施策略模式:
class DogFeedController
{
protected $strategy = null;
public function __construct(iDogFeedStrategy $strategy) {
$this->strategy = $strategy;
}
public function getFeedingSchedule() {
$morningFeeds = $this->strategy->generateMorningFeeds();
$eveningFeeds = $this->strategy->generateEveningFeeds();
}
}
class GeneralFeedStrategy implements iDogFeedStrategy
{
public function generateMorningFeeds() {
//do stuff here
}
public function generateEveningFeeds() {
//do stuff here
}
}
Run Code Online (Sandbox Code Playgroud)