在asp.net mvc 中,action方法里根据参数获取数据,假如获取的数据为空,为了响应404错误页,我们可以return HttpNotFound(); 但是在asp.net webform中,实现方式就不一样了。

为了体现本人在实现过程中的所遇到的问题,现举例来说明。

1. 在asp.net webform 中,新建一个WebForm1.aspx文件,WebForm1.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="PageNotFoundDemo.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
当你看到这行文字时,表示访问正常!
</body>
</html>

浏览时会显示如下的效果:

现在需要实现传参id,如果id=3时获取不到数据,响应404

WebForm1.aspx.cs文件如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace PageNotFoundDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["Id"];
if (id == "")
{
Response.StatusCode = ;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
}

访问之后发现,还是显示了文字“当你看到这行文字时,表示访问正常!”,而开发人员工具中监视的响应状态码是404。

这不是我想要的效果,我想要的效果如下(类似访问一个不存在的资源时响应的404错误页):

该问题困扰了我很久,甚至有查找过资料是通过配置Web.Config自定义成错误页去实现,但是与我想要的效果不一致,我想要的效果是响应默认的IIS (或IISExpress)中的404错误页。

某天也是在找该问题的解决方案,不经意间找到了解决方法:

Response.StatusCode = ;
Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();

Response.SuppressContent的解释如下:

修改后webform1.aspx.cs的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace PageNotFoundDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["Id"];
if (id == "")
{
Response.StatusCode = ;
Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
}

编译,再次访问,效果如下:

asp.net根据参数找不到记录后响应404及显示错误页的更多相关文章

  1. 从ASP.NET传递参数给水晶报表

    原文 http://www.cnblogs.com/insus/p/3281114.html 上次Insus.NET有简单写了一篇文章<Visual Studio 2012使用水晶报表Cryst ...

  2. asp.net mvc 不找其他view模板,只找cshtml

    asp.net mvc 默认找view文件时,依次找后辍名为aspx.ascx.cshtml.vbhtml的view文件.但是项目住住用C#+Razor开发,这样找,岂不有性能损失. 添加以下代码: ...

  3. asp.net core参数保护之自定义要保护的参数类型

    asp.net core参数保护之自定义要保护的参数类型 Intro 为了实现 asp.net core 下的参数保护,扩展了asp.net core 中 DataProtection,可以自动化的保 ...

  4. ASP.NET Web API 记录请求响应数据到日志的一个方法

    原文:http://blog.bossma.cn/dotnet/asp-net-web-api-log-request-response/ ASP.NET Web API 记录请求响应数据到日志的一个 ...

  5. [转]asp.net5中使用NLog进行日志记录

    本文转自:http://www.cnblogs.com/sguozeng/articles/4861303.html asp.net5中使用NLog进行日志记录 asp.net5中提供了性能强大的日志 ...

  6. ASP.NET Core 如何记录每次响应的Response信息 - sky 胡萝卜星星 - CSDN博客

    原文:ASP.NET Core 如何记录每次响应的Response信息 - sky 胡萝卜星星 - CSDN博客 上一篇文章中我们已经成功的记录了Request部分的信息,现在我们来看下如何记录Res ...

  7. 给 asp.net core 写个中间件来记录接口耗时

    给 asp.net core 写个中间件来记录接口耗时 Intro 写接口的难免会遇到别人说接口比较慢,到底慢多少,一个接口服务器处理究竟花了多长时间,如果能有具体的数字来记录每个接口耗时多少,别人再 ...

  8. ASP.NET MVC下自定义错误页和展示错误页的几种方式

    在网站运行中,错误是不可避免的,错误页的产生也是不可缺少的. 这几天看了博友的很多文章,自己想总结下我从中学到的和实际中配置的. 首先,需要知道产生错误页的来源,一种是我们的.NET平台抛出的,一种是 ...

  9. asp.net mvc项目使用spring.net发布到IIS后,在访问提示错误 Could not load type from string value 'DALMsSql.DBSessionFactory,DALMsSql'.

    asp.net mvc项目使用spring.net发布到IIS后,在访问提示错误 Could not load type from string value 'DALMsSql.DBSessionFa ...

随机推荐

  1. Golang标准库——io-结构

    结构 LimitedReader 定义 限制从Reader中读取的字节数. type LimitedReader struct { R Reader // underlying reader N in ...

  2. Ribbon使用Hystrix

    1.导入依赖spring-cloud-starter-hystrix <dependency> <groupId>org.springframework.cloud</g ...

  3. git reset命令使用

    版本回退 当前有三个commit提交版本commit1commit2commit3Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本上一个版本是HEAD^,上上一个版本是HEAD^^ ...

  4. rem布局原理深度理解(以及em/vw/vh)

    一.前言 我们h5项目终端适配采用的是淘宝那套<Flexible实现手淘H5页面的终端适配>方案.主要原理是rem布局.最近和别人谈弹性布局原理,发现虽然已经使用了那套方案很久,但是自己对 ...

  5. springboot+cloud 学习(六)Docker

    注:由于本人用的是window10系统,docker的版本是Docker Community Edition(Docker CE ). 什么是Docker 按官网的话说,Docker是一个世界领先的容 ...

  6. [Codeforces 1016F]Road Projects

    Description 题库链接 给你一棵 \(n\) 个节点的树,定义 \(1\) 到 \(n\) 的代价是 \(1\) 到 \(n\) 节点间的最短路径的长度.现在给你 \(m\) 组询问,让你添 ...

  7. csharp: Setting the value of properties reflection

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(第三部分)

    Earthstone Keeper Time Limit: 4 Seconds      Memory Limit: 65536 KB Earthstone Keeper is a famous ro ...

  9. JS处理数组内如果相同ID追加一个属性(如字体颜色)

    var arr=[{id:0},{id:0},{id:3},{id:2},{id:0},{id:4},{id:0},{id:1},{id:1},{id:2},{id:2}]; for(var i=0; ...

  10. AOP,过滤器,监听器,拦截器【转载】

    面向切面编程(AOP是Aspect Oriented Program的首字母缩写) ,我们知道,面向对象的特点是继承.多态和封装.而封装就要求将功能分散到不同的对象中去,这在软件设计中往往称为职责分配 ...