|
WebPageControllerPostPage Method
|
Common page post handler.
Namespace: GSF.Web.HostingAssembly: GSF.Web (in GSF.Web.dll) Version: 2.4.207-beta
Syntax Remarks
For Ajax post-based requests to pages handled by this controller, the Ajax request will need to specify
verification token in the header and set flag that indicates an Ajax request. See example code below:
@{
string verificationHeader = AuthenticationOptions.DefaultRequestVerificationToken;
string useAjaxVerfication = AuthenticationOptions.DefaultAjaxRequestVerificationToken;
ReadonlyAuthenticationOptions options = ViewBag.AuthenticationOptions;
if (options != null) {
if (!string.IsNullOrWhiteSpace(options.RequestVerificationToken)) {
verificationHeader = options.RequestVerificationToken;
}
if (!string.IsNullOrWhiteSpace(options.AjaxRequestVerificationToken)) {
useAjaxVerfication = options.AjaxRequestVerificationToken;
}
}
string verificationValue = Html.RequestVerificationHeaderToken();
string constants = string.Format(@"
const verificationHeader = ""{0}"";
const verificationValue = ""{1}"";
const useAjaxVerfication = ""{2}"";
",
verificationHeader.JavaScriptEncode(),
verificationValue.JavaScriptEncode(),
useAjaxVerfication.JavaScriptEncode()
);
}
@section Scripts {
<script>
"use strict";
@Raw(new Minifier().MinifyJavaScript(constants));
function doAjaxThing() {
$.ajax({
cache: false,
url: "AjaxThing.ashx",
method: "post",
data: { value: "some data to post" },
dataType: "application/json",
success: function (result) {
console.log("Success: " + result);
},
beforeSend: function (xhr) {
xhr.setRequestHeader(verificationHeader, verificationValue);
xhr.setRequestHeader(useAjaxVerfication, "true");
}
});
}
</script>
}
See Also