标签: httpcontext

访问WCF RequestInterceptor中的HttpContext

我正在使用WCF REST stater工具包来构建一个简单的xml over HTTP服务.作为这个Im的一部分,我使用RequestInterceptor进行身份验证.在RequestInterceptor内部,我可以访问System.ServiceModel.Channels.RequestContext对象,从中可以获取请求url,querystring params和其他有用的东西.我无法解决的是如何访问请求的HttpContext.我有几个东西存储在HttpContext中,我想在requestInterceptor中访问但是我很难找到它们.当我在Visual Studio中使用quickwatch时,我可以看到它隐藏在requestContext的私有成员中.有人可以告诉我如何访问HttpContext,也许使用RequestContext对象上的反射?

wcf httpcontext wcf-rest

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

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

我创建了一个其他网站可以用来在我的数据库中存储错误的Web服务.然后,他们可以访问我的网站查看他们的错误,搜索错误,过滤错误等.但是,我的Web服务出现以下错误:


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

描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

异常详细信息:System.InvalidOperationException:System.Web.HttpContext无法序列化,因为它没有无参数构造函数.


Web服务包含以下功能:

[WebMethod]
public static void LogError(HttpContext context, Exception exception, string APIKey)
{
    //Log the error
}

使用Web服务记录异常的外部站点在global.asax文件中包含以下代码:

void Application_Error(object sender, EventArgs e) 
{ 
    // Code that runs when an unhandled error occurs

    WebService.Errors.ErrorHandler.LogError(HttpContext.Current, Server.GetLastError(), "NOLDFHOI");
}

如何从Web站点将HttpContext从其站点获取到我的函数中?

c# asp.net serialization web-services httpcontext

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

在.NET 3.5和Visual Studio 2008中找不到HttpContext

我从这里使用代码,我得到以下错误:无法使用HttpContext.Current.Server.MapPath()

在Visual Studio 2008中,当您缺少引用时,ContextMenuEntry"Solve"会帮助您吗?

我已经发现这HttpContext不是System.Web我IDE中的成员.根据帮助>信息我使用的是.NET 3.5 SP1.

我该如何运行?

在这种情况下你通常如何反应?你在msdn.com找什么东西?

.net c# httpcontext

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

ASP.NET MVC - 单元测试,在不使用任何模拟框架的情况下模拟HttpContext

由于我在使用Moq框架(ASP.NET MVC -使用Moq框架测试RenderPartialViewToString()的单元测试RenderPartialViewToString())时遇到问题,我正在考虑直接获取控制器,而不使用Moq进行这些特定测试,但是,如何在不使用任何Moq框架的情况下为我的测试模拟(或设置)HttpContext?

我需要能够做类似的事情,当然没有Moq:

    var mockHttpContext = new Mock<ControllerContext>();

    mockHttpContext.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("n1\\test");
    mockHttpContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);
Run Code Online (Sandbox Code Playgroud)

非常感谢你.

asp.net asp.net-mvc unit-testing httpcontext asp.net-mvc-2

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

在BaseController中获取/设置HttpContext会话方法与Mocking HttpContextBase一起创建Get/Set方法

我在BaseController类中创建了Get/Set HttpContext会话方法,还创建了Mocked HttpContextBase并创建了Get/Set方法.

哪个是使用它的最佳方式.

    HomeController : BaseController
    {
        var value1 = GetDataFromSession("key1") 
        SetDataInSession("key2",(object)"key2Value");

        Or

        var value2 = SessionWrapper.GetFromSession("key3");
        GetFromSession.SetDataInSession("key4",(object)"key4Value");
    }
Run Code Online (Sandbox Code Playgroud)
   public class BaseController : Controller
   {
       public  T GetDataFromSession<T>(string key)
       {
          return (T) HttpContext.Session[key];
       }

       public void SetDataInSession(string key, object value)
       {
          HttpContext.Session[key] = value;
       }
   }
Run Code Online (Sandbox Code Playgroud)

要么

  public class BaseController : Controller
  {
     public ISessionWrapper SessionWrapper { get; set; }

     public BaseController()
     {
       SessionWrapper = new HttpContextSessionWrapper();
     }
  }

  public interface ISessionWrapper
  {
     T GetFromSession<T>(string key);
   void    SetInSession(string key, …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc session httpcontext

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

尝试访问dbcontext时获取httpContext是null错误

我们有一个MVC 4项目,并使用Structure Map作为IoC框架.当我们对使用DbContext的服务类进行异步调用时,它会抛出异常:

Value cannot be null.
Parameter name: httpContext

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: httpContext
Run Code Online (Sandbox Code Playgroud)

一旦抛出这个:

The operation cannot be completed because the DbContext has been disposed.
Run Code Online (Sandbox Code Playgroud)

我怎么能纠正这个?

structuremap httpcontext dbcontext

6
推荐指数
0
解决办法
440
查看次数

如何将HttpContext.Current传递给在.net中使用Parallel.Invoke()调用的方法

我有两个方法,它们使用HttpContext.Current来获取userID.当我单独调用这些方法时,我得到userID,但是当使用Parallel.Invoke()调用相同的方法时,HttpContext.Current为null.

我知道原因,我只是在寻找可以访问HttpContext.Current的工作.我知道这不是线程安全的,但我只想执行读操作

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Display();
            Display2();
            Parallel.Invoke(Display, Display2);
        }

        public void Display()
        {
            if (HttpContext.Current != null)
            {
                Response.Write("Method 1" + HttpContext.Current.User.Identity.Name);
            }
            else
            {
                Response.Write("Method 1 Unknown" );
            }
        }

        public void Display2()
        {

            if (HttpContext.Current != null)
            {
                Response.Write("Method 2" + HttpContext.Current.User.Identity.Name);
            }
            else
            {
                Response.Write("Method 2 Unknown");
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

谢谢

c# asp.net httpcontext task-parallel-library

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

如何模拟HttpContext.User

我正在开发一个Asp.net MVC 5项目,我正在尝试设置一个模拟以在控制器中返回自定义主体.我有搜索并尝试了不同的方法,但没有一个有效.

我有一个BaseController,我的所有控制器都继承自.BaseController有一个User属性,它在getter中返回HttpContext.User.HttpContext.user在项目中调用时返回一个值,但在从单元测试项目调用时返回null.

BaseController

public class BaseController : Controller
{
    protected virtual new CustomPrincipal User
    {
        get { return HttpContext.User as CustomPrincipal; }  ***<== Line with issue***
    }
}
Run Code Online (Sandbox Code Playgroud)

自定义校长

public class CustomPrincipal : IPrincipal, ICustomPrincipal
{
    public IIdentity Identity { get; private set; }

    public string UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool   IsStoreUser { get; set; }

    public CustomPrincipal(string username)
    {
        this.Identity = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc unit-testing moq httpcontext

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

将 HttpContext 添加到 HangFire

我是 HangFire 的初学者,期待使用 HangFire 每月在我的网络应用程序中调用一些操作。但是这些操作需要 HttpContext。

那么我的问题是:有没有办法在 HangFire 项目中添加(或创建)一个 httpcontext?

我试图谷歌但没有合适的答案。谢谢你的帮助!

我找到了一个简短的讨论。遗憾地看到,答案是“没有办法”。更新:参考https://discuss.hangfire.io/t/passing-site-url-to-hangfire-recurrent-jobs/2641

httpcontext hangfire

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

HttpContext.Session不保持状态

我到处都是网络,以查找使用HttpContext.Session出了什么问题。似乎在设置会话时已在设置会话,但是在离开函数后,我无法访问它。我已经检查了Startup.cs文件,以确保正确添加了所有内容并安装了正确的NuGet软件包。

在我实际使用会话的控制器类中,我有以下代码:

    /// <summary>
    /// Check to see if user exists.  If yes, go to the administrator interface,
    /// else return to the login page.
    /// </summary>
    /// <param name="administrator"></param>
    /// <returns></returns>
    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult AdminLogin([Bind("UserName", "Password")] Administrator administrator)
    {
        try
        {
            var admin = _context.Administrators.Where(x => x.UserName == administrator.UserName
                && x.Password == administrator.Password).FirstOrDefault();

            if (admin != null)
            {
                HttpContext.Session.SetString("IsAdmin", "true");
                _httpContextAccessor.HttpContext.Response.Cookies.Append("IsAdmin", "true");
            }

            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// Shows …
Run Code Online (Sandbox Code Playgroud)

c# session persistence httpcontext asp.net-core

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