我创建了一个 Blazor 客户端应用程序,在该应用程序中,我有许多具有自定义要求和处理程序的授权策略。其中之一检查URL中请求的ID并检查登录用户是否可以查看该资源。
\n\n例如,通过客户端,用户导航到https://localhost/resource/1f28e41c-bc75-44d6-9eef-d46b66b649c7 ,这是我的 API 上的资源。
\n\nI\xe2\x80\x99m 使用以下代码查看请求路径:
\n\nvar httpContext = _httpContextAccessor.HttpContext;\nstring requestedPath = httpContext.Request.Path.ToString();\nRun Code Online (Sandbox Code Playgroud)\n\n这曾经有效,requestedPath 确实包含值 \xe2\x80\x9c1f28e41c-bc75-44d6-9eef-d46b66b649c7\xe2\x80\x9d
\n\n但是,在 _Host.cshtml 中,我已将渲染模式从“ServerPrerendered”更改为“Server”。这是因为在页面调用期间代码在不同位置执行了两次。
\n\n由于我更改了此设置,requestedPath 值始终为“/_blazor”。
\n\n所以我想知道,在 blazor 应用程序中,如果渲染模式设置为“服务器”,是否可以获得请求的路径?
\n我正在尝试在MOSS的文档库中的事件处理程序中获取HTTPContext,但我所拥有的只是HTTPContext.Current的空值,我在List上执行相同的操作并返回HTTPContext.有一种方法可以获取文档库中的HTTPContext来访问HTTPContext.Request方法吗?
谢谢你的帮助
这是代码:
public class TestContextListItemEventReceiver : SPItemEventReceiver
{
HttpContext current;
static object obj;
/// <summary>
/// Initializes a new instance of the Microsoft.SharePoint.SPItemEventReceiver class.
/// </summary>
public TestContextListItemEventReceiver()
{
current = HttpContext.Current;
}
public override void ItemAdding(SPItemEventProperties properties)
{
obj = current;
}
}
Run Code Online (Sandbox Code Playgroud) 什么是httpContext.Response.SubStatusCode?此值是IIS集成模式管道的一部分.
有点远,但是当没有HttpContext.Current可用时,ASP.NET中有一种方法可以动态获取网站的URL(http://www.example.com).
没有HttpContext,因为代码在后台线程*中运行(但在ASP.NET AppDomain下).我有一个后台进程,将每天晚上邮件出去,需要包括网络地址,但我不想因为部署和测试的硬编码(它改变的http://本地主机:12345到HTTP:/ /testing.example.com然后到http://www.example.com获取实时网站).
*请不要建议Windows服务,我知道这些,但托管限制阻止我这样做.
可能重复:
将视图渲染为字符串
你好
我想知道是否有可能在你的C#代码中(通过调度程序在它自己的线程上并且不知道httpcontext)一个请求转到控制器?
//server side code
// do calculations
// post to a controller that takes in a list of view models
// do stuff with the collection of view models.
public myControllerIwantToCallFromServerSide(List<VM> viewModels)
{
// stuff here
}
Run Code Online (Sandbox Code Playgroud)
我需要一些方法来做一个http请求,以便我可以获得一个httpcontext,因为我需要一个实时的http上下文来使用一个库(动作邮件程序),它接受一个mvc视图并将其呈现为一个电子邮件并发送它.
我想在ASP.NET中缓存一些数据,这样一旦创建了一个缓存,就可以访问所有用户.HttpContext.Current.Cache会这样做,还是我需要访问另一个上下文缓存?
我需要在静态方法中编写一个cookie(我需要静态,因为我想从其他类调用此方法).我找到了解决方案HttpContex.Current,但它对我不起作用.我收到这个错误
非静态字段,方法或属性'System.Web.Mvc.Controller.HttpContext.get'需要对象引用
我也试过添加using System.Web.HttpContext.Current;,我得到这个错误
'System.Web.HttpContext.Current'是'属性',但用作'类型'
我的方法:
public static void WriteCookie(Guid token)
{
HttpCookie cookie = new HttpCookie("LoginControl");
cookie.Value = token.ToString();
cookie.Expires = DateTime.Now.AddHours(0.5);
HttpContext.Current.Reponse.Cookies.Add(cookie);
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?非常感谢Mathew.
因此,我正在创建一个类库,用于处理用户信息,如用户名,密码等.我想这样做,以便我可以使用我的任何Web应用程序引用此库,而不必不断重写用户信息部分.
在,用户信息类库,我想处理登录.我之前在app_code中完成了这项工作,它是使用的web项目的一部分HttpContext.Current.Session.但是,当我尝试在我的类库(甚至是using System.Web)中使用它时,它会抛出一个编译错误,说明在此上下文中不存在HttpContext.我怎样才能访问它?
我已经尝试过以下选项:
var resourcePath = "data.xml";
Application files are located in: c:/MyApp/bin/Release.
AppDomain.CurrentDomain.BaseDirectory refers to "c:/MyApp" folder.
AppDomain.CurrentDomain.SetupInformation.PrivateBinPath refers to "c:/MyApp/bin/Release" folder.
HttpContext.Current.Server.MapPath(resourcePath) refers to http://www.domain.com/MyApp/data.xml
Run Code Online (Sandbox Code Playgroud)
只有PrivateBinPath起作用,但是根据定义它可以包含多个bin目录,所以也许我不应该使用它。
Web API应用程序访问Bin文件夹中的文件的标准方法是什么?
我正在尝试建立一个新项目,并且我添加了一个新类MembershipService,它需要在它的构造函数中传递HttpContext.
在之前的项目中,我使用了代码
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IMembershipService>()
.To<MembershipService>()
.InRequestScope()
.WithConstructorArgument("context", HttpContext.Current);
....
}
Run Code Online (Sandbox Code Playgroud)
然而,在新项目中,我正在使用Ninject Modules,在对StackOverflow和Google进行一些搜索之后,我提出了以下代码:public class ServiceHandlerModule:NinjectModule {
public override void Load()
{
Bind<IMembershipService>()
.To<MembershipService>()
.WithConstructorArgument("context", ninjectContext=> HttpContext.Current);
this.Kernel.Bind(x =>
{
x.FromAssemblyContaining(typeof(NinjectWebCommon))
.SelectAllClasses()
.Where(t => t != typeof(MembershipService))
.BindDefaultInterface();
});
this.Kernel.Bind(x =>
{
x.FromAssemblyContaining<BrandServiceHandler>()
.SelectAllClasses()
.Where(t => t != typeof(MembershipService))
.BindDefaultInterface();
});
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详细信息:Ninject.ActivationException:激活字符串时出错没有匹配的绑定可用,并且该类型不可自我绑定.激活路径:
5)将依赖字符串注入到HttpRequest类型的构造函数的参数filename中
4)将依赖关系HttpRequest注入到HttpContext类型的构造函数的参数请求中
3)将依赖关系HttpContext注入到MembershipService类型的构造函数的参数httpContext中
2)将依赖关系IMembershipService注入到HomeController类型的构造函数的参数membershipService中
1)请求HomeController
谁能指出我哪里出错了?
谢谢,约翰
httpcontext ×10
asp.net ×5
c# ×5
asp.net-mvc ×2
appdomain ×1
blazor ×1
c#-4.0 ×1
caching ×1
http ×1
iis-7 ×1
ninject ×1
response ×1
session ×1
sharepoint ×1
system.web ×1
url ×1