标签: httpcontext

是否可以设置特定于页面的 cookie?

我知道可以为“/”或“/folder/”等路径设置 cookie,但是是否可以为特定页面(如“/folder/page.html”)设置 cookie?

.net c# cookies path httpcontext

5
推荐指数
1
解决办法
3453
查看次数

是否可以在ASP.NET中重定向期间设置cookie?

我正在使用ASP.NET.我要么添加或设置一个cookie(取决于是否HttpRequest包含具有指定密钥的cookie),然后立即调用Response.Redirect.cookie未设置.这是正确的行为吗?在具有302状态代码的http响应期间设置cookie是否相互排斥?

这是来源:

        if (context.HttpContext.Request.Browser.Cookies)
        {
            var cookies = context.HttpContext.Request.Cookies;
            var stateCookie = new HttpCookie(SR.session, clientState.SessionId.ToString());
            if (cookies.AllKeys.Contains(SR.session))
            {
                context.HttpContext.Response.Cookies.Set(stateCookie);
            }
            else
            {
                context.HttpContext.Response.Cookies.Add(stateCookie);
            }
        }
Run Code Online (Sandbox Code Playgroud)

以下是响应标头

  • X-AspNetMvc-Version - 2.0
  • 连接 - 关闭
  • 缓存控制 - 私有
  • 内容类型 - 文本/ html
  • 日期 - 2011年3月20日星期日03:48:04 GMT
  • 位置 - http:// localhost:3599/Home/Redirected
  • 服务器 - ASP.NET Development Server/9.0.0.0
  • X-AspNet-Version - 2.0.50727

asp.net response.redirect httpresponse httpcontext httpcookie

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

从当前请求获取应用程序URL

我正在写ac#application.

我正在访问一个页面,例如http://dev.mysite.com/page.aspx

如何从当前上下文中检索此http://dev.mysite.com/

我想在不同环境中创建url时使用它,因此需要从当前请求上下文中读取它.

c# request httprequest httpcontext

5
推荐指数
1
解决办法
6075
查看次数

到底什么是“上下文”命名约定

我正在尝试定义何时将类命名为上下文,但我遇到了非常困难的时期。有人可以为我定义“上下文”并解释何时命名类“上下文”吗?

asp.net naming-conventions httpcontext

5
推荐指数
1
解决办法
1658
查看次数

5
推荐指数
1
解决办法
7555
查看次数

System.Web.HttpContext vs System.Web.MVC.HttpContext

我的控制器使用了这样的声明(不确定订单是否重要)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
Run Code Online (Sandbox Code Playgroud)

我试图访问HTTPContext并注意到我甚至无法获得HTTPContext.Current

看起来有两个HTTPContext变量,而MVC就是先例.我必须完全限定对象的命名空间才能使应用程序编译.

  • 为什么有两个HTTPContext变量?
  • 使用顺序是否会影响我的冲突对象将使用哪个对象进行编译?

c# asp.net-mvc httpcontext

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

在自定义NLog目标中检索HttpContext

我可能在这里错过了一些基本的知识-但是否可以HttpContext.Current在自定义NLog事件中检索?

我试图给每个请求一个唯一的Guid,以便我可以将日志消息与单个事件相关联(即,将单个请求的每个日志事件关联在一起)。因此,我想将此Guid存储在中HttpContext.Current.Items,然后在NLog目标中检索它,并将其包括在日志消息中。

这是我要访问的示例目标HttpContext.Current

[Target("AzureTableTarget")]
public class AzureTableTarget : TargetWithLayout
{

    public AzureTableTarget()
    {
        _appSettings = IoCResolver.Get<IAppSettings>();
    }

    protected override void Write(LogEventInfo logEvent)
    {
        var correlationId = HttpContext.Current; //This is always null

        var batchOperation = new TableBatchOperation();
        CxLogEventBuilder.Build(_appSettings, logEvent).ForEach(batchOperation.Insert);
        _loggingTable.ExecuteBatchAsync(batchOperation);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# logging nlog httpcontext

5
推荐指数
1
解决办法
1067
查看次数

带有 Web.API 2 的 MiniProfiler;是否有全局魔术请求上下文对象?

我正在尝试在我的 web api 站点上设置 MiniProfiler,并且很难MiniProfiler.Current开始工作。

我按照 miniprofiler.com 上的指示,在 global.asax 中有以下内容:

protected void Application_Start()
{
  MiniProfilerEF6.Initialize();
  // other setup
}
protected void Application_BeginRequest() {
  // need to start one here in order to render out the UI
  MiniProfiler.Start();
}
protected void Application_EndRequest() {
  MiniProfiler.Stop();
}
Run Code Online (Sandbox Code Playgroud)

这使用默认的WebRequestProfilerProvider,它将实际的配置文件对象存储在HttpContext.Current.Items.

当我要求时MiniProfiler.Current,它看起来像HttpContext.Current

当我请求我的 Web api URL 之一时:

  • Application_BeginRequest 创建分析器,将其存储在 HttpContext.Current
  • 在 web api 中MessageHandler,我可以看到HttpContext.Current
  • 在网络 apu 中IActionFilterHttpContext.Current现在为空,我的尝试MiniProfiler.Current.Step("controller:action")失败了 …

httpcontext action-filter mvc-mini-profiler asp.net-web-api

5
推荐指数
1
解决办法
3233
查看次数

Response.TransmitFile() 的替代方案

所以我有一组我过去几天一直在玩的代码,我需要从服务器下载一个文件到客户端。这是最简单的部分,但我还需要在完成后刷新网格视图并在文件已成功创建的警报中显示,但我发现下载的每种方式都包含一个选择的代码行,这将是我的倒台。

Response.End()

Response.Close() 或

ApplicationInstance.CompleteRequest()

所有这些都会结束当前的响应,或者我相信在 ApplicationInstance 的情况下,它将页面的所有源代码刷新到我尝试下载的文本文件中。下面是我从服务器下载文件的代码片段,下面是下载我的文件的源代码。如果您有任何可以帮助解决这个永无止境的噩梦的东西,我们将不胜感激。

            //I brought everything together in an arraylist to write to file.
            asfinalLines = alLines.ToArray(typeof(string)) as string[];

            string FilePath = HttpContext.Current.Server.MapPath("~/Temp/");
            string FileName = "test.txt";

            // Creates the file on server
            File.WriteAllLines(FilePath + FileName, asfinalLines);

            // Prompts user to save file
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.ClearContent();
            response.Clear();
            response.ContentType = "text/plain";
            response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
            response.TransmitFile(FilePath + FileName);
            response.Flush();

            // Deletes the file on server
            File.Delete(FilePath + FileName);

            response.Close();
Run Code Online (Sandbox Code Playgroud)

c# asp.net webforms download httpcontext

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

Url Referrer 在 WebApi 2 MVC 项目中不可用

我有一个带有Controllers控制器的 MVC WebAPI 2 项目。我试图调用的方法是POST(创建)。我需要访问调用该方法的引用 URL,无论我访问什么对象,引用 URL 要么不存在于对象中,要么为空。

例如,我添加了HTTPContext引用并返回以下内容null

var thingythingthing = HttpContext.Current.Request.UrlReferrer;
Run Code Online (Sandbox Code Playgroud)

Request对象不具有UrlReferrer财产。

这也返回 null:

HttpContext.Current.Request.ServerVariables["HTTP_REFERER"]
Run Code Online (Sandbox Code Playgroud)

我无法修改标头,因为我需要能够生成指向该方法的链接并按调用来源过滤访问。

我应该查看的任何特定地方,或者,为什么这些返回 null 的任何特定原因?

编辑:我有一个针对 GET 方法 ( HttpContext.Current.Request.RequestContext.HttpContext.Request.UrlReferrer)的解决方案,但没有针对 POST 方法的解决方案。

c# controller httpcontext asp.net-mvc-4 asp.net-web-api2

5
推荐指数
1
解决办法
8052
查看次数