Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要当前Request.Url的类型的Ninject绑定_C#_Dependency Injection_Asp.net Mvc 5_Ninject - Fatal编程技术网

C# 需要当前Request.Url的类型的Ninject绑定

C# 需要当前Request.Url的类型的Ninject绑定,c#,dependency-injection,asp.net-mvc-5,ninject,C#,Dependency Injection,Asp.net Mvc 5,Ninject,我正在一个基于MVC5的网站中使用Ninject 3,并试图解决如何让DI使用一种类型,该类型测试传递到其构造函数中的Uri.Host值的属性。我希望绑定以某种方式提供当前URL。我最初尝试的最小结构是: public class StructuredUrlTester : IStructuredUrlTester { // Expose public getters for parts of the uri.Host value bool MyBooleanProperty {

我正在一个基于MVC5的网站中使用Ninject 3,并试图解决如何让DI使用一种类型,该类型测试传递到其构造函数中的
Uri.Host
值的属性。我希望绑定以某种方式提供当前URL。我最初尝试的最小结构是:

public class StructuredUrlTester : IStructuredUrlTester
{
    // Expose public getters for parts of the uri.Host value
    bool MyBooleanProperty { get; private set; }

    public StructuredUrlTester(Uri uri)
    {
        // Test the value of uri.Host and extract parts via regex
    }
}

// In Global.asax.cs
public class MvcApplication : NinjectHttpApplication
{
    protected override IKernel CreateKernel()
    {
        kernel.Bind<IStructuredUrlTester>()
            .To<StructuredUrlTester>()
            .InTransientScope();
            .WithConstructorArgument("uri", Request.Url);
    }
}

// In MyController.cs
public class MyController : Controller
{
    private readonly IStructuredUrlTester _tester;

    public ContentPageController(IStructuredUrlTester tester)
    {
        this._tester = tester;
    }

    public ActionResult Index()
    {
        string viewName = "DefaultView";
        if (this._tester.MyBooleanProperty)
        {
            viewName = "CustomView";
        }

        return View(viewName);
    }
}
公共类结构DurlTester:IStructuredUrlTester
{
//公开uri.Host值部分的公共getter
bool MyBooleanProperty{get;private set;}
公共结构DurlTester(Uri)
{
//测试uri.Host的值并通过regex提取部分
}
}
//在Global.asax.cs中
公共类MVC应用程序:NinjectHttpApplication
{
受保护的覆盖IKernel CreateKernel()
{
kernel.Bind()
.至()
.InTransientScope();
.WithConstructorArgument(“uri”,Request.Url);
}
}
//在MyController.cs中
公共类MyController:Controller
{
专用只读IStructuredUrlTester\u测试仪;
公共内容页面控制器(IStructuredUrlTester测试仪)
{
本测试仪=测试仪;
}
公共行动结果索引()
{
字符串viewName=“DefaultView”;
if(此._tester.MyBooleanProperty)
{
viewName=“CustomView”;
}
返回视图(viewName);
}
}
由于
CreateKernel()
调用发生在
Request
对象可用之前,因此
.WithConstructorArgument()
部分引发异常(“System.Web.HttpException:请求在此上下文中不可用”)


如何提供接口到具体类型的绑定,同时也能够在运行时向具体类型的构造函数提供例如
HttpContext.Current.Request.Url
值(在控制器中可用)?

将所需的功能封装在抽象中:

public interface IUriProvider {
    Uri Current { get; }
}
重构tester类:

public class StructuredUrlTester : IStructuredUrlTester {
    // Expose public getters for parts of the uri.Host value
    bool MyBooleanProperty { get; private set; }

    public StructuredUrlTester(IUriProvider provider) {
        Uri uri = provider.Current;
        // Test the value of uri.Host and extract parts via regex
    }
}
提供者实现应该包装
请求.Url

public class UriProvider : IUriProvider {
    public Uri Current { get { return  HttpContext.Current.Request.Url; } }
}

请注意,
Current
属性实际上应该在控制器的操作中调用,其中
HttpContext
及其请求可用。

将HttpContext包装在抽象中。您是否考虑过从这里获取uri System.Web.HttpContext.Current.request.Url,在StructureDurlTester的构造函数中,有一个明确的理由说明为什么需要在抽象和抽象背后实现这种逻辑?很明显,我会把它放在一个helper/扩展方法中…Dima和Kayes:在这种情况下,我需要传入当前URL,但我不想让StructuredUrlTester依赖于了解当前HttpContext等。它应该只接受一个
System.Uri
对象,并根据其结构公开属性。我可能希望以后用其他具体的测试机制来替换那个具体的类型。非常好,正如我所需要的那样工作。我现在可以看到,使用提供程序意味着只有在构建
MyController
时(通过Ninject,这意味着也在构建
StructuredUrlTester
),才能访问
HttpContext
,此时请求对象已填充并可用。谢谢你的帮助!