我使用Metadata和JsonIgnore来删除序列化中的特殊字段.
[Authorize(Roles = "admin")]
public class UserController : ApiController
{
public IEnumerable<user> Get()
{
using (var mydb = new ModelContainer())
{
return mydb.userSet.ToList();
}
}
}
[MetadataType(typeof(user_Metadata))]
public partial class user
{
private class user_Metadata
{
[JsonIgnore]
public virtual password { get; set; }
public virtual adminFile { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
如何动态控制应该序列化哪个字段.对于某些事情
public partial class user
{
private class user_Metadata
{
[JsonIgnore]
public virtual password { get; set; }
[Roes == admin?JsonIgnore:JsonNotIgnore] //some thing like this
public virtual …Run Code Online (Sandbox Code Playgroud) 我可以轻松地在C#中获得异步设计
HttpResponseMessage response = await httpClient.GetAsync(InputAddress.Text);
{
....// run when request finished. And response closely relation to request.
}
Run Code Online (Sandbox Code Playgroud)
但是我怎么能在QT中做到这一点?我在下面找到一些代码.但还是有些问题.
如果我想在请求完成时运行一些代码(如上面的c#代码),我该怎么做?我想我可以将UUID绑定到每个请求或绑定一个回调函数指针来请求?最好的方法是什么?
QNetworkAccessManager *manager=new QNetworkAccessManager(this);
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(requestFinished(QNetworkReply*)));
QNetworkRequest request(QUrl(serverUrl));
QNetworkReply *sentReply = manager->post(request, buffer.toUtf8());
void requestFinished(QNetworkReply *reply)
{
QByteArray msg = reply->readAll();
if (sentReply == reply)
qDebug("this is it");
}
Run Code Online (Sandbox Code Playgroud)根据qlogging.h
#define qDebug QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug
Run Code Online (Sandbox Code Playgroud)
但是当我使用这样的时候,文件,行,功能名称都没有显示出来.
qDebug()<< "abc"; // only show abc;
qDebug()<< ""; // show nothing;
Run Code Online (Sandbox Code Playgroud)
我搜索了一会儿,似乎没有人像我上面那样有问题.
我使用ubuntu14.04,g ++版本4.8.2,从git构建qt5.3.
这是我的HttpService类,它工作正常.但它看起来很奇怪,每个函数中的代码都大致相同.我应该在每个函数中编写try/catch,特别是TaskCanceledException,如果我没有在这里捕获它,我的应用程序将终止.有人能给我一些关于如何优化代码的例子吗?
[Export(typeof(IDataSource))]
public class HttpService : IDataSource
{
HttpClient client = new HttpClient();
public HttpService()
{
client.BaseAddress = new Uri("https://localhost:3721");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public void Initialize(CurrentUser currentUser)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes(currentUser.Name + ":" + currentUser.Password)));
}
public async Task<IEnumerable<User>> getUsers()
{
try
{
var response = await client.GetAsync("api/User");
//response.EnsureSuccessStatusCode(); // Throw on error code.
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<IEnumerable<User>>();
return result;
}
else
{
return null;
}
}
catch (Newtonsoft.Json.JsonException ex)
{
Console.WriteLine(ex.ToString());
}
catch …Run Code Online (Sandbox Code Playgroud) 应用程序的窗口没有边框,所以右边没有退出按钮?如何以正确的方式关闭它?
这是我的方式,fisrt将命令绑定到自定义退出按钮.
<Button Content="Exit" HorizontalAlignment="Left" Margin="327,198,0,0" VerticalAlignment="Top" Width="75" Command="{Binding ExitCommand}"/>
Run Code Online (Sandbox Code Playgroud)
单击按钮时在ViewModel中抛出异常.
class ViewModel:NotificationObject
{
public ViewModel()
{
this.ExitCommand = new DelegateCommand(new Action(this.ExecuteExitCommand));
}
public DelegateCommand ExitCommand { get; set; }
public void ExecuteExitCommand()
{
throw new ApplicationException("shutdown");
}
}
Run Code Online (Sandbox Code Playgroud)
在Application类中捕获异常
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Bootstrapper bootstrapper = new Bootstrapper();
AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
try
{
bootstrapper.Run();
}
catch (Exception ex)
{
HandleException(ex);
}
}
private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) …Run Code Online (Sandbox Code Playgroud)