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