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. Andoid实现手动绘图

    public class MainActivity extends Activity { int width,height; private GameView gameview; private Ca ...

  2. enable ide

    http://pve.proxmox.com/wiki/Migration_of_servers_to_Proxmox_VE The vmware system consists of two dis ...

  3. python部落刷题宝学到的内置函数(二)

    感觉到刷题宝有一个好处,也许也不是好处,它的答案必须是真正输出的值,也就是说应该输出字符串aaaa的时候,答案必须写成界面上返回的值,即'aaaa'.有利于真正记忆返回值类型,但是....太繁琐了 1 ...

  4. spring aop实现原理

    通过两种代理方式,一是JDK本身的代理方式,二是CGLIB提供的代理方式,在代理类的前面加事务begin,在后面加事务commit,需要的数据库连接从ThreadLocal中取

  5. [深入React] 8.refs

    我们可以从 this.refs.xxx 获取到对象,有俩种情况: <input type="text" ref="name"/> 取到的是DOM元素 ...

  6. c#序列化反序列化工具(json,binary,xml)

    using System; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Bina ...

  7. Android利用setLayoutParams在代码中调整布局(Margin和居中)

    我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...

  8. DLNA介绍(包含UPnP,2011/6/20 更新)

    这部分的内容大多来源于网络及官方文档,依照自己的翻译理解整理所成.东西比較多,从头慢慢看还是能够懂个大概的. 文件夹: 一.DNLA的建立 二.DLNA的成员 三.DLNA标准的制定 四.DLNA的设 ...

  9. (一)一个工作任务引起的乱战——c#中结构体与byte[]间相互转换

    一个工作任务涉及到c#与c++系统间的udp通信,处理了蛮长时间没有完成任务,但是期间接触到不少小知识点.本人是初接触c#,c++语言没有接触过.可能写的东西都很小儿科,暂且记录下来当工作日记把. 先 ...

  10. Aspose.Words导出dt到word的问题

    已解决:单挑数据导入到一个word文档,导出文字和图片成功,执行Main();方法导出dt到word 待解决:多条数据(文字,图片的导入到一个word文档里面)从dt导入到word;已发现的错误提示“ ...