小编Bry*_*yan的帖子

Web Api Core 2 区分 GET

为什么 Web API Core 2 不能区分这些?

    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values?name=dave
    [HttpGet]
    public string Get(string name)
    {
        return $"name is {name}";
    }
Run Code Online (Sandbox Code Playgroud)

这是发生了什么 -

二者http://localhost:65528/api/valueshttp://localhost:65528/api/values?name=dave引起所述第一Get()方法来执行。

这个确切的代码在 Web Api 2 中工作正常。

知道有 多种方法可以解决这个问题,但我不知道为什么会发生这种情况。

有人可以解释为什么这已经改变了吗?

routing asp.net-web-api asp.net-web-api-routing asp.net-core-2.0

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

扩展方法没有设置值

我有一个看起来像这样的产品类 -

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有一个看起来像这样的扩展类

public static class ProductExtension
{
    public static void FixProduct(this Product product)
    {
        product = new Product(){Name = product.Name.ToUpper()};
        //product.Name is now UPPERCASE
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的主要方法中,我有 -

static void Main(string[] args)
{
    Product p = new Product() {ProductId = 1, Name = "steve"};
    p.FixProduct(); 
    System.Console.WriteLine(p.Name);
}
Run Code Online (Sandbox Code Playgroud)

这打印"steve"而不是我想要打印的内容:"STEVE".为什么扩展方法中的赋值不起作用?

c# extension-methods

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

组合根与服务定位器

我一直在阅读这两种解决依赖关系的方法,并找到了一些 ninject 实现的示例代码。

对于服务定位器遵循类似

 public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
 {
    IKernel kernel;

    public NinjectDependencyResolver(IKernel kernel)
        : base(kernel)
    {
        this.kernel = kernel;
    }

    public IDependencyScope BeginScope()
    {
        return new NinjectDependencyScope(kernel.BeginBlock());
    }
}
Run Code Online (Sandbox Code Playgroud)

public class NinjectDependencyScope : IDependencyScope
{
    IResolutionRoot resolver;

    public NinjectDependencyScope(IResolutionRoot resolver)
    {
        this.resolver = resolver;
    }

    public object GetService(Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this", "This scope has been disposed");

        return resolver.TryGet(serviceType);
    }

    public System.Collections.Generic.IEnumerable<object> GetServices(Type serviceType)
    {
        if (resolver == null)
            throw new …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection ninject inversion-of-control compositionroot

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

重写一个?运算符在c#中使用if-else

我能写这个吗

 return (a, b) => (b == 0) ? 0: a / b;
Run Code Online (Sandbox Code Playgroud)

随着if-else以权(a, b) =>

c# if-statement

0
推荐指数
1
解决办法
84
查看次数