Some students like to test or run the project, jump directly to the page to Home/index, but this project directly input Home/index will be error
Because there is a user.name parameter in Home/index, if you jump directly to the Home/index page after you start the project, you will get an error. Below, we learn to use filters, directly enter Home/index after the page to jump to the login page. (Note: If you are currently in the test, have logged in to jump to the home page, before logging off before the refresh, enter the Home/index address, and will not error, because the User.Name parameter is saved in the session, and we in the logoff operation to empty the session content)
OK, let's construct the filter below.
1. Create a new folder named filter under the Controllers folder to put the filter code, and then add a class file named CheckLoginFilter.cs
2. Add the following code to the CheckLoginFilter.cs:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespaceusermanager.web.controllers.filter{ Public classCheckloginfilter:filterattribute, Iactionfilter { Public voidonactionexecuted (ActionExecutedContext filtercontext) {if(httpcontext.current.session["User"] ==NULL) {FilterContext.HttpContext.Response.Write ("-1"); } } Public voidonactionexecuting (ActionExecutingContext filtercontext) {if(httpcontext.current.session["User"] ==NULL) {Filtercontext.result=NewRedirectresult ("/account/index"); } } }}
3. Add a filter to the controller/homecontrollers.cs file:
Display results: When the project is run, the address bar enters Home/index, immediately jumps to the Account/index login screen.
Asp.net+mvc+easyui+sqlite simple User System learning Journey (vi)--use of simple filters