Asp.net mvc outputs custom 404 error pages, not 302 jump ., Mvc404, etc.

Source: Internet
Author: User
Tags asp net

Asp.net mvc outputs custom 404 error pages, not 302 jump ., Mvc404, etc.

A friend asked me a question about how to output a custom error page without 302 redirection. The current page address cannot be changed.

You also need to execute some code to generate some error messages so that you can submit feedback.

 

500 error. The mvc framework already has a ready-made solution:

filters.Add(new HandleErrorAttribute());

404 the current solution to the error is as follows:

First go to the Code Global. asax:

1 protected void Application_Error (object sender, EventArgs e) 2 {3 var ex = Server. getLastError () as HttpException; 4 if (ex = null) 5 return; 6 7 var httpStatusCode = ex. getHttpCode (); 8 9 if (httpStatusCode = 404) 10 {11 var httpContext = (sender as MvcApplication ). context; 12 13 httpContext. clearError (); 14 httpContext. response. clear (); 15 httpContext. response. statusCode = 404; 16 ServiceFocus. logService. addLog (ex); 17 18 httpContext. response. contentType = "text/html; charset = UTF-8"; 19 var routeData = new RouteData (); 20 routeData. values ["controller"] = "Sys"; 21 routeData. values ["action"] = "NotFound"; 22 var requestContext = new RequestContext (new HttpContextWrapper (httpContext), routeData); 23 var controller = ControllerBuilder. current. getControllerFactory (). createController (requestContext, "Sys") as SysController; 24 // controller. viewData. model = model; 25 (controller as IController ). execute (requestContext); 26 ControllerBuilder. current. getControllerFactory (). releaseController (controller); 27} 28}View Code
Controller code:
1 public class CompressAttribute: ActionFilterAttribute 2 {3 public override void OnActionExecuting (ActionExecutingContext filterContext) 4 {5 var acceptEncoding = filterContext. httpContext. request. headers ["Accept-Encoding"]; 6 if (! String. isNullOrEmpty (acceptEncoding) 7 {8 acceptEncoding = acceptEncoding. toLower (); 9 var response = filterContext. httpContext. response; 10 if (acceptEncoding. contains ("gzip") 11 {12 response. appendHeader ("Content-encoding", "gzip"); 13 response. filter = new GZipStream (response. filter, CompressionMode. compress); 14} 15 else if (acceptEncoding. contains ("deflate") 16 {17 response. appendHeader ("Content-encoding", "deflate"); 18 response. filter = new DeflateStream (response. filter, CompressionMode. compress); 19} 20} 21} 22} 23 [Compress] 24 public class SysController: Controller25 {26 // 27 // GET:/sys/28 29 public ActionResult NotFound () 30 {31 return View (); 32} 33 public ActionResult Error () 34 {35 return View (); 36} 37}View Code

Web. config

<system.webServer>    

At present, there are a few questions, not to go into depth: I hope some netizens know that they can solve the problem, so they don't have to go to google to find the source code.

 

1. if this line of code is not added, text/html is output by default; the browser outputs the content directly without parsing.

httpContext.Response.ContentType = "text/html; charset=utf-8";

2. iis does not use gzip compression. No matter how large the output 404 error page is, it will not be automatically compressed. Therefore, use the following replacement method.

  [Compress]    public class SysController : Controller

Guess:

Mvc has done many things after the Execute stage of action, such as. Normal 200 requests will Execute the default filter stage.

When it is a 404 request, these stages are skipped. Maybe 500 requests are similar.

Just speculation, not verified yet,


For a custom 404 error page, how can I redirect the address bar to the URL where 404 is located?

404 not found
The webpage is not found, indicating that the webpage has been deleted or renamed on the server. Add a blacklist in the Security Assistant and enter the website address of the website. It is not allowed to pop up.
404 is an encoding of not found errors. The HTTP protocol error information may be different in different software and languages, but its code is uniform, so that the browser can correctly identify and handle the problem.
 
Asp net mvc hyperlink jump page problem my path is correct, but the jump is wrong, I don't know why

First, check whether the webpage you are redirected to is correctly written. It is likely that the webpage is in the same folder or in the same path, if so, simply write the name of the page. If not, write the name of the folder above "... /wenjianjia/wenjian. aspx ", hope to help you

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.