Tag: httpcontext

什么是HttpContext.Current.Request.RawUrl的WCF等价物?

我有一些RESTful服务运行在纯WCF上下文(即ASP.NET兼容性未启用,因此没有HttpContext.Current对象可用)。 服务的URL在请求开始时使用一个IHttpModule (在那个时候有一个HttpContext并且使用HttpContext.Current.RewritePath重写它)来重写,以去掉URL中的.svc扩展名。 但是,我需要访问WCF基础结构内请求的原始URL。 在OperationContext或WebOperationContext类的任何地方有没有相当于HttpContext.Current.Request.RawUrl ? 使用WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri返回重写的URL不是原来的。

Request.UserHostAddress问题返回结果“:: 1”

我正在尝试使用客户端IP地址 HttpContext.Request.UserHostAddress; 但它返回::1 。 如何解决这个问题?

在控制器的构造函数中访问HttpContext.Request

我正在关注微软的ASP.NET MVC教程 : 我的代码略有不同,我试图访问控制器的构造函数中的HttpContext.Request.IsAuthenticated 。 namespace SCE.Controllers.Application { public abstract class ApplicationController : Controller { public ApplicationController() { bool usuario = HttpContext.Request.IsAuthenticated; } } } 问题是HttpContext始终为空。 有针对这个的解决方法吗?

获取应用程序path而不使用httpcontext。 (asp.net)

怎么做? 我不想用这个: HttpContext.Current.Server.MapPath 有没有类似的function,我可以打电话,而不需要一个httpcontext? 例如,如果开始一个线程做一些东西,我不能使用httpcontext,但我仍然需要得到的应用程序的path。 不,我不能将上下文作为parameter passing或从共享variables中读取。

从HttpContext获取当前的System.Web.UI.Page?

这实际上是一个两部分问题。 首先,HttpContext.Current是否对应于当前的System.UI.Page对象? 第二个问题可能与第一个问题有关,那就是为什么我不能使用以下来查看当前页面是否实现了一个接口: private IWebBase FindWebBase() { if (HttpContext.Current as IWebBase != null) { return (IWebBase)HttpContext.Current.; } throw new NotImplementedException("Crawling for IWebBase not implemented yet"); } 一般情况下,一些控件需要知道它们是作为SharePoint webpart执行还是作为Asp.Net框架的一部分执行。 我已经通过要求控件传递一个引用给自己,并检查控件的Page属性来解决这个问题,但是我仍然很好奇为什么上面的代码不起作用。 编译器错误是:无法将System.Web.HttpContext转换为… IWebBase通过引用转换,装箱转换,拆箱转换,包装转换或空types转换。

如何访问Thread或Timer中的HttpServerUtility.MapPath方法?

我在我的Asp.Net应用程序中使用System.Timers.Timer ,我需要使用HttpServerUtility.MapPath方法,似乎只能通过HttpContext.Current.Server.MapPath 。 问题是当Timer.Elapsed事件触发时HttpContext.Current为null 。 有没有另一种方式来获取对HttpServerUtility对象的引用? 我可以在我的类的构造函数中注入它。 安全吗? 我如何确定在当前请求结束时不会收集垃圾? 谢谢!

如何使用Moq模拟ASP.NET MVC中的HttpContext?

[TestMethod] public void Home_Message_Display_Unknown_User_when_coockie_does_not_exist() { var context = new Mock<HttpContextBase>(); var request = new Mock<HttpRequestBase>(); context .Setup(c => c.Request) .Returns(request.Object); HomeController controller = new HomeController(); controller.HttpContext = context; //Here I am getting an error (read only). … } 我的基础控制器有一个重载的Initialize,得到这个requestContext。 我正试图通过这一点,但我没有做正确的事情。 protected override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); } 我在哪里可以得到更多关于使用Moq嘲弄我的RequestContext和HttpContext的信息? 我试图嘲笑cookies和一般情况。

如何从ASP.NET MVC 1中的HttpContextBase获取HttpContext对象?

我正在使用一些WebForms / MVC不可知论的工具,我需要得到一个HttpContext的实例给定一个HttpContextBase对象的引用。 我不能使用HttpContext.Current因为我需要这个asynchronous工作( HttpContext.Currentasynchronous请求期间返回null )。 我知道HttpContextWrapper ,但错误的方式。

在testing初始化​​方法中模拟HttpContext.Current

我试图添加unit testing到我已经build立的ASP.NET MVC应用程序。 在我的unit testing中,我使用下面的代码: [TestMethod] public void IndexAction_Should_Return_View() { var controller = new MembershipController(); controller.SetFakeControllerContext("TestUser"); … } 通过以下帮助程序来模拟控制器上下文: public static class FakeControllerContext { public static HttpContextBase FakeHttpContext(string username) { var context = new Mock<HttpContextBase>(); context.SetupGet(ctx => ctx.Request.IsAuthenticated).Returns(!string.IsNullOrEmpty(username)); if (!string.IsNullOrEmpty(username)) context.SetupGet(ctx => ctx.User.Identity).Returns(FakeIdentity.CreateIdentity(username)); return context.Object; } public static void SetFakeControllerContext(this Controller controller, string username = […]

如何获得一个wcf应用程序的工作path?

我想获得WCF应用程序的工作文件夹。 我怎样才能得到它? 如果我尝试 HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath) 我得到一个空引用exception(Http.Current对象为空)。 我的工作文件夹的意思是我的WCF服务运行的文件夹。 如果我设置aspNetCompatibilityEnabled="true" ,我得到这个错误: 服务器没有提供有意义的回复; 这可能是由于合同不匹配,会话过早closures或内部服务器错误造成的。