WebAPI filter interception processing and exception trapping

Source: Internet
Author: User

Intercept actions on action

  Public classUserauthorizeattribute:actionfilterattribute {/// <summary>        ///token for CC calls/// </summary>        //private static readonly string cctoken = configurationmanager.appsettings["Cctoken"]. ToString ();        /// <summary>        ///WEBAPI Request for monitoring/// </summary>        /// <param name= "Actioncontext" ></param>         Public Override voidonactionexecuting (Httpactioncontext ohttpactioncontext) {stringActionName = ohttpactioncontext.controllercontext.routedata.values["Action"].            ToString (); if(ActionName = ="Login"|| ActionName = ="postsendsms")            {                return; } httpcontextbase ohttpcontextbase= (httpcontextbase) ohttpactioncontext.request.properties["Ms_httpcontext"];//Get Traditional context            stringStoken = ohttpcontextbase.request.form["Token"];//Check the post first .            if(string. IsNullOrEmpty (Stoken)) Stoken= ohttpcontextbase.request.querystring["Token"];//and check it out from Get .//1. Verify token            if(string. IsNullOrEmpty (Stoken)) {httpresponsemessage Ohttpresponsemessage=NewHttpresponsemessage (); Ohttpresponsemessage.content=NewStringcontent ("{' Flag ': false, ' Message ': ' token is null ', ' resultobj ': null, ' Code ': ' 9101 '}", Encoding.UTF8,"Application/json"); Throw Newhttpresponseexception (ohttpresponsemessage); }            stringCctoken = configurationmanager.appsettings["Cctoken"].            ToString (); if(Stoken = =Cctoken) {                return; } logininfobiz loginbiz=Newlogininfobiz (); E_logininfo value=Loginbiz.gettoken (Stoken); if(Value = =NULL) {httpresponsemessage ohttpresponsemessage=NewHttpresponsemessage (); Ohttpresponsemessage.content=NewStringcontent ("{' Flag ': false, ' Message ': ' token invalidation ', ' resultobj ': null, ' Code ': ' 9101 '}", Encoding.UTF8,"Application/json"); Throw Newhttpresponseexception (ohttpresponsemessage); } oHttpActionContext.ControllerContext.Request.Properties.Add ("Itcode", value.        Engcode); }    }

When an Ajax call is made to an exception in error, it gets the exception information thrown in the interceptor.

function Apitest () {$ ("#result"). HTML (""); $.ajax ({type: $ ("#type"). Val (), DataType:"JSON", URL: $ ("#url"). Val (), Data:eval ("("+ $("#data"). Val () +")"), success:function (results) {if(typeofResults = ="Object") {                        $("#result"). HTML (json.stringify (results)); }                    Else {                        varResultobj =Json.parse (results); $("#result"). HTML (results); }                    //when used, it needs to be converted to a JSON object}, Error:function (XMLHttpRequest, Textstatus, Errorthrown) {alert                (Xmlhttprequest.responsetext);        }            }); }

WebAPI filter interception processing and exception trapping

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.