[ASP. net mvc] Embarrassment Between Ajax and customerrors

Source: Internet
Author: User

In ASP. NETProgramTo display user-friendly error information, the following settings are usually performed in Web. config:

 
<CustomerrorsMode= "Remoteonly"Defaultredirect= "/Error/error.htm"></Customerrors>

However, if an error occurs on the server side for an Ajax request, it will be embarrassing. We have encountered such awkwardness. See:

"Sorry! System Error !" Is the content loaded by Ajax.CodeAs follows:

$. Ajax ({success:Function(Data ){If(Data) {resultelement.html (Data );}}});

As you can see from the code above, although the Ajax request has an error, it is still processed in the success callback function, resulting in displaying the custom error information as normal returned content, resulting in embarrassment in the previous figure.

When faced with this problem, the solution we came up with was to judge based on statuscode. If it was 500, we knew that an error had occurred and then specific error handling was performed. We wrote the following test code:

$. Ajax ({statuscode :{500:Function() {Console. Log ('Error! ') ;}}, Success:Function(Data ){If(Data) {resultelement.html (''+Data );}}});

The result shows that no callback function corresponding to 500 statuscode is executed.

After checking in the browser, the server returns the 302 status code. That is to say, ASP. NET returns a custom error message to the browser by default through redirection. In web. config> customerrors, there is a special attribute redirectmode. The default value is responseredirect. Redirectmode has another value: responserewrite. can we solve our problem? Modify web. config as follows:

<CustomerrorsMode= "Remoteonly"Defaultredirect= "/Error/error.htm"Redirectmode= "Responserewrite"></Customerrors>

The result shows that the 500 status code is indeed returned, but the custom error is missing. The runtime error is returned.

When redirectmode = "responserewrite" is set and an error occurs, ASP. NET will actually executeServer. Transfer () returns the custom error information page, while server. Transfer () and ASP. net mvc routes are compatible. For details, seeCustomerrors does not work when setting redirectmode = "responserewrite ".

The server cannot find a solution for the moment. Try it on the browser side.

We thought of a solution, that is, Processing Based on 302 statuscode. According to our actual scenario (redirectmode is the default value of responseredirect), if the server returns 302, it is definitely an error. So we changed it to the following Ajax code:

 
$. Ajax ({statuscode :{302:Function() {Console. Log ('Error! ') ;}}, Success:Function(Data ){If(Data) {resultelement.html (''+Data );}}});

The result shows that the callback function of 302 is not executed. That is to say, the Ajax request cannot get the HTTP status code. The actual result is the 302 status code.

Since no solution can be found on the browser side, only "turning back to the bank" is returned to the server side.

Since customerrors cannot solve the problem, we will discard it:

<CustomerrorsMode= "Off"></Customerrors>

Then, process the custom error information and add the following code to global. asax. CS:

Protected VoidApplication_error (Object sender, eventargs e) {exception lasterror=Server. getlasterror ();If(Lasterror! =Null) {Response. statuscode=500; Response. writefile ("~ /Error/error.htm"); Server. clearerror ();}}

The problem is solved in this way!

In addition, the default "Redirect display custom error information" method is not used. When an error occurs, the browser address bar does not jump. In this way, when the user reports an error, you can directly report the complete website address accessed when a problem occurs. Screenshot to commemorate the troubles of customerrors.

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.