分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

Asp.Net Core 404处理

发布时间:2023-09-06 02:26责任编辑:胡小海关键词:暂无标签

在使用Asp.Net Core Mvc时 404处理整理如下

一、自带404状态处理

1.控制器视图子弹404视图 NotFoundResult,NotFoundObjectResult

 ???????// ???????// 摘要: ???????// ????Creates an Microsoft.AspNetCore.Mvc.NotFoundObjectResult that produces a Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound ???????// ????response. ???????// ???????// 返回结果: ???????// ????The created Microsoft.AspNetCore.Mvc.NotFoundObjectResult for the response. ???????[NonAction] ???????public virtual NotFoundObjectResult NotFound(object value); ???????// ???????// 摘要: ???????// ????Creates an Microsoft.AspNetCore.Mvc.NotFoundResult that produces a Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound ???????// ????response. ???????// ???????// 返回结果: ???????// ????The created Microsoft.AspNetCore.Mvc.NotFoundResult for the response. ???????[NonAction] ???????public virtual NotFoundResult NotFound();

2.当前操作返回404状态,或者返回404的一句话提示。

二、自定义404页面显示

在网站中,为了增强提前,通常使用自定义404页面

1.自定义404视图,在控制器中返回

 ???/// <summary> ???/// 定义404视图 ???/// </summary> ???public class NotFoundViewResult : ViewResult ???{ ???????public NotFoundViewResult(string viewName) ???????{ ???????????ViewName = viewName; ???????????StatusCode = (int)HttpStatusCode.NotFound; ???????} ???}

2.在控制器中返回使用

 ???????public IActionResult Index() ???????{ ???????????//返回404页面 ???????????return new NotFoundViewResult("~/views/Error/code_404.cshtml"); ???????????return View(); ???????}

3.呈现结果:

三、更多错误处理

更多:

Asp.Net Core异常处理整理

.Net Core邮件发送之MailKit

Asp.Net Core中Json序列化处理整理

Asp.Net Core 404处理

原文地址:https://www.cnblogs.com/tianma3798/p/10122910.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved