Tag: httpcontext

HttpContext和SignalR HubCallerContext之间的统一静态类

我有很多依赖于HttpContext.Current的代码,我注意到来自SignalR集线器的请求有HttpContext.Current == null ,所以我的代码断了,例如: HttpContext.Current.Request.IsAuthenticated 所以我想出了以下内容: public static class UnifiedHttpContext { private static HubCallerContext SignalRContext { get; set; } private static int SignalRUserId { get { return WebSecurity.GetUserId(SignalRContext.User.Identity.Name); } } private static bool IsSignalRRequest { get { return SignalRContext != null; } } public static void SetSignalRContext(HubCallerContext context) { SignalRContext = context; } public static […]

我们可以使用Response.Flush()而不是Response.End()

Response.End()生成ThreadAbortException 。 使用HttpContext.Current.ApplicationInstance.CompleteRequest代替它并不能解决问题。 那么,我们可以使用Response.Flush()而不是Response.End()

“System.Web.HttpContext无法序列化,因为它没有无参数构造函数。”

我创建了一个其他网站可以用来在我的数据库中存储错误的Web服务。 然后,他们可以访问我的网站查看他们的错误,搜索错误,过滤错误等。但是,我的Web服务出现以下错误: System.Web.HttpContext无法序列化,因为它没有无参数构造函数。 描述:执行当前Web请求期间发生未处理的exception。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 exception详细信息:System.InvalidOperationException:System.Web.HttpContext无法序列化,因为它没有无参数构造函数。 Web服务包含以下function: [的WebMethod] public static void LogError(HttpContext context,Exception exception,string APIKey) { //记录错误 } 使用Web服务记录exception的外部站点在global.asax文件中包含以下代码: void Application_Error(object sender,EventArgs e){//当发生未处理的错误时运行的代码WebService.Errors.ErrorHandler.LogError(HttpContext.Current,Server.GetLastError(),“NOLDFHOI”); } 如何从网站上将HttpContext从他们的网站获取到我的function中?

这两个HttpContext.Current.Session和Session – asp.net 4.0有什么区别

这两段代码有什么区别。 HttpContext.Current.Session[“myvariable”] Session[“myvariable”] asp.net 4.0和C#4.0

使用HttpContext.Current.Application存储简单数据

我想在我的ASP.NET MVC应用程序中存储一个简单对象(包含三个字符串)的小列表。 该列表是从数据库加载的,并且很少通过编辑站点管理区域中的某些值来更新。 我正在考虑使用HttpContext.Current.Application来存储它。 这样我可以在Global.asax中加载它: protected void Application_Start() { RegisterRoutes(RouteTable.Routes); HttpContext.Current.Application[“myObject”] = loadDataFromSql(); // returns my object } 然后可以根据需要从任何控制器或视图轻松引用它。 然后,如果管理区域调用updateMyObject控制器操作,我可以只更新数据库并再次加载它并替换HttpContext.Current.Application[“myObject”] 。 这样做有什么缺点吗? 看起来它会对我想要实现的目标起作用,但是有没有人知道更好的方法来做到这一点,假设我已经列出的方法有一些主要的缺点?

测试使用HttpContext.Current.Request.Files的Web API方法?

我正在尝试编写一个使用HttpContext.Current.Request.Files的Web API方法的测试,经过详尽的搜索和实验后,我无法弄清楚如何模拟它。 正在测试的方法如下所示: [HttpPost] public HttpResponseMessage Post() { var requestFiles = HttpContext.Current.Request.Files; var file = requestFiles.Get(0); //do some other stuff… } 我意识到还有其他 类似的 问题 ,但它们没有解决这个具体情况。 如果我试图模拟上下文,我会遇到Http*对象层次结构的问题。 假设我设置了各种模拟对象(使用Moq ),如下所示: var mockFiles = new Mock(); mockFiles.Setup(s => s.Count).Returns(1); var mockFile = new Mock(); mockFile.Setup(s => s.InputStream).Returns(new MemoryStream()); mockFiles.Setup(s => s.Get(It.IsAny())).Returns(mockFile.Object); var mockRequest = new Mock(); mockRequest.Setup(s => […]

正确使用HttpContext.Current.User与异步等待的方法

我正在使用异步操作并使用像这样的HttpContext.Current.User public class UserService : IUserService { public ILocPrincipal Current { get { return HttpContext.Current.User as ILocPrincipal; } } } public class ChannelService : IDisposable { // In the service layer public ChannelService() : this(new Entities.LocDbContext(), new UserService()) { } public ChannelService(Entities.LocDbContext locDbContext, IUserService userService) { this.LocDbContext = locDbContext; this.UserService = userService; } public async […]

HttpSelfHostServer和HttpContext.Current

我正在开发一个自托管的ASP.NET web api-application。 一切正常,但现在我正在努力与HttpContext : 我需要从客户端保存会话信息。 但是HttpContext.Current总是为null。 所以很明显我的HttpSelfHostServer不能使用静态HttpContext-Class。 我不明白的是:为什么……? 我无法想办法告诉HtttpSelfHostServer和HttpSelfHostConfiguration都不能使用HttpContext 。 这就是我正在做的事情: 创建HttpSelfHostConfiguration 1.1添加Service-Resolvers和Routes 1.2添加自定义UserNamePassword-Validator 使用config创建新的HttpSelfHostServer实例 2.1 server.OpenAsync().Wait() 任何帮助我如何告诉我的服务器使用HttpContext.Current非常感谢! 干杯!

ASP.Net中线程敏捷性的含义是什么?

我正在阅读一篇关于HttpContext和CallContext的文章,看看线程敏捷性。 这是什么意思?

ASP.NET中的System.Web.HttpContext.Current.User.Identity.Name与System.Environment.UserName的对比

在ASP.Net Web应用程序项目的上下文中, System.Web.HttpContext.Current.User.Identity.Name和System.Environment.UserName之间有什么区别? 这是我正在尝试做的代码: Database myDB = DatabaseFactory.CreateDatabase(); bool IsAuthUser = myDB.ExecuteScalar(“procIsAuthorizedUser”, System.Environment.UserName); 如果它们function相同,哪个性能更好? 这是一个C#4.0 / ASP.Net Web应用程序,它将在组织内部看到适度的使用情况。 谢谢你的回答。