将Session []和Request []传递给C#中的方法 [英] Passing Session[] and Request[] to Methods in C#

查看:355
本文介绍了将Session []和Request []传递给C#中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,如何将Session []和Request []对象传递给方法?

In C#, How do you pass the Session[] and Request[] objects to a method?

我想使用一种方法来解析.aspx页的会话和请求参数,以减小我的Page_Load方法的大小.我传递了很多变量,并且需要同时支持POST和GET方法.对于大多数调用而言,并非所有变量都存在,因此我必须以多种方式测试每个变量,并且代码变得冗长...

I would like to use a method to parse out Session and Request paramaters for a .aspx page to reduce the size of my Page_Load method. I am passing quite a few variables, and need to support both POSTand GET methods. For most calls, not all variables are present, so I have to test every variable multiple ways, and the code gets long...

这是我要尝试的操作,但是我似乎无法正确识别Session和Request参数(此代码无法编译,因为数组是按数字索引的)

This is what I am trying to do, but I can't seem to properly identify the Session and Request paramaters (this code will not compile, because the arrays are indexed by number)

static string getParam(
    System.Web.SessionState.HttpSessionState[] Session,
    System.Web.HttpRequest[] Request,
    string id)
{
    string rslt = "";

    try
    {
        rslt = Session[id].ToString();
    }
    catch
    {
        try
        {
            rslt = Request[id].ToString();
        }
        catch { }
    }

    return rslt;
}

我要从Page_Load调用此方法,如下所示以检索"MODE"参数:

From Page_Load, I want to call this method as follows to retrieve the "MODE" paramater:

string rslt;
rslt = getParam(Session, Request, "MODE");

谢谢!

推荐答案

您无需将它们声明为数组参数;它们不是数组,它们只是单个对象.因此,您的函数签名应如下所示:

You don't need to declare them as array parameters; they aren't arrays they are just a single object. So your functions signature should look something like this:

static string getParam(
    System.Web.SessionState.HttpSessionState Session,
    System.Web.HttpRequest Request,
    string id)

只需进行此更改,您的代码就可以工作,但是可以进行很多改进,例如,以下操作可以实现相同功能,但是没有 NullReferenceException 的风险:

With just that change your code should work, but it could be improved quite a bit, for example the following does the same but without the risk of NullReferenceException:

private static string GetParameter(
    HttpSessionState session,
    HttpRequest request,
    string id)
{
    var value = session[id] ?? request[id];
    return value == null ? string.Empty : value.ToString();
}

需要注意的几点:

  1. 遵循在框架中看到的样式指南.这意味着函数名称,参数名称等应使用相同的大小写.此外,请不要缩写单词(即使用参数"而不是参数").显式声明访问修饰符也是一个好主意(即,如果您要表示方法是私有的,则编写 private ).

永远不要使用 try/catch 来捕获非特定的异常.您可能最终尝试吞没异步异常,例如内存不足或线程中止,这是灾难的根源.如果您不知道为什么不应该捕获非特定的异常,那么还有更多的理由不知道,除非您确实知道.

Never, ever, use try/catch to catch non-specific exceptions. You could end up attempting to swallow asynchronous exceptions like out-of-memory or thread-abort which is a recipe for disaster. If you don't know why you shouldn't catch non-specific exceptions, then it's even more reason not to until you do know.

永远不会捕获到 NullReferenceException (我怀疑这是这里经常发生的异常以及为什么 try/catch 块在那里的原因).每当抛出此异常时,它都是您需要修复的代码中的错误.与大多数开发规则不同,该规则没有例外.

Never, ever, catch NullReferenceException (which I suspect is the exception that is frequently happening here and why the try/catch blocks are there). Any time this exception is thrown it is a bug in your code that you need to fix. Unlike most rules of development, there are no exceptions to this rule.

这篇关于将Session []和Request []传递给C#中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆