C# 如何从Web请求确定Ninject绑定的值';谁的网址?
以下是我的大致情况: Api控制器C# 如何从Web请求确定Ninject绑定的值';谁的网址?,c#,asp.net-web-api,ninject,C#,Asp.net Web Api,Ninject,以下是我的大致情况: Api控制器 public class MyController : ApiController { public MyController(IServiceThatNeedsPrinicipal myService) { } } MyService public class MyService: IServiceThatNeedsPrinicipal { public MyService(IMyPrincipal myService)
public class MyController : ApiController
{
public MyController(IServiceThatNeedsPrinicipal myService)
{
}
}
MyService
public class MyService: IServiceThatNeedsPrinicipal
{
public MyService(IMyPrincipal myService)
{
}
}
IMyPrincipal
public interface IMyPrincipal : IPrincipal {}
此自托管网站将收到帖子
请求,如:
localhost:9005/Start?UserId=1
我想做的是:
this.Bind<IMyPrincipal>().ToMethod(c =>
{
int userId = /* Get UserId from Request -- How do I do this inside a NinjectModule? */
IMyPrincipal mp = CodeToGetMyPrincipalFromUserId(userId); /* I know how to do this */
return mp;
});
并进行查询,但在运行时HttpContext.Current
为null
有什么想法吗?我如何从url中获取值并在我的Ninject模块中访问
提前谢谢 我希望它能发挥我想要的功能。似乎有效。(当然很难看。) 我创建了一个具有静态属性的类:
public class RequestMessage
{
public static HttpRequestMessage Contents { get; set; }
}
然后我添加了一个DelegatingHandler来设置它
public class RequestMessageHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
RequestMessage.Contents = request;
return base.SendAsync(request, cancellationToken);
}
}
并将我的代码更改为使用:
var request = RequestMessage.Contents;
config.MessageHandlers.Add(new RequestMessageHandler());
var request = RequestMessage.Contents;