小编tob*_*bre的帖子

在Web API中返回自定义错误对象

我有一个Web API,我正在使用MVC 4 Web API框架.如果有异常,我现在正在抛出一个新的HttpResponseException.即:

if (!Int32.TryParse(id, out userId))
    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid id")); 
Run Code Online (Sandbox Code Playgroud)

这会将对象简单地返回给客户端 {"message":"Invalid id"}

我希望通过返回更详细的对象来进一步控制对异常的响应.就像是

{
 "status":-1,
 "substatus":3,
 "message":"Could not find user"
 }
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?是序列化我的错误对象并在响应消息中设置它的最佳方法吗?

我也看了ModelStateDictionary一下,并提出了这个"黑客",但它仍然不是一个干净的输出:

var msd = new ModelStateDictionary();
msd.AddModelError("status", "-1");
msd.AddModelError("substatus", "3");
msd.AddModelError("message", "invalid stuff");
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, msd));
Run Code Online (Sandbox Code Playgroud)

编辑
看起来像HttpError我需要的自定义.这似乎可以解决问题,现在可以从我的业务层扩展它...

var error = new HttpError("invalid stuff") {{"status", -1}, {"substatus", 3}};
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, error));
Run Code Online (Sandbox Code Playgroud)

error-handling exception-handling asp.net-mvc-4 asp.net-web-api

31
推荐指数
2
解决办法
3万
查看次数

非交互模式下的Powershell

我使用Octopus进行部署.我有一个Powershell脚本的问题来控制部署:

# stops running processes
$processes = @("Notepad",
               "Firefox")
foreach ($process in $processes)
{
    $prc = Get-Process -Name $process -ErrorAction SilentlyContinue
    if (-not($prc -eq $null))
    {
        Write-Host "Stopping " $prc.ProcessName
        Stop-Process -InputObject $prc -ErrorAction SilentlyContinue
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图阻止的程序不是你在上面的脚本中看到的程序,但它们代表了我想要做的事情.现在我遇到的问题是,它在一台服务器上运行良好,但在另一台服务器上运行不正常.如果它不起作用,我收到错误消息:

停止处理:Windows PowerShell处于NonInteractive模式.读取和提示功能不可用.

适用的脚本在Powershell 3.0上运行,Powershell 3.0不适用于Powershell 2.0.我无法在任何地方升级到Powershell 3.0,因为旧服务器与Windows Server 2003一起运行.如何在PS 2.0上运行?

deployment powershell octopus-deploy

18
推荐指数
1
解决办法
3万
查看次数

如何在WPF中自动检测Flowdirection RightToLeft或LeftToRight

有没有办法根据使用的Unicode字符自动检测TextBox中的FlowDirection?

如果Excel中有阿拉伯文本,它将自动对齐到单元格的右侧.我想在我的WPF应用程序中使用相同的逻辑.有没有人经历过这个,可以告诉我该怎么做?

.net c# wpf direction

6
推荐指数
1
解决办法
1036
查看次数

System.IO.FileLoadException:无法加载文件或程序集Log4net

我在解决方案中添加了一个现有项目.当我使用MSTest运行程序运行所有单元测试时,我在几个测试中得到以下错误:

消息:测试方法soandso引发异常:System.IO.FileLoadException:无法加载文件或程序集'log4net,版本1.2.12.0,Culture = neutral,PublicKeyToken = 669e0ddf0bb1aa2a'或其中一个依赖项.定位的程序集的清单定义与程序集引用不匹配.(HRESULT异常:0x80131040)

我知道其他人也有同样的问题,关于这个话题还有其他问题和答案.但我尝试了很多东西,但没有任何帮助.我们使用的log4net版本是1.2.13.0.

我检查了FUSLOGVW.exe是否存在绑定错误.log4net显示添加的程序集和一些未知程序集.程序集中log4net的引用显示了随NuGet添加的版本1.2.13.0.所以它可能是一个依赖程序集导致所有这些麻烦.我尝试将log4net更改回版本1.2.12.0,但我仍然收到相同的错误消息.

当我使用MSTest testrunner运行所有测试时,会出现这些错误.当我只运行失败的测试时,它们会通过.当我单独运行它们时,它们也会通过.我尝试了绑定重定向,但我是为测试而不是测试程序集的程序集做的.我不知道如何为测试程序集做到这一点 - 没有配置.当我使用Resharper测试运行器运行测试时,它们也会通过(但其他测试失败).TFS Build服务器运行MSTest-runner,因此我需要使用MSTest.

有谁知道如何解决这个问题?

c# log4net fileloadexception fuslogvw nuget-package

6
推荐指数
1
解决办法
1万
查看次数

声纳未分析某些文件“文件中遇到无效字符”

我有几个文件未通过以下消息进行分析:

第9行的文件[具有完整路径的文件名]中遇到无效字符,无法对UTF-8进行编码。请修复文件内容或使用属性“ sonar.sourceEncoding”配置要使用的编码。

在Visual Studio中,当我选择“文件” /“高级保存选项”时,文件设置为“西欧(Windows)-代码页1252”。

我将其更改为Unicode(带签名的UTF-8)-代码页65001。

但是SonarQube仍然抱怨无效字符。“无效字符”是德语注释,带有Umlaut字符(ä,ö,ü)

我该怎么做才能解决此问题(不删除评论)?

c# code-analysis utf-8 sonarqube

5
推荐指数
1
解决办法
5528
查看次数

c#:Linq with Dictionary with multiple conditions

所以,我必须ListDictionary<string, object> ,我想解析1项与第2列名的条件:

如果我搜索列名为"Id"的1项,我会这样做:

var collection ....

var result = collection.OfType<Dictionary<string, object>>()
    .SelectMany(d => d.Where(x => x.Key == "id"))
    .Where(x => x.Value?.ToString() == "1234")
    .ToList();
Run Code Online (Sandbox Code Playgroud)

在这里,我搜索具有Id其值的列名称的项目,1234这可以正常工作.

现在我想添加一些条件:

我想搜索具有列名Id,值1234和列名的项目"Class",我想获取"Class"列名值.

有什么建议 ?

c# linq dictionary

4
推荐指数
1
解决办法
438
查看次数

从Java类连接到Oracle数据库的问题(ocijdbc11.dll)

我有一些数据库导入和导出ant脚本运行很少的java程序来导入和导出oracle 11g的数据.

这些脚本曾用于Vista 32位,但已停止使用Windows 7 64位.我可以使用ocijdbc11.dll的两个版本.

当我使用64位版本时,错误消息是:java.lang.UnsatisfiedLinkError:C:\ tools\oracle\ocijdbc11.dll:无法在IA 32位平台上加载AMD 64位.dll.

当我使用32位版本时,错误消息是:java.lang.UnsatisfiedLinkError:C:\ tools\oracle\ocijdbc11.dll:%1不是有效的Win32应用程序.

有谁知道我需要改变什么来解决这个问题?

java oracle connection dll

3
推荐指数
1
解决办法
5099
查看次数

让TextBox填充网格中的剩余空间

我有一个包含Grid的ListBox.Grid有一个Label Column,一个GridSplitter和一个TextBox Column.如何让Grid占用ListBox的整个宽度并使TextBox使用Grid Column的整个空间?

<Grid>
    <ListBox x:Name="myList"  Margin="4" ItemsSource="{Binding Path=Parameter}" MinHeight="60" Grid.IsSharedSizeScope="True" HorizontalAlignment="Stretch" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="parameterGrid" HorizontalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition x:Name="labelColumn" Width="Auto" SharedSizeGroup="LabelColumn"/>
                        <ColumnDefinition x:Name="splitterColumn" Width="5"/>
                        <ColumnDefinition x:Name="textColumn" Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Path=Key}" Grid.Column="0"/>
                    <GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
                    <TextBox Grid.Column="2" Text="{Binding Path=Value, UpdateSourceTrigger=LostFocus}"
                                                                 IsEnabled="{Binding ElementName=_view, Path=DataContext.IsEditEnabled}">
                    </TextBox>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)

参数只是为了证明这个想法:

public class Parameter
{ 
   public string Key { get; set; }
   public string Value { get; set; }

   public Parameter(string key, string value)
   {
       Key = …
Run Code Online (Sandbox Code Playgroud)

c# wpf grid textbox

3
推荐指数
1
解决办法
6135
查看次数

尝试激活时无法解析类型“System.Int32”的服务

在 ASP.NET Core 2.2 Razor Pages 网站中,我尝试从银行帐户列表导航到单个银行帐户的脚手架编辑页面。编辑页面打不开,并显示错误信息:

System.InvalidOperationException:尝试激活“ForexWeb.Pages.BankAccountModel”时无法解析类型“System.Int32”的服务。在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp,类型 type,类型 requiredBy,布尔 isDefaultParameterRequired)

银行账户页面 (BankAccounts.cshtml) 是

@page 
@model ForexWeb.Pages.BankAccountsModel

@{
    ViewData["Title"] = "BankAccounts";
    Layout = "~/Pages/Shared/_Layout.cshtml";
}

<h1>Bank Accounts</h1>

<table>
    @foreach (var account in Model.BankAccounts)
    {
        <tr>
            <td>@account.AccountNumber</td>
            <td>
                <a asp-page="Edit" asp-route-id="@account.Id">More</a>
                @*<a asp-action="Account/Edit" asp-route-id="@account.Id">Edit</a>*@
            </td>
        </tr>
    }
</table>
Run Code Online (Sandbox Code Playgroud)

编辑页面 (Edit.cshtml) 开头为

@page "{id}"
@model ForexWeb.Pages.BankAccountModel

@{
    ViewData["Title"] = "BankAccount";
    Layout = "~/Pages/Shared/_Layout.cshtml";
}

<h1>BankAccount</h1>
Run Code Online (Sandbox Code Playgroud)

路由在 Startup.cs 中设置

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage(); …
Run Code Online (Sandbox Code Playgroud)

c# url-routing razor-pages

2
推荐指数
1
解决办法
3085
查看次数