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的跳转的更多相关文章

  1. ASP.NET MVC 解决LINQ表达式中的SqlMethods 未找到命名空间问题

    右键项目属性下的引用: 添加引用: 搜索寻找——System.Data.Linq,然后添加成功,即可解决LINQ表达式中的SqlMethods 未找到命名空间问题

  2. 转 mvc项目中,解决引用jquery文件后智能提示失效的办法

    mvc项目中,解决用Url.Content方法引用jquery文件后智能提示失效的办法   这个标题不知道要怎么写才好, 但是希望文章的内容对大家有帮助. 场景如下: 我们在用开发开发程序的时候,经常 ...

  3. ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案

    摘要: ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案 在struts2应用中使用ueditor富文本编辑器上传图片或者附件时,即使配置 ...

  4. use_frameworks!和#use_frameworks!的区别、解决Swift项目中use_frameworks!冲突的问题

    use_frameworks!和#use_frameworks!的区别 转自:https://www.jianshu.com/p/0ae58a477459 1. 用cocoapods 导入swift ...

  5. 谈谈MVC项目中的缓存功能设计的相关问题

    本文收集一些关于项目中为什么需要使用缓存功能,以及怎么使用等,在实际开发中对缓存的设计的考虑 为什么需要讨论缓存呢? 缓存是一个中大型系统所必须考虑的问题.为了避免每次请求都去访问后台的资源(例如数据 ...

  6. 在 ASP.NET MVC 项目中使用 WebForm、 HTML

    原文地址:http://www.cnblogs.com/snowdream/archive/2009/04/17/winforms-in-mvc.html ASP.NET MVC和WebForm各有各 ...

  7. MVC项目中如何判断用户是在用什么设备进行访问

    使用UAParser在C#MVC项目中如何判断用户是在用什么设备进行访问(手机,平板还是普通的电脑) 现在我们开发的很多web应用都要支持手机等移动设备.为了让手机用户能有更加好的用户体验,我们经常为 ...

  8. 搭建php环境时解决jpeg6 make: ./libtool:命令未找到

    搭建php环境时解决jpeg6 make: ./libtool:命令未找到 [root@bogon jpeg-6b]# make; make install ./libtool --mode=comp ...

  9. 在已有的Asp.net MVC项目中引入Taurus.MVC

    Taurus.MVC是一个优秀的框架,如果要应用到已有的Asp.net MVC项目中,需要修改一下. 1.前提约定: 走Taurus.MVC必须指定后缀.如.api 2.原项目修改如下: web.co ...

随机推荐

  1. AsyncTask和Handler的优缺点比较

    AsyncTask实现的原理和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口 ...

  2. HDU_2553——n皇后问题,作弊

    在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N,求出有多少种合法的放置方法.   Inp ...

  3. superslide2

    标签切换 / 书签切换 / 默认效果 http://www.superslide2.com/demo.html 4个Web前端经典实用值得学习收藏的地图实现模板~ http://www.iteye.c ...

  4. Linux安装mysql-python库时报错解决办法

    用pip安装mysql-python库的时候遇到如下报错 root@LoidAir:~# pip install mysql-python Collecting mysql-python Using ...

  5. Intellj Idea 2016.1.3的使用

    Intellj Idea 2016.1.3的使用:http://blog.csdn.net/bleachswh/article/details/51811055 极客学院教程:http://wiki. ...

  6. Mac OS X下HomeBrew安装卸载

    1.卸载 cd `brew --prefix` rm -rf Cellar brew prune rm `git ls-files` rm -r Library/Homebrew Library/Al ...

  7. Javascript:简单拖拽效果的实现

    核心代码: /* *完成一个拖拽事件由三大事件组成: *1:onmousedown:选择元素 *2:onmousemove:移动元素 *3:onmouseup:释放元素 */ function dra ...

  8. 线段树求逆序数方法 HDU1394&amp;&amp;POJ2299

    为什么线段树能够求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:能够从头開始往后找比当前元素小的值,也能够从后往前找比当前元素大的值,有几个逆序数就是几. 线段 ...

  9. response和request

    请求响应流程图 response response是用来向客户端响应的对象! 需要回忆一下http响应内容: l  首行:状态码 l  响应头: 1头1值,1头多值: l  响应体(正文):html ...

  10. C复习手记(Day3)

    C预处理器 C 预处理器不是编译器的组成部分,但是它是编译过程中一个单独的步骤.简言之,C 预处理器只不过是一个文本替换工具而已,它们会指示编译器在实际编译之前完成所需的预处理.我们将把 C 预处理器 ...