如何确定传入请求是否针对控制器(或与此相关的任何其他资源)?

发布于 2024-12-11 15:05:12 字数 315 浏览 0 评论 0原文

我试图帮助减少分发数据库会话(我正在使用 NHibernate)的时间,以便仅在传入请求针对需要会话的对象时才分发一个。

我知道有一种方法可以获取 HttpApplication 中的信息,让我确定这一点,但我不确定它是否像我希望的那样直接,或者我是否必须根据路由请求实现一些逻辑。 ..ETC。

有人可以推荐某种方式让我可以审查请求以确定这一点吗?我已经审查了 HttpApplication 类和许多属性/方法,并假设它将归结为 HttpContext.Current.Request.RequestContext 中的某个位置,但我想我会看看是否有一种简单的方法可以做到这一点。

I am attempting to help reduce when I hand out a database session (I am using NHibernate) so as to only hand one out if the incoming request is for a object that requires a session.

I know there is a way to get to information in the HttpApplication that would let me determine this, but I am not sure if it is as straight forward as I would like, or if I have to implement some logic based on the route request...etc.

Can someone recommend some way that I can review the request for determining this? I have reviewed the HttpApplication class and a number of properties/methods and assuming it is going to come down to somewhere in HttpContext.Current.Request.RequestContext, but I thought I would see if there is a easy way of doing so.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘过的浮云 2024-12-18 15:05:12

这是来自 Oyvind Valland 的博客,他正在做我正在寻找的事情,并检查传入的请求请求资源以确定是否需要会话。 Julian Margarintescu 提出了一个很好的观点,但为了回答实际问题,以防因其他原因需要它。

private static readonly string[] NoPersistenceFileExtensions = new string[] { ".jpg", ".jpeg", ".gif", ".png", ".css", ".js", ".swf", ".xap", ".ico" };    
private static bool RequestMayRequirePersistence(HttpApplication httpApplication)
{
if (httpApplication == null)
return false;

HttpContext context = httpApplication.Context;
if (context == null)
return false;

string fileExtension = Path.GetExtension(context.Request.PhysicalPath);
return fileExtension != null && Array.IndexOf(NoPersistenceFileExtensions, fileExtension.ToLower()) < 0;
}

This is from Oyvind Valland's Blog who was doing precisely what I was looking for and checking the incoming request for the resource requested to determine if a session was needed. Julian Margarintescu makes a good point, but for an answer to the actual question in case it was needed for some other reason.

private static readonly string[] NoPersistenceFileExtensions = new string[] { ".jpg", ".jpeg", ".gif", ".png", ".css", ".js", ".swf", ".xap", ".ico" };    
private static bool RequestMayRequirePersistence(HttpApplication httpApplication)
{
if (httpApplication == null)
return false;

HttpContext context = httpApplication.Context;
if (context == null)
return false;

string fileExtension = Path.GetExtension(context.Request.PhysicalPath);
return fileExtension != null && Array.IndexOf(NoPersistenceFileExtensions, fileExtension.ToLower()) < 0;
}
青瓷清茶倾城歌 2024-12-18 15:05:12

您应该阅读 Ayende 撰写的关于成本的文章打开会话。空/未使用的会话非常轻,您可以为任何请求创建它们,因为它们不会打开数据库连接。

You should read this post by Ayende about the cost of opening a session. Empty/unused sessions are so light you can create them for any request, as they will not open a db connection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文