小编Ral*_*ing的帖子

Ok()方法新的ObjectResult()之间有什么区别吗?

场景:在.net核心控制器上实现标准REST API/GET方法.

文档指出OkObjectResult是一个状态为200的ObjectResult.这可以通过Ok(myResult)从ControllerBase继承的方法获得.我认为这是一种方便的方法.

但是,本教程没有使用这种方法 - 而是返回new ObjectResult(myResult)默认为状态200的方法.

这两种方法有什么区别吗?

c# asp.net-core-mvc asp.net-core

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

如何在MailKit中将邮件标记为已读

我使用MailKit从GMail帐户中读取一些消息.工作得很好,但是当我的应用程序读取了一条消息时,我想将消息标记为已读,并将该状态保存到GMail.MailKit可以实现吗?我还没有发现任何相关信息.

最好的问候René

c# mailkit

21
推荐指数
1
解决办法
6642
查看次数

计算.NET核心项目的代码度量标准?

我正在玩ASP.NET核心和.NET核心项目.对于经典的C#项目,Visual Studio 2015具有计算代码度量的功能.对于.NET Core,预览2工具中缺少支持.

在工具更完整之前,有没有人知道解决方法?

c# visual-studio visual-studio-2015 .net-core

20
推荐指数
1
解决办法
6589
查看次数

在ASP.NET Core中的_Layout.cshtml中访问cookie

我正在尝试在登录成功时将身份验证密钥存储到我的cookie中:

HttpContext.Response.Cookies.Append("Bearer", accessToken, cookieMonsterOptions);
Run Code Online (Sandbox Code Playgroud)

所以在控制器类中这是有效的.我可以轻松创建和阅读我的cookie.但是现在我想检查一下,如果它存在,请读取我的cookie的值,_Layout.cshtml并显示登录用户的名称 - 或登录的链接.但是如何在部分中读取我的cookie _Layout.cshtml

string value = HttpContext.Request.Cookies.Get("Bearer");
Run Code Online (Sandbox Code Playgroud)

不起作用.它试图添加System.Web到我的使用或者说HttpContext是静态的并且需要访问的引用Request.

有什么建议或想法吗?

c# asp.net authentication cookies asp.net-core

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

如何在asp.net中的动作过滤器中添加参数?

我有以下过滤器属性,我可以像这样将字符串数组传递给属性[MyAttribute("string1", "string2")].

public class MyAttribute : TypeFilterAttribute
{
    private readonly string[] _ids;

    public MyAttribute(params string[] ids) : base(typeof(MyAttributeImpl))
    {
        _ids = ids;
    }

    private class MyAttributeImpl : IActionFilter
    {
        private readonly ILogger _logger;

        public MyAttributeImpl(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<MyAttribute>();
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            // HOW DO I ACCESS THE IDs VARIABLE HERE ???
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何将字符串数组传递_ids给动作过滤器的实现?我错过了一些非常明显的东西!?

c# custom-action-filter actionfilterattribute asp.net-core-mvc asp.net-core

16
推荐指数
1
解决办法
9075
查看次数

使用JWT身份验证的RestSharp不起作用

这是我"学习"如何操作的页面:https://stormpath.com/blog/token-authentication-asp-net-core

但对我来说这不起作用(也不适用于Fiddler)我的ApplicationUser模型有这个控制器:

[Authorize] //works when it's not set, doesn't work when it's set
[Route("api/[controller]")]
public class ApplicationUserController : Controller
{
    private IRepository<ApplicationUser> _applicationUserRepository;

    public ApplicationUserController(IRepository<ApplicationUser> applicationUserRepository)
    {
        _applicationUserRepository = applicationUserRepository;
    }

    [HttpGet("{id}")]
    public ApplicationUser Get(int id)
    {
        return _applicationUserRepository.Get(id);
    }
}
Run Code Online (Sandbox Code Playgroud)

并且我的RestSharp包装器可以获取所有应用程序用户:

public Task<T> GetResponseContentAsync<T>(string resource, int id) where T : new()
{
    RestRequest request = new RestRequest($"{resource}/{{id}}", Method.GET);
    request.AddUrlSegment("id", id);
    if (!AuthenticationToken.IsNullOrEmpty(true))
    {
        request.AddHeader("Authorization", string.Format("Bearer {0}", AuthenticationToken));
        _client.Authenticator = new JwtAuthenticator(AuthenticationToken);
        _client.Authenticator.Authenticate(_client, request);
    }

    TaskCompletionSource<T> tcs …
Run Code Online (Sandbox Code Playgroud)

c# restsharp access-token asp.net-web-api2 asp.net-core

14
推荐指数
1
解决办法
3769
查看次数

如何在Visual Studio中测量ASP.NET Core项目中的代码覆盖率?

我想在ASP.NET Core应用程序中测量我的XUnit-Tests的代码覆盖率.Visual Studio 2015中的.NET Core工具是预览2,目前代码覆盖率不起作用.

2月份的博客文章http://dotnetthoughts.net/measuring-code-coverage-of-aspnet-core-applications-using-opencover/通过使用打开封面的命令行显示了一种解决方法.我正在寻找Visual Studio内部更加集成的方式.

有没有人听说过与XUnit结合使用的更好/更集成的代码覆盖率测量方法?

c# code-coverage visual-studio-2015 .net-core asp.net-core

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

.Net Core WebAPI,无法使用POSTMAN发布数据,错误 - 415不支持的MediaType

我正在使用Postman测试我的第一个.net Core WebAPI

发生未知的媒体类型错误.

我错过了什么?

这是邮递员休息客户端

这是我的发布对象

public class Country
{
    [Key]
    public int CountryID { get; set; }
    public string CountryName { get; set; }
    public string CountryShortName { get; set; }
    public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是webapi控制器

[HttpPost]
public async Task<IActionResult> PostCountry([FromBody] Country country)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    _context.Country.Add(country);
    try
    {
        await _context.SaveChangesAsync();
    }
    catch (DbUpdateException)
    {
        if (CountryExists(country.CountryID))
        {
            return new StatusCodeResult(StatusCodes.Status409Conflict);
        }
        else
        {
            throw;
        }
    }

    return CreatedAtAction("GetCountry", new { …
Run Code Online (Sandbox Code Playgroud)

rest asp.net-web-api2 postman asp.net-core-mvc

12
推荐指数
1
解决办法
9542
查看次数

PHP从字符串中删除html标记

我有字符串:

<p justify;"="">Vers­lo cent­rai Lie­tu­vos ne­kil­no­ja­mo­jo turto pl?t­ros aso­cia­ci­jos kon­kur­se  ...</p>
Run Code Online (Sandbox Code Playgroud)

并希望删除标签

<p justify;"=""></p>
Run Code Online (Sandbox Code Playgroud)

我的代码:

$content = strip_tags($text, '<p>');
Run Code Online (Sandbox Code Playgroud)

但我得到空字符串:string(0) "",我做错了什么?

html php

9
推荐指数
2
解决办法
4万
查看次数

除LINQ中的LIKE条件外

我有一个包含文件路径的字符串列表.

List<string> allFilesWithPathList = new List<string>();

allFilesWithPathList.Add(@"G:\Test\A.sql");
allFilesWithPathList.Add(@"G:\Test\B.sql");
allFilesWithPathList.Add(@"G:\Test\C.sql");

return allFilesWithPathList;
Run Code Online (Sandbox Code Playgroud)

我有另一个列表,其中包含文件的子集 - 但它只有文件名; 不是路径.

List<string> excludeList = new List<string>();
excludeList.Add("B.sql");
Run Code Online (Sandbox Code Playgroud)

现在我需要从allFilesWithPathList获取excludeList中不存在的文件.目前,我EXCEPT在创建另一个仅包含文件名的列表后,正在执行以下操作.

List<string> allFileNamesOnlyList = new List<string>();
foreach (string fileNameWithPath in allFilesWithPathList)
{
    //Remove path and get only file name
    int pos = fileNameWithPath.LastIndexOf(@"\") + 1;
    string value = fileNameWithPath.Substring(pos, fileNameWithPath.Length - pos);
    allFileNamesOnlyList.Add(value);
}

//EXCEPT logic
List<string> eligibleListToProcess = allFileNamesOnlyList.Except(excludeList).ToList();
Run Code Online (Sandbox Code Playgroud)

LINQ不引入上述其他列表的情况下,使这种逻辑工作的最佳方法是什么?

注意:我正在使用 .Net 4.5

完整的代码

class Program
{
    static void Main(string[] args)
    {
        List<string> allFilesWithPathList …
Run Code Online (Sandbox Code Playgroud)

c# linq

9
推荐指数
2
解决办法
328
查看次数