我开始在任何地方看到上下文.在ASP.NET MVC中,有ControllerContexts,RequestContexts,HttpContexts,FormContexts.在Entity Framework中,您有ObjectContexts和DbContexts.Ninject有Ninject.Activation.IContext.
什么是背景?
asp.net-mvc httpcontext controllercontext objectcontext dbcontext
我在静态类中有以下静态方法.我的问题是在静态方法中使用HttpContext.Current.Response是否安全?我想100%确定它是线程安全的,并且只与调用线程相关联.有人知道答案吗?
public static void SetCookie(string cookieName, string cookieVal, System.TimeSpan ts)
{
try
{
HttpCookie cookie =
new HttpCookie(CookiePrefix + cookieName)
{Value = cookieVal, Expires = DateTime.Now.Add(ts)};
HttpContext.Current.Response.Cookies.Add(cookie);
}
catch (Exception)
{
return;
}
}
Run Code Online (Sandbox Code Playgroud) 这两段代码有什么区别.
HttpContext.Current.Session["myvariable"]
Session["myvariable"]
Run Code Online (Sandbox Code Playgroud)
asp.net 4.0和C#4.0
我有一个Web服务(.svc),我试图使用StackOverflow上其他地方找到的代码捕获SOAP请求.
问题是HttpContext.Currentnull,所以我无法访问Request.InputString.
为什么这是空的,怎么解决?
XmlDocument xmlSoapRequest = new XmlDocument();
Stream receiveStream = HttpContext.Current.Request.InputStream;
receiveStream.Position = 0;
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
xmlSoapRequest.Load(readStream);
}
Run Code Online (Sandbox Code Playgroud) 我知道这里有一个非常相似的问题,但我希望能有更好的探索.如果HttpContext真的在后台使用HttpRuntime.Cache,为什么我会使用HttpContext.Cache而不是HttpRuntime.Cache?
在使用ASP.NET模拟Windows服务来运行预定作业的文章中, Omar使用HttpContext来存储他的缓存项,但是当Jeff Atwood 在这里实现它时,他选择使用HttpRuntime.显然,在这种特殊情况下,它是有意义的,因为您不必执行Web请求将缓存项添加回HttpContext.
但是,我正在寻找一些关于何时使用一个与另一个的好指针.
是什么区别System.Web.HttpContext.Current.User.Identity.Name,并System.Environment.UserName在ASP.Net Web应用程序项目的情况下?
这是我正在尝试做的代码:
Database myDB = DatabaseFactory.CreateDatabase();
bool IsAuthUser = myDB.ExecuteScalar("procIsAuthorizedUser", System.Environment.UserName);
Run Code Online (Sandbox Code Playgroud)
如果它们功能相同,哪个性能更好?
这是一个C#4.0/ASP.Net Web应用程序,它将在组织内部看到适度的使用情况.谢谢你的答案.
我正在开发一个自托管的ASP.NET web api-application.一切正常,但现在我正在努力HttpContext:
我需要从客户端保存会话信息.但HttpContext.Current总是空的.所以很明显我HttpSelfHostServer不使用静态HttpContext-Class.
我不明白的是:为什么......?而且我无法想办法既不告诉也HtttpSelfHostServer不能HttpSelfHostConfiguration合作HttpContext.
这是我正在做的事情:
创建一个 HttpSelfHostConfiguration
Service-Resolvers&RoutesUserNamePassword-Validator使用config创建新的HttpSelfHostServer实例
server.OpenAsync().Wait()任何帮助我如何告诉我的服务器工作HttpContext.Current非常感谢!干杯!
有没有什么办法让HttpContext.Current.Request.Url.Host和HttpContext.Current.Request.ApplicationPath一个电话吗?
像"完整的应用程序URL"?
编辑:澄清 - 我需要的是[]中的部分:
http://[www.mysite.com/mywebapp]/Pages/Default.aspx
Run Code Online (Sandbox Code Playgroud)
我只是出于好奇而问.
编辑2:感谢所有的回复,但没有一个是我正在寻找的.仅供参考,我以这种方式解决了问题(但我仍然有兴趣知道是否有更顺畅的方式):
public string GetWebAppRoot()
{
if(HttpContext.Current.Request.ApplicationPath == "/")
return "http://" + HttpContext.Current.Request.Url.Host;
else
return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
}
Run Code Online (Sandbox Code Playgroud) 这是一个难以制定的问题.我想知道HttpContext.Current如何为每个请求分配一个唯一的实例,因为它是一个静态对象?
谢谢!
当我需要从Web服务器上的目录中删除一些图像文件时,我有一些代码可以正常工作:
Dim ImageURL As String = dsImages.Tables(0).Rows(iImgRow).Item("ImageURL")
Dim physicalName = Server.MapPath(ImageURL)
oUpload.DeleteFileFromServer(physicalName, iAdid, iImgID)
Run Code Online (Sandbox Code Playgroud)
..但是当以设定的时间间隔在单独的线程中运行的维护任务确定需要删除上述文件时,我遇到了问题:
Dim ImageURL As String = dsImage.Tables(0).Rows(i - 1).Item("ImageURL")
Dim iImgID As Integer = dsImage.Tables(0).Rows(i - 1).Item("ImageId")
Dim physicalName As String = HttpContext.Current.Server.MapPath(ImageURL)
oUpload.DeleteFileFromServer(physicalName, iAdID, iImgID)
Run Code Online (Sandbox Code Playgroud)
在后一种情况下,HttpContext.Current.Server.MapPath(ImageURL)的值为Nothing.
有没有办法获得这种情况的完整路径?
httpcontext ×10
asp.net ×6
c# ×5
asp.net-mvc ×1
caching ×1
code-behind ×1
dbcontext ×1
inputstream ×1
self-hosting ×1
session ×1
vb.net ×1
wcf ×1
web-services ×1