首页 技术 正文
技术 2022年11月20日
0 收藏 450 点赞 2,585 浏览 3674 个字

上一篇文章我们主要讲解了一些webApi和redis缓存操作,这篇文章我们主要说一些MVC相关的知识(过滤器和错误处理),及采用ajax调用webApi服务。

本篇例子采用的开发环境为:VS2010(sp1)、MVC4,所有的数据都是与webApi服务进行交互。

1、先来一张项目结构图

MVC4+WebApi+Redis Session共享练习(下)

  1. LoginAttribute.cs为我们定义的Action过滤器,主要检测是否登陆。因为我们要测试sessioin共享,就做了一个登陆界面,存储用户名。
  2. BaseController.cs 公共控制器,主要重写OnException方法对错误捕捉。HomeController继承BaseController。
  3. ErrorController.cs 错误控制器
  4. LoginController.cs登陆控制器。

1.1、LoginAttribute.cs 过滤器

LoginAttribute继承ActionFilterAttribute并重写了OnActionExecuting方法,OnActionExecuting方法会先与控制器Action执行,因此我们可以在该方法中判断session是否为空。我们打开ActionFilterAttribute发现里面有四个方法OnActionExecuted、OnActionExecuting、OnResultExecuted、OnResultExecuting,不清楚其执行顺序的童鞋可以百度一下,这里就不用介绍了,下面直接看代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;namespace MvcApplication2.Attribute
{
public class LoginAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
SessionHelper session = new SessionHelper();
if (session["user"] == null)
{
filterContext.Result = new RedirectResult("~/Login/index");
}
base.OnActionExecuting(filterContext);
}
}
}

1.2、BaseController.cs 控制器基类

BaseController继承MVC的Controller,在BaseController.cs中我们重写OnException方法进行异常处理,我们可以记录日志,跳转错误页面等,这也我们就不用每个页面写自己的异常处理了,在BaseController中我们定义了一个SessionHelper session的变量,SessionHelper为上一篇文章介绍的基于Redis的session共享,这样只要继承BaseController的页面都可以只用该变量。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;namespace MvcApplication2.Controllers
{
public class BaseController : Controller
{
protected SessionHelper session = new SessionHelper(); public BaseController()
{ }
/// <summary>
/// 异常处理
/// </summary>
/// <param name="filterContext"></param>
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
JsonResult result = new JsonResult();
result.Data = new { type = "error", data = "请求异常" };
filterContext.Result = result;
}
else
{
filterContext.Result = new RedirectResult("~/error/index");
}
filterContext.ExceptionHandled = true;
base.OnException(filterContext);
}
}
}

1.3、LoginController.cs 为登陆控制器

该页面值需要填写用户名,然后把该用户名存到session中,这样webAPi项目中的Get方法也会获取到该session值(详细看上一篇博文)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;namespace MvcApplication2.Controllers
{
public class LoginController : Controller
{
//
// GET: /Login/ public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult LoginIn(string user)
{
SessionHelper session = new SessionHelper();
user = Request.Form["user"];
if (!string.IsNullOrWhiteSpace(user))
{
session["user"]=user.ToString();
return RedirectToAction("index", "Home");
} return RedirectToAction("LoginIn", "Login");
} }
}

1.4、HomeController.cs 展示数据控制器

HomeController继承BaseController,并且在用[Login]过滤器作用在HomeController上,这样每一个Action前都会执行LoginAttribute,判断session值是否为空,

HomeController也继承了BaseController中的错误处理,具体看代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using MvcApplication2.Attribute;namespace MvcApplication2.Controllers
{
[Login]
public class HomeController : BaseController
{
//
// GET: /Home/ public ActionResult Index()
{
ViewBag.User = session["user"].ToString();
return View();
}
public ViewResult Add()
{
return View();
}
public ViewResult edit(string id)
{
ViewBag.id = id;
return View();
}
public ActionResult LogOut()
{
session["user"] = null;
return RedirectToAction("index", "Login");
} }
}

视图页面就不介绍了,感兴趣的童鞋可以下载源代码查看。

1.5、ErrorController.cs 错误页面,这里就不介绍了

2、测试

2.1、MVC项目和WebApi部署

MVC4+WebApi+Redis Session共享练习(下)

  1. webApi为上一篇介绍的webApi程序。
  2. webApiTest为本片介绍的MVC项目。
  3. 域名都是localhost,不牵扯跨域问题

2.2、上几张图片

1、登陆界面

MVC4+WebApi+Redis Session共享练习(下)

2、点击登陆,进入首页面,记得打开Redis缓存服务

MVC4+WebApi+Redis Session共享练习(下)

我们发现我们获取登陆页面的session值,并取到webApi服务中的数据,说明webApi项目的session也有值了,因为webApi项目的HttpResponseMessage Get()方法也做session值是否为空的判断,详见上一篇博客的说明。

好了项目就写到这里吧,我只实现了数据的获取和数据修改功能,增加和删除没有实现。如果你感兴趣欢迎交流学习。

点击下载源代码

每天学习一点点,每天进步一点点。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898