Asp.net+mvc+easyui+sqlite simple User System learning Journey (vi)--use of simple filters

Source: Internet
Author: User
Tags httpcontext sqlite

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.