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. delphi7调用webservice Java 传入参数为空

    在delphi7中,new-webservices-wsdl importer中输入wsdl地址,会自动生成wsdl单元代码.在调用时,传入参数到服务器端时为空了. 网上说缺少 InvRegistry ...

  2. 如何在Excel中启用宏?

    OFFICE2003版本中启用宏的方法: 1.首先打开EXCEL应用程序. 2.点击上方的"工具"--"宏"--"安全性" 3.在" ...

  3. HDOJ(HDU) 1465 不容易系列之一(错排)

    Problem Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好"一件"事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就 ...

  4. 如何修改Oracle用户密码

    大家如果不知道oracle数据库的密码的话,我们可以通过简单的命令行把密码进行修改. 1.打开cmd 2 在弹出的命令提示窗口输入 set oracle_sid=数据库名称(实例),回车.例如数据库名 ...

  5. nova-network

    nova network-create demo-net --bridge br100 --multi-host T --fixed-range-v4 192.168.3.0/26 192.168.3 ...

  6. window.open的小技巧分享

        今天再次谈起window.open是因为发现了一个比较好玩的小技巧,详细内容我们稍后详细说明.       聊到window.open,不得不说明一下他的使用方法,主要有两种形式:   win ...

  7. Eclipse Tomcat : Project facet Java version 1.7 is not supported.

    在Eclipse打开一个项目,并用Tomcat运行时,报错:Project facet Java version 1.7 is not supported. 大致的截图如下: "项目中的jd ...

  8. Redis环境搭建(Linux)

    1.简介       redis是一个开源的key-value数据库.它又经常被认为是一个数据结构服务器.因为它的value不仅包括基本的string类型还有 list,set ,sorted set ...

  9. 实战:推断mysql中当前用户的连接数-分组筛选

    #connets.sh #!/bin/sh #ocpyang@126.com #依据输入參数u或d来显示出相应的username或数据库名中用户的连接数. #也能够输入u 详细username或d 详 ...

  10. 设计模式 - 命令模式(command pattern) 具体解释

    命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对 ...