session和cookie的區別,ASP.NET MVC 中解決Session,Cookie等依賴的方式

 2023-10-01 阅读 27 评论 0

摘要:目錄 介紹 使用Filter方式 Filter在MVC生命周期中的位置 Filter常見的應用場景 Filter的執行順序 MVC中常見的對Session,Cookie的依賴 使用Filter解除依賴 使用Model Binding方式 什么是Model Binding? 自定義Model Binding 總結 介紹 本文將分別介紹在MVC中使用

目錄

介紹

使用Filter方式

Filter在MVC生命周期中的位置

Filter常見的應用場景

Filter的執行順序

MVC中常見的對Session,Cookie的依賴

使用Filter解除依賴

使用Model Binding方式

什么是Model Binding?

自定義Model Binding

總結


介紹

本文將分別介紹在MVC中使用Filter和Model Binding兩種方式來說明如何解除對Session,Cookie等的依賴。當然也會適當對Filter在MVC請求的生命周期中的作用和角色,使用場景等作下簡單的說明。

?

使用Filter方式

Filter在MVC生命周期中的位置

?

如上圖所示,一個完成的MVC的生命周期分為5個步驟, 對應圖例中的1~5。

  1. IIS中傳遞請求到程序
  2. MVC根據Routing來選擇由哪個Controller/Action來處理
  3. Controller調用Model(業務邏輯)來處理數據
  4. Controller選擇一個View, 同時把需要呈現的數據交給View Engine呈現
  5. 最后,返回最終的Response到客戶端

Filter在MVC的生命周期中的角色就像是一個一個的截面,在MVC的處理過程中,攔截請求。session和cookie的區別,

Filter分為:

Authorization filters——需要實現IAuthorizationFilter接口,用于驗證處理驗證相關的操作

Action filters——需要實現IActionFilter接口. 在Action處理的開始和結束做攔截操作

Result filters——需要實現IResultFilter接口. 在View呈現前和呈現后做處理

Exception filters——需要實現IExceptionFilter接口,只要是添加了Exception Filter的請求中出現異常,都會被攔截

每個Filter的作用時機,對應于上圖中的2a, 2b, 4a, 4b。

?

Filter常見的應用場景

下面是個人在開發中,常用到的Filter處理:

  • 權限驗證

使用Authorization filters,攔截請求,在進入到Controller處理之前,驗證用戶是否登錄或者登錄用戶是否有權限訪問改頁面。

如果合法,就繼續交由Controller處理,如果非法,中斷流程,跳轉到登錄頁面。

?

  • 日志記錄

通過Action Filter跟蹤記錄Action處理開始的時間,結束時間,訪問的具體Controller和Action, 參數,訪問者ip等信息。下面哪項不是session對象的方法,

?

  • 異常處理

異常處理Exception filter能夠在發生異常的時候,記錄異常信息。如果是session過期引起的異常,則跳轉到登錄頁面,如果是程序運行導致的無法處理異常,則跳轉到友好的錯誤頁面。

?

  • 提升SEO效果

每篇博客文章的meta信息能夠幫助提高SEO效果,但是很多人對于填寫keyword, description等信息覺得太繁瑣。

可以使用Result filters,在最后呈現頁面前,使用程序分析內容,提取keyword和description來,然后填充到meta信息中。關于session對象的屬性,

這樣,每篇博客文章都能夠有程序實現最佳的SEO效果,甚至生成一份SEO報告出來。

?

Filter的執行順序

Filter之間執行的順序,首先根據類型區分:

分別是Authorization filters, Action filters, Result filtersException Filter沒有列入的原因是, 它是在發生異常的時候處理,沒有特定的順序。

當同時對一個類型的Filter的時候,執行順序可以通過Filter的Order屬性來排序。下面哪項不是JSP內置對象,

?

MVC中常見的對Session,Cookie的依賴

在Web程序中,對于Session和Cookie等的使用是必不可少的。比如, 很多的Action的代碼中,會要從Session中獲取當前登錄用戶信息:

public ActionResult Index()
{var user = Session[“UserAccuont”];//從Session中獲取當前登錄用戶的信息//send emailvar email = user.Email;…………
}

上面的Index方法的問題就是和Session耦合,很難單元測試。下面介紹如何使用Filter來解除對于Session的依賴。

?

使用Filter解除依賴

添加一個SessionUserParameterAttribute的Action Filter, 它的功能是:

從Session中取得User, 將取得的User賦值給Action中的參數sessionUser。php session和cookie的區別?

public class SessionUserParameterAttribute : ActionFilterAttribute
{public override void OnActionExecuting(ActionExecutingContext filterContext){const string key = "sessionUser";if (filterContext.ActionParameters.ContainsKey(key)){filterContext.ActionParameters[key] = Session[“UserAccuont”];//為Action設置參數}base.OnActionExecuting(filterContext);}
}

改造后的Index Action方法如下:

[SessionUserParameter]
public ActionResult Index(UserAccount sessionUser)
{//send emailvar email = sessionUser.Email;…………
}

這樣Index方法就解除了對于Session的依賴,而只是依賴于一個普通的實體類UserAccount。在單元測試中,只需要簡單的構造一個UserAccount的對象就可以了。

?

使用Model Binding方式

什么是Model Binding?

Model Binding的作用就是將Request請求中包含的散亂參數,根據接受請求的Action方法的參數列表,自動智能地構建這些參數的過程。

?

自定義Model Binding

繼承接口IModelBinder, 實現BindModel方法。js cookie和session區別。

這個UserAccountModelBinder的作用就是從Session中取得UserAccount。(問題和Filter中提到的問題相同)

   public class UserAccountModelBinder : IModelBinder{public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext){if(controllerContext.HttpContext.Session["UserAccuont"] != null){return controllerContext.HttpContext.Session["UserAccuont"];}return null;}}

接下來,我們要為這個ModelBinder,添加到MVC的Model Binding設置中,使得它能夠在MVC的生命周期中起作用。

在Global.asax.cs文件的Application_Start()方法中,添加UserAccountModelBinder。

protected void Application_Start()
{………//凡是UserAccount類型的參數,都會使用UserAccountModelBinder來處理,也就是會從Session中取值ModelBinders.Binders.Add(typeof(UserAccount), new UserAccountModelBinder ());}

改造后的Index方法如下:

public ActionResult Index(UserAccount sessionUser)
{//send emailvar email = sessionUser.Email;…………
}

上面就是全部的Model Binding解決問題的過程,希望能夠幫助大家更好地理解MVC中的Model Binding。cookie和session的作用和區別?

?

總結

本文主要介紹了MVC中兩種解決對Session,Cookie等依賴的方式,這兩種方式可根據自己項目的需求具體選擇使用哪種。其中Filter還有其他一些場景的使用,有興趣的小伙伴也可以自己多多探索。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/108174.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息