我知道可以为“/”或“/folder/”等路径设置 cookie,但是是否可以为特定页面(如“/folder/page.html”)设置 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)
asp.net response.redirect httpresponse httpcontext httpcookie
我正在写ac#application.
我正在访问一个页面,例如http://dev.mysite.com/page.aspx
如何从当前上下文中检索此http://dev.mysite.com/
我想在不同环境中创建url时使用它,因此需要从当前请求上下文中读取它.
我正在尝试定义何时将类命名为上下文,但我遇到了非常困难的时期。有人可以为我定义“上下文”并解释何时命名类“上下文”吗?
从http上下文类是否有一种获取当前Controller名称的方法?
model-view-controller controller namespaces httpcontext asp.net-mvc-3
我的控制器使用了这样的声明(不确定订单是否重要)
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.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) 我正在尝试在我的 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.CurrentMessageHandler,我可以看到HttpContext.CurrentIActionFilter,HttpContext.Current现在为空,我的尝试MiniProfiler.Current.Step("controller:action")失败了 …所以我有一组我过去几天一直在玩的代码,我需要从服务器下载一个文件到客户端。这是最简单的部分,但我还需要在完成后刷新网格视图并在文件已成功创建的警报中显示,但我发现下载的每种方式都包含一个选择的代码行,这将是我的倒台。
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) 我有一个带有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 方法的解决方案。
httpcontext ×10
c# ×6
asp.net ×3
controller ×2
.net ×1
asp.net-mvc ×1
cookies ×1
download ×1
httpcookie ×1
httprequest ×1
httpresponse ×1
logging ×1
namespaces ×1
nlog ×1
path ×1
request ×1
webforms ×1