我有一个webAPI 2控制器,目前它正在抛出异常,并且这是在响应消息中发送回客户端.500内部服务器错误,它的自我序列化的例外,我可以在fiddler中看到它的json表示.(json粘贴在下面)
我一直在尝试找到一种方法将此异常反序列化为.NET异常对象,但不断收到错误:
"找不到会员'ClassName'."
目前在我的客户端我试图通过以下代码反序列化异常.
if (apiResponse.ResponseCode.Equals(500)) // Unhandled exception on server
{
var exceptionObject = await response.Content.ReadAsAsync<Exception>();
}
Run Code Online (Sandbox Code Playgroud)
如何在客户端应用程序上正确反序列化此异常对象?理想情况下,我应该能够取回原始的System.InvalidOperationException`对象.
下面是我试图在fiddler中捕获的反序列化的响应:
HTTP/1.1 500内部服务器错误缓存控制:无缓存Pragma:no-cache内容类型:application/json; charset = utf-8 Expires:-1 Server:Microsoft-IIS/10.0 X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET Date:Wed,30 Sep 2015 13:43:42 GMT Content-Length :1556
{"Message":"发生了错误.","ExceptionMessage":"UrlHelper.Link必须不返回null.","ExceptionType":"System.InvalidOperationException","StackTrace":"在System.Web.Http. Results.CreatedAtRouteNegotiatedContentResult
1.Execute()\r\n at System.Web.Http.Results.CreatedAtRouteNegotiatedContentResult1.ExecuteAsync(CancellationToken cancellationToken)\ r \n在System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\ r \n ---从抛出异常的上一个位置的堆栈跟踪结束---\r \n \n System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \n在System.Web.Http.Controllers.ActionFilterResult.d__上的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \n中.MoveNext()\ r \n ---从抛出异常的上一个位置开始的堆栈跟踪结束---在系统中的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \n中的\ r \n. System.Web.Http.Filters.AuthorizationFilterAttribute.d__2中的Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \n.MoveNext()\ r \n ---从抛出异常的上一个位置开始的堆栈跟踪结束---在System.Runtime上的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \n中的\ r \n \n \n System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}中的.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \n
我有一个linq查询,其结果我在foreach循环中迭代.
第一个是从表布局面板中获取控件集合的查询,然后迭代集合并从tableLayoutPanel中删除控件:
var AllItems = (from Item in this.tableLayoutPanel1.Controls.OfType<ItemControl>()
select Item);
foreach (ItemControl item in AllItems)
{
Trace.WriteLine("Removing " + item.ToString());
this.tableLayoutPanel1.Controls.Remove(item);
item.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
上面没有按照我的预期进行(即抛出一个错误),它只删除了一半的控件(ODD编号为1),看起来每次迭代时AllItems都会减少它的自身,虽然底层集合被修改但没有错误是抛出.
如果我用一串字符串做同样的话:
string[] strs = { "d", "c", "A", "b" };
List<string> stringList = strs.ToList();
var allitems = from letter in stringList
select letter;
foreach (string let in allitems)
{
stringList.Remove(let);
}
Run Code Online (Sandbox Code Playgroud)
这次Visual Studio抛出一个错误(正如预期的那样)抱怨底层集合已经改变.
为什么第一个例子不会爆炸呢?
有一些关于Iterators/IEnumerable的东西,我在这里不理解,我想知道是否有人可以帮助我了解linq和foreach引擎盖下发生了什么.
(我知道我可以通过AllItems.ToList()解决这两个问题;在迭代之前,但是想要理解为什么第二个例子抛出错误而第一个没有
我正在学习角度和打字稿.
我在这个服务中有一个CustomerService我有一个方法,我希望从RESTfull服务返回一组客户.
最初我创建了我的GetCustomers函数:
public GetCustomers(): Dtos.ICustomer[] {
var _customers: Dtos.ICustomer[];
this._httpService.get('http://localhost/myTestApi/api/customers/')
.success(function (data) {
_customers = data as Dtos.ICustomer[];
}).error(function (error) {
console.log(error);
});
return _customers;
}
Run Code Online (Sandbox Code Playgroud)
这个函数最终得到了客户,但显然它会在httpservice实际获取数据之前返回_customers.
此时我以为我可以使用Typscript async/await,这就是我在一团糟中结束的时候.
我想写这样的函数:
public async GetCustomers(): Dtos.ICustomer[] {
var _customers: Dtos.ICustomer[];
await this._httpService.get('http://localhost/myTestApi/api/customers/')
.success(function (data) {
_customers = data as Dtos.ICustomer[];
}).error(function (error) {
console.log(error);
});
return _customers;
}
Run Code Online (Sandbox Code Playgroud)
我立即得到此错误:错误TS1055类型'Dtos.ICustomer []'不是有效的异步函数返回类型.
我找到了这个Async/Await,简单的例子(typescript)
但它使用Promise对象:返回新的Promise
如果我尝试重写我的GetCustomers方法签名:
public async GetCustomers(): Promise<Dtos.ICustomer[]> {}
Run Code Online (Sandbox Code Playgroud)
我得到并且错误:
找不到名字'承诺'
我需要导入一些东西来获得承诺吗?
我在MVC 6 WebAPI中调用api端点:
POST http://localhost:57287/mytestapi/testentity/ HTTP/1.1
Accept: application/json
X-APIKey: 00000000-0000-0000-0000-000000000000
Content-Type: application/json; charset=utf-8
Host: localhost:57287
Content-Length: 1837
Expect: 100-continue
Connection: Keep-Alive
Run Code Online (Sandbox Code Playgroud)
在身体我有json序列化测试实体.
我的实体控制器代码中有一个错误,api返回500响应'服务器错误'我知道错误是什么将修复它,但我需要一些帮助的问题是API返回HTML而不是json序列化异常对象 - Json是我所期望的:它是旧webapi将返回的内容.我已经从我知道可行的旧测试项目中移植了编码.
那么为什么MVC 6 WebAPI返回html而不是json?我需要做一些配置吗?
编辑:我添加了Accept:application/json到@danludwig建议的标题,但是这没有解决问题,我仍然有一个html错误页面.
我查看了我的StartUp.cs并发现:
if (env.IsDevelopment())
{
//app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
Run Code Online (Sandbox Code Playgroud)
在ConfigureApp方法中.我用app.UseDeveloperExceptionPage()进行了测试; 评论说.这阻止了在api响应体中返回html错误页面,但是我仍然没有得到json序列化的异常对象.
我正在寻找一种方法来获取缓存到本地 OneDrive 文件夹的文件的 OneDrive 文件 URL?我唯一能想到的就是为我拥有的每个 OneDrive 文件夹硬编码一些根 URL,但这看起来很糟糕!
有谁知道任何 OneDrive 客户端 API 可以根据本地文件路径查询 URL?
我的用例:我正在尝试附加并打开 Excel 工作簿的实例。我曾经能够做到这一点Marshal.BindToMoniker(_workbookPath);
但是,Excel 现在似乎在 ROT 中注册 OneDrive URL,而不是本地文件路径。这种情况发生在为 Excel 2016 带来新的自动保存功能的更新中,我认为这似乎是我现有代码崩溃的时间。
这里有一个类似的未解答的问题:C# OneDrive for Business / SharePoint: get server path from localsynced file
我有一个列表视图控件与分组和排序.
组标题是按降序排列的日期.
我试图找出如何按升序排序每个组头下的分组项目,但无法弄清楚如何完成它或者甚至可以使用ListView.
这是我到目前为止的XAML.
注意:ScheduledItemSearchResults是一个可观察的ScheduleItem集合,每个项目都有Title和ScheduleDate属性.
<Grid x:Name="TxScheduleItemResults"
Grid.Column="1">
<Grid.Resources>
<CollectionViewSource Source="{Binding ScheduledItemSearchResults}" x:Key="scheduledItems">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Value.ScheduleDateTime" Direction="Descending"/>
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<dat:PropertyGroupDescription PropertyName="Value.ScheduleDateTime.Date" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Grid.Resources>
<ListView x:Name="ScheduledItemResultsList"
Style="{StaticResource TransparentListViewStyle}"
ItemContainerStyle="{StaticResource alternatingListViewItemStyle}"
AlternationCount="2"
ItemsSource="{Binding Source={StaticResource scheduledItems}}"
>
<ListView.View>
<GridView>
<GridViewColumn Header="Scheduled Items"
Width="{Binding ElementName=ScheduledItemResultsList, Path=ActualWidth}"
>
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource ModuleGroupHeader}"
Text="{Binding}"
/>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Value.Title}" Width="200"/>
<TextBox Text="{Binding Value.ScheduleDateTime, StringFormat={}{0:HH:mm:ss}}" Width="120"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter …Run Code Online (Sandbox Code Playgroud) 作为一个简单的例子,我有一个WPF应用程序,主窗口上有一个按钮,因此代码背后:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
async void Button_Click(object sender, RoutedEventArgs e)
{
await Task<bool>.Run(() => this.DoOnThread());
}
async Task<bool> DoOnThread()
{
Thread.CurrentThread.Name = "MyTestThread";
Thread.Sleep(1000);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我通过VisualStudio线程窗口中断"返回true"我可以获取ThreadID,如果我继续并让代码运行完成并等待一段时间直到线程退出,我得到"线程0x9ad34已经退出代码259(0x103) )"显示在VS输出窗口中.
我做错了什么,如何确保线程退出代码为0?
我有一个DTO对象,它有一个Date参数.我将此Dto包装在视图模型对象中,然后我将视图中的属性绑定到标签.
<label class="form-control">{{controller.ViewModel.Date}}</label>
Run Code Online (Sandbox Code Playgroud)
在视图模型中,我有一个吸气剂.(我正在使用TypeScript)
public get Date(): Date {
return new Date(Date.parse(this.dto.Date));
//return moment(this.dto.Date).toDate();
}
Run Code Online (Sandbox Code Playgroud)
发出的JavaScript:
Object.defineProperty(ViewModel.prototype, "Date", {
get: function () {
return new Date(Date.parse(this.dto.Date));
},
enumerable: true,
configurable: true
});
Run Code Online (Sandbox Code Playgroud)
我相信原因是因为我在getter和angular中创建一个新的Date认为这意味着日期总是新的并且它一直保持日期直到模型稳定,从而导致无限循环.
为什么棱角分明这样做?
为什么它一遍又一遍地调用getter,只调用一次有什么问题?
我可以告诉角度只是调用一次吸气剂并接受它给出的值吗?
我一直在尝试使用MVC WebAPI,非常酷的东西.但我正在努力解决路线问题.
作为一个例子,我有一个webAPI项目结构,如下所示:
项目:
目前我在WebApiConfig.cs中定义了一个API路由
config.Routes.MapHttpRoute(
name: "CustomerApi",
routeTemplate: "api/customer/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
Run Code Online (Sandbox Code Playgroud)
当我只有客户相关的控制器时,这工作正常.所以我可以打电话:
但是现在我已经添加了与配置相关的产品相关控制器(当然)以获得我必须调用Uri的产品:
相反,我想:
对于产品类别,我想要类似于:
我认为我必须做的就是添加更多路线,但是我无法找到如何将各种控制器与我希望的路线联系起来?
如果我确实添加了另一条路线,我最终会将两条不同的uri路由到我建立的每个控制器.
如何实现我想要的逻辑分区?
我已经构建了一个运行自托管SignalR服务的Windows服务.
我启动了webApp:WebApp.Start("http://*:1111")
要开始这个我必须提供我的服务管理员登录.但这似乎有点太过特权了.
我尝试使用NETWORK SERVICE,但获得访问被拒绝错误.
为所提供的URL启动WebApp所需的最低权限是多少.或者我必须使用管理员权限运行我的服务吗?