小编Rom*_*syk的帖子

无法使用X509Certificate2找到指定的对象

我正在尝试加载包含要访问的密钥的文件Google Calendar API.遵循本教程. 为此,我创建了这段代码:

var certificate = new X509Certificate2("client_secret.json", "notasecret", X509KeyStorageFlags.Exportable);
Run Code Online (Sandbox Code Playgroud)

client_secret.json在我的解决方案中上传了文件,这是文件的路径:"...\Visual Studio 2015\Projects\Calendar\Calendar\bin\Debug\client_secret.json"

但似乎代码无法找到该文件并返回此错误:

无法使用X509Certificate2找到指定的对象

我还在文件Always copy上设置了Copy in the output directory要读取的属性.

c# x509certificate2

7
推荐指数
1
解决办法
1705
查看次数

如何使用Paginate方法

我在Web API项目中遇到了PaginatedList的问题.

在存储库中有一个方法,如:

public virtual PaginatedList<T> Paginate<TKey>(int pageIndex, int pageSize,
    Expression<Func<T, TKey>> keySelector,
    Expression<Func<T, bool>> predicate,
    params Expression<Func<T, object>>[] includeProperties)
{
    IQueryable<T> query = AllIncluding(includeProperties).OrderBy(keySelector);

    query = (predicate == null)
        ? query
        : query.Where(predicate);

    return query.ToPaginatedList(pageIndex, pageSize);
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用它时,像这样:

var a = repository.Paginate<Region>(pageNo, pageSize, x => x.ID, null);
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

无法将类型'int'隐式转换为'Domain.Entities.Dictionaries.Region'

我究竟做错了什么?

c# asp.net-mvc asp.net-web-api

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

UTF8 byte []到字符串转换

我有byte[]无限大小的UTF8 (即非常大的尺寸).我想将其截断为1024仅字节,然后将其转换为字符串.

Encoding.UTF8.GetString(byte[], int, int)这样做对我来说.它首先缩短1024字节,然后给我转换后的字符串.

但是在这个转换中,如果最后一个字符是UTF8字符集,它由2个字节组成,并且其第一个字节落在范围内而另一个字节超出范围,那么它将?在转换后的字符串中显示该字符.

有什么方法可以?让它不会被转换成字符串吗?

c# string utf-8 type-conversion

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

webapi 2全局异常处理,controller-context为null

在WebAPI 2全局异常处理程序中,我试图从抛出错误的位置获取控制器对象的引用.

下面是它的代码:

public class CustomExceptionHandler : ExceptionHandler
{
     public override void Handle(ExceptionHandlerContext context)
     {
         var controller = context.ExceptionContext.ControllerContext;
         var action = context.ExceptionContext.ActionContext;

         //.....some code after this
     }
}
Run Code Online (Sandbox Code Playgroud)

controller以及action上面的变量都是空的.

任何指针为什么这样?

c# error-handling asp.net-web-api2

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

Swashbuckle:使非空属性成为必需

在ASP.NET Core Web应用程序中使用Swashbuckle.AspNetCore,我们具有以下响应类型:

public class DateRange
{
    [JsonConverter(typeof(IsoDateConverter))]
    public DateTime StartDate {get; set;}

    [JsonConverter(typeof(IsoDateConverter))]
    public DateTime EndDate {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

当使用Swashbuckle发出不拘一格的API JSON时,它将变为:

{ ...

  "DateRange": {
    "type": "object",
    "properties": {
      "startDate": {
        "format": "date-time",
        "type": "string"
      },
      "endDate": {
        "format": "date-time",
        "type": "string"
      }
    }
  }
...
}
Run Code Online (Sandbox Code Playgroud)

这里的问题在于这DateTime是一个值类型,并且永远不能为null。但发出的Swagger API JSON不会将2个属性标记为required。所有其他值类型的行为相同:int,long,byte等-它们都被视为可选值。

为了完成图片,我们将Swagger API JSON馈送到dtsgenerator来为JSON响应模式生成Typescript接口。例如,上面的类变为:

export interface DateRange {
    startDate?: string; // date-time
    endDate?: string; // date-time
}
Run Code Online (Sandbox Code Playgroud)

这显然是不正确的。在深入研究这一点之后,我得出结论,dtsgenerator在使不需要的属性在打字稿中可为空方面做了正确的事情。也许摇摇欲坠的规范需要对nullable vs required的明确支持,但是现在将2混为一谈。

我知道我可以[Required] …

c# asp.net-web-api swagger swashbuckle asp.net-core

6
推荐指数
3
解决办法
2564
查看次数

不支持.Net core 2.0中的log4net AdoNetAppender?

我执行log4net的AdoNetAppenderasp.net 2.0核心,但我想它是不支持.我已经在核心2.0中实现了log4net RollingFileAppender,它使用log4.net config成功运行.那么,如果log4net AdoNetAppender在核心2.0中不支持,有没有其他方法将日志插入到核心2.0中的sql数据库?

谢谢

c# adonetappender log4net-appender asp.net-core-2.0

6
推荐指数
2
解决办法
3623
查看次数

ORA-12899:列值太大

我以feed的形式从erp系统获取数据,特别是feed中的一列长度仅为15.

在目标表中也对应列也是长度varchar2(15) 但是当我尝试将其加载到db时它显示错误如:

ORA-12899:列emp_name的值太大(实际:16,最大值:15)

我不能增加列长度,因为它是生产中的基表.

oracle

5
推荐指数
2
解决办法
7万
查看次数

运行存储过程后刷新存储库

我在我的WebAPI项目中执行以下操作,该项目运行存储过程以更新(以及其他一些操作)People表:

public HttpResponseMessage SetAsMain(int id)
{
    People people = repository.GetById(id);

    if (people == null)
    {
        return ErrorMsg(HttpStatusCode.NotFound, string.Format("No people with ID = {1}", id));
    }

    if (people.IsMain)
    {
        return ReturnMsg(HttpStatusCode.OK, string.Format("{0} is already set as main", people.FullName)); 
    }

    try
    {
        var parameters = new Dictionary<string, string>();
        parameters.Add("PeopleID", id.ToString());                

        repository.ExecProcedure("usp_PeopleSetMain", parameters);
        return Request.CreateResponse(HttpStatusCode.OK, repository.GetById(id)); 
    }
    catch (Exception ex)
    {
        return ErrorMsg(HttpStatusCode.InternalServerError, ex.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在使用存储过程更新后检索数据时遇到问题.在这个地方:

return Request.CreateResponse(HttpStatusCode.OK, repository.GetById(id));
Run Code Online (Sandbox Code Playgroud)

我得到的数据与更新前的数据相同.但是当我再次调用这个方法时,会有一个实际的数据.

如何刷新存储库中的数据?

你能告诉我为什么repository.GetById(id)从来没有取过新的数据ExecProcedure.如果我需要添加更多信息,请告诉我


我的存储库中的UPDATE方法GetById:

public …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net-mvc entity-framework asp.net-web-api

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

如何在java中替换"{Name}"

我需要用值替换字符串中的{Name}值.
我们怎样才能更换特殊字符{}
我试过这个:

str.replaceAll("{Name}","A");
Run Code Online (Sandbox Code Playgroud)

但如果我们有特殊字符,这不起作用.

java special-characters

5
推荐指数
2
解决办法
1050
查看次数

如何在 Swagger 中隐藏 OData 元数据控制器?

将我的项目更新到 .NET6 并将 OData 更新到 8.0.4 后,出现了一个具有这些端点的新元数据控制器:

在此输入图像描述

我想以某种方式禁用它或将其从我的服务中删除。

添加OData服务的代码:

启动.cs

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddControllers(mvcOptions => mvcOptions.EnableEndpointRouting = false)
        .AddOData(opt => opt.AddRouteComponents("", GetEdmModel()).Select().Expand());
}
Run Code Online (Sandbox Code Playgroud)

版本:

  • .NET6
  • ASP.NET 核心 6
  • 奥数据8.0.4

.net c# odata asp.net-core .net-6.0

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