Project summary (1) - centralized processing context

xiaoxiao2021-03-06  98

When we receive integer parameters that are passed, we can usually do int QuestionID = Convert.Toint32 (Request ["QuestionID"]) or int QuestionId = Convert.Toint32 (httpcontext.current.Request ["QuestionID"]) passed The characters are also roughly acquired, usually the parameters will be more, so we must repeat the work of the conversion parameters, if we implement a self-container, centrally handle these parameters, which will be better, but also the validation of the parameters Processing, simply implement a custom context

PUBLIC? CLASS

? TestContext

{???? int? QuestionID? =? - 1?; ???? public? int? QuestionID ???? {???????? get? {? return? QuestionID?;?} ?? ??} ???????? public? static? int? getintfromqueryString (string? key) ???? {???????? String? ReturnValue? =? - 1?;??? ???? String? querystringvalue? =? String.empty ??; ???????? queryStringValue? =? httpContext.current.request.QueryString [key]?; ???????? Try? ??????? {????????????? // Here you can write some verification code for this parameter, such as whether it is an integer, etc. ????????}? ??????? catch? {} ???????? Return? RETURNVALUE?; ????} ????????} ??????? public? testContext ???? {????? ???? QuestionID? =? getIntFromQueryString ("QuestionID")?; ????} ???? // Implement the current static attribute ???? public? static? testContext? current ???? {??? ????? get ???????? {?????????????? f (httpContext.current? ==? null) ????????? ????????? Return? new? testContext ()?; ??????????????????????????? Return? (TestContext) httpcontext .Current.items ["testContext"]; ????????} ????}}

If we need to get QuestionID, you can int QuestionID = TestContext.curent.questionID additional, this Context class, not only for querystring, you can also concentrate all the context's PostSession, etc.

In fact, it is concentrated in context, and there is a benefit that can standardize the parameters of the entire development team.

PS: The above code is written by text, may have a pen error, and no debugging

转载请注明原文地址:https://www.9cbs.com/read-124452.html

New Post(0)