解决MVC项目中,静态html 未找到时候,404的跳转
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Routing; namespace WebClientService.Modules
{
public class GateKeeperOfStaticHtml : IHttpModule
{
// Fields
private static readonly object _contextKey = new object();
private static readonly object _requestDataKey = new object();
private RouteCollection _routeCollection; // Properties
public RouteCollection RouteCollection
{
get
{
if (this._routeCollection == null)
{
this._routeCollection = RouteTable.Routes;
}
return this._routeCollection;
}
set
{
this._routeCollection = value;
}
} public void Init(HttpApplication application)
{ if (application.Context.Items[_contextKey] == null)
{
application.Context.Items[_contextKey] = _contextKey;
application.PostResolveRequestCache += new EventHandler(this.OnApplicationPostResolveRequestCache);
} } private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
{
HttpContextBase context = new HttpContextWrapper(((HttpApplication)sender).Context);
this.PostResolveRequestCache(context);
} /// <summary>
/// 检测是否是合法的处理后缀类型
/// </summary>
/// <param name="toCheckedStr"></param>
/// <returns></returns>
private bool IsValidExtension(string toCheckedStr) {
bool result = false;
if (string.IsNullOrEmpty(toCheckedStr))
{
result= false;
}
if (toCheckedStr.EndsWith(".html", StringComparison.CurrentCultureIgnoreCase)|| toCheckedStr.EndsWith(".htm", StringComparison.CurrentCultureIgnoreCase))
{
result= true;
} return result;
} public virtual void PostResolveRequestCache(HttpContextBase context)
{ RouteData routeData = this.RouteCollection.GetRouteData(context);
if (routeData != null)
{
//检测路由数据,仅仅监听 html 资源
var controller = routeData.Values.FirstOrDefault(x => x.Key == "controller");
if (default(KeyValuePair<string,object>).Equals(controller))
{
//没有控制器数据 不做处理
return;
}
//检测路由控制器的后缀,是否是 *****.html 或者 *****.htm的格式
if (controller.Value==null)
{
return;
} if (!this.IsValidExtension(controller.Value.ToString()))
{
return;
} IRouteHandler routeHandler = routeData.RouteHandler;
if (routeHandler == null)
{
return;
} if (!(routeHandler is StopRoutingHandler))
{
RequestContext requestContext = new RequestContext(context, routeData);
context.Request.RequestContext = requestContext;
IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
if (httpHandler == null)
{
return;
} try
{ context.RemapHandler(httpHandler); }
catch (Exception ex)
{
throw ex;
} }
} } public void Dispose()
{
throw new NotImplementedException();
} }
}
2 注册默认的错误页面
<customErrors defaultRedirect="defaultError.html" mode="On">
<error statusCode="404" redirect="404notfind.html"/>
</customErrors>
</system.web>
3 注册模块
<modules>
<remove name="FormsAuthentication"/>
<add name ="GateKeeper" type="WebClientService.Modules.GateKeeperOfStaticHtml,WebClientService"/>
</modules>
解决MVC项目中,静态html 未找到时候,404的跳转的更多相关文章
- ASP.NET MVC 解决LINQ表达式中的SqlMethods 未找到命名空间问题
右键项目属性下的引用: 添加引用: 搜索寻找——System.Data.Linq,然后添加成功,即可解决LINQ表达式中的SqlMethods 未找到命名空间问题
- 转 mvc项目中,解决引用jquery文件后智能提示失效的办法
mvc项目中,解决用Url.Content方法引用jquery文件后智能提示失效的办法 这个标题不知道要怎么写才好, 但是希望文章的内容对大家有帮助. 场景如下: 我们在用开发开发程序的时候,经常 ...
- ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案
摘要: ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案 在struts2应用中使用ueditor富文本编辑器上传图片或者附件时,即使配置 ...
- use_frameworks!和#use_frameworks!的区别、解决Swift项目中use_frameworks!冲突的问题
use_frameworks!和#use_frameworks!的区别 转自:https://www.jianshu.com/p/0ae58a477459 1. 用cocoapods 导入swift ...
- 谈谈MVC项目中的缓存功能设计的相关问题
本文收集一些关于项目中为什么需要使用缓存功能,以及怎么使用等,在实际开发中对缓存的设计的考虑 为什么需要讨论缓存呢? 缓存是一个中大型系统所必须考虑的问题.为了避免每次请求都去访问后台的资源(例如数据 ...
- 在 ASP.NET MVC 项目中使用 WebForm、 HTML
原文地址:http://www.cnblogs.com/snowdream/archive/2009/04/17/winforms-in-mvc.html ASP.NET MVC和WebForm各有各 ...
- MVC项目中如何判断用户是在用什么设备进行访问
使用UAParser在C#MVC项目中如何判断用户是在用什么设备进行访问(手机,平板还是普通的电脑) 现在我们开发的很多web应用都要支持手机等移动设备.为了让手机用户能有更加好的用户体验,我们经常为 ...
- 搭建php环境时解决jpeg6 make: ./libtool:命令未找到
搭建php环境时解决jpeg6 make: ./libtool:命令未找到 [root@bogon jpeg-6b]# make; make install ./libtool --mode=comp ...
- 在已有的Asp.net MVC项目中引入Taurus.MVC
Taurus.MVC是一个优秀的框架,如果要应用到已有的Asp.net MVC项目中,需要修改一下. 1.前提约定: 走Taurus.MVC必须指定后缀.如.api 2.原项目修改如下: web.co ...
随机推荐
- MVC4.0中ViewBag、ViewData、TempData和ViewModel几种传值方式的区别
MVC框架实现了数据的分离,使页面看起来更加的简洁,MVC4.0中Controller和View的数据传输有上边这几种方式,今天我们来探讨下这几种方式的却别. 一:ViewBag和ViewData V ...
- 如何解决ajax跨域问题
如何解决ajax跨域问题(转) 由 于此前很少写前端的代码(哈哈,不合格的程序员啊),最近项目中用到json作为系统间交互的手段,自然就伴随着众多ajax请求,随之而来的就是要解决 ajax的跨域问题 ...
- mysql数据库优化[千万级查询]
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- JSP动作--JSP有三种凝视方式
一.JSP动作 动作是特殊的标记,通过一个动作标记能够实现多行Java代码实现的效果.能够动态插入文件.重用JavaBean组件.导向还有一个页面等. 动作元素和指令元素不同,动作元素是在client ...
- .NET基本权限管理框架源代码
有兴趣的朋友欢迎加群讨论:312677516 1.菜单导航管理 2.操作按钮 3.角色管理 4.部门管理 5.用户管理(用户权限) 6.用户组管理(设置成员,用户组权限) 7.系统配置(动态配置系统参 ...
- [Angular 2] @ViewChild to access Child component's method
When you want to access child component's method, you can use @ViewChild in the parent: Parent Compo ...
- Eclipse中如何安装和使用GrepCode插件
GrepCode(GC)Eclipse插件允许Eclipse用户在Eclipse IDE中搜索由GrepCode提供的工厂类.本教程介绍如何安装和使用插件.使用Eclipse3.5(Galileo)的 ...
- Java初转型-Tomcat安装和配置
1.http://www.cnblogs.com/diegodu/p/5915358.html tomcat直接解压,启动然后测试是否正常. 2.http://www.cnblogs.com/mq00 ...
- vs开发常用快捷键
Ctrl+K+D:快速对齐代码///按D的时候K快速弹起 如果出现语法错误则无法对齐Ctrl+Z:撤销Ctrl+S:保存Ctrl+J:快速弹出智能提示Shift+End.Shift+Home//快速选 ...
- C#socket通讯两个最经典错误解决方案
1.经典错误之 无法访问已释放的对象. 对象名:“System.Net.Sockets.Socket” (1).问题现场 (2).问题叙述 程序中的某个地方调用到了socket.close ...