PageResult always returns null in IAsyncPageFilter - OnPageHandlerExecutionAsync in Asp.Net Core 2.0 Razor...












0















I'm using a ModelValidationFilter to handle model validation errors for all my post or put requests. I'm using IAsyncPageFilter and registering as a global filter. In the OnPageHandlerExecutionAsync method, I'm able to handle the validation errors for ajax requests and send back a json response. But for non ajax request,



I'm getting always null for var result = (PageResult)context.Result;



Please could you assist me on this?




I'm implementing this so that I don't need to write model validation in all post or put handlers in any razor page in my application.




Here is my implementation:



if (context.HttpContext.Request.Method.Equals("POST") || context.HttpContext.Request.Method.Equals("PUT"))
{
if (!context.ModelState.IsValid)
{
if (context.HttpContext.Request.IsAjaxRequest())
{
var errorModel = context.ModelState.Keys.Where(x => context.ModelState[x].Errors.Count > 0)
.Select(x => new
{
key = x,
errors = context.ModelState[x].Errors.Select(y => y.ErrorMessage).ToArray()
});

context.Result = new JsonResult(new AjaxResultHelper<IEnumerable<object>>
{
Response = errorModel,
Message = "_InvalidData_"
});
}
else
{
var result = (PageResult)context.Result;

context.Result = new PageResult
{
ViewData = result.ViewData,
ContentType = result.ContentType,
StatusCode = 400,
};
}
}
}
else
{
await next.Invoke();
}









share|improve this question

























  • You should just be able to do context.Result = new PageResult();.

    – Kirk Larkin
    Dec 31 '18 at 13:27











  • @KirkLarkin: what about the ViewData and ContentType that needs to be passed to the PageResult

    – Abdul
    Dec 31 '18 at 15:50


















0















I'm using a ModelValidationFilter to handle model validation errors for all my post or put requests. I'm using IAsyncPageFilter and registering as a global filter. In the OnPageHandlerExecutionAsync method, I'm able to handle the validation errors for ajax requests and send back a json response. But for non ajax request,



I'm getting always null for var result = (PageResult)context.Result;



Please could you assist me on this?




I'm implementing this so that I don't need to write model validation in all post or put handlers in any razor page in my application.




Here is my implementation:



if (context.HttpContext.Request.Method.Equals("POST") || context.HttpContext.Request.Method.Equals("PUT"))
{
if (!context.ModelState.IsValid)
{
if (context.HttpContext.Request.IsAjaxRequest())
{
var errorModel = context.ModelState.Keys.Where(x => context.ModelState[x].Errors.Count > 0)
.Select(x => new
{
key = x,
errors = context.ModelState[x].Errors.Select(y => y.ErrorMessage).ToArray()
});

context.Result = new JsonResult(new AjaxResultHelper<IEnumerable<object>>
{
Response = errorModel,
Message = "_InvalidData_"
});
}
else
{
var result = (PageResult)context.Result;

context.Result = new PageResult
{
ViewData = result.ViewData,
ContentType = result.ContentType,
StatusCode = 400,
};
}
}
}
else
{
await next.Invoke();
}









share|improve this question

























  • You should just be able to do context.Result = new PageResult();.

    – Kirk Larkin
    Dec 31 '18 at 13:27











  • @KirkLarkin: what about the ViewData and ContentType that needs to be passed to the PageResult

    – Abdul
    Dec 31 '18 at 15:50
















0












0








0


1






I'm using a ModelValidationFilter to handle model validation errors for all my post or put requests. I'm using IAsyncPageFilter and registering as a global filter. In the OnPageHandlerExecutionAsync method, I'm able to handle the validation errors for ajax requests and send back a json response. But for non ajax request,



I'm getting always null for var result = (PageResult)context.Result;



Please could you assist me on this?




I'm implementing this so that I don't need to write model validation in all post or put handlers in any razor page in my application.




Here is my implementation:



if (context.HttpContext.Request.Method.Equals("POST") || context.HttpContext.Request.Method.Equals("PUT"))
{
if (!context.ModelState.IsValid)
{
if (context.HttpContext.Request.IsAjaxRequest())
{
var errorModel = context.ModelState.Keys.Where(x => context.ModelState[x].Errors.Count > 0)
.Select(x => new
{
key = x,
errors = context.ModelState[x].Errors.Select(y => y.ErrorMessage).ToArray()
});

context.Result = new JsonResult(new AjaxResultHelper<IEnumerable<object>>
{
Response = errorModel,
Message = "_InvalidData_"
});
}
else
{
var result = (PageResult)context.Result;

context.Result = new PageResult
{
ViewData = result.ViewData,
ContentType = result.ContentType,
StatusCode = 400,
};
}
}
}
else
{
await next.Invoke();
}









share|improve this question
















I'm using a ModelValidationFilter to handle model validation errors for all my post or put requests. I'm using IAsyncPageFilter and registering as a global filter. In the OnPageHandlerExecutionAsync method, I'm able to handle the validation errors for ajax requests and send back a json response. But for non ajax request,



I'm getting always null for var result = (PageResult)context.Result;



Please could you assist me on this?




I'm implementing this so that I don't need to write model validation in all post or put handlers in any razor page in my application.




Here is my implementation:



if (context.HttpContext.Request.Method.Equals("POST") || context.HttpContext.Request.Method.Equals("PUT"))
{
if (!context.ModelState.IsValid)
{
if (context.HttpContext.Request.IsAjaxRequest())
{
var errorModel = context.ModelState.Keys.Where(x => context.ModelState[x].Errors.Count > 0)
.Select(x => new
{
key = x,
errors = context.ModelState[x].Errors.Select(y => y.ErrorMessage).ToArray()
});

context.Result = new JsonResult(new AjaxResultHelper<IEnumerable<object>>
{
Response = errorModel,
Message = "_InvalidData_"
});
}
else
{
var result = (PageResult)context.Result;

context.Result = new PageResult
{
ViewData = result.ViewData,
ContentType = result.ContentType,
StatusCode = 400,
};
}
}
}
else
{
await next.Invoke();
}






c# asp.net-core razor-pages






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 10:24







Abdul

















asked Dec 31 '18 at 7:51









AbdulAbdul

62




62













  • You should just be able to do context.Result = new PageResult();.

    – Kirk Larkin
    Dec 31 '18 at 13:27











  • @KirkLarkin: what about the ViewData and ContentType that needs to be passed to the PageResult

    – Abdul
    Dec 31 '18 at 15:50





















  • You should just be able to do context.Result = new PageResult();.

    – Kirk Larkin
    Dec 31 '18 at 13:27











  • @KirkLarkin: what about the ViewData and ContentType that needs to be passed to the PageResult

    – Abdul
    Dec 31 '18 at 15:50



















You should just be able to do context.Result = new PageResult();.

– Kirk Larkin
Dec 31 '18 at 13:27





You should just be able to do context.Result = new PageResult();.

– Kirk Larkin
Dec 31 '18 at 13:27













@KirkLarkin: what about the ViewData and ContentType that needs to be passed to the PageResult

– Abdul
Dec 31 '18 at 15:50







@KirkLarkin: what about the ViewData and ContentType that needs to be passed to the PageResult

– Abdul
Dec 31 '18 at 15:50














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53984956%2fpageresult-always-returns-null-in-iasyncpagefilter-onpagehandlerexecutionasync%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53984956%2fpageresult-always-returns-null-in-iasyncpagefilter-onpagehandlerexecutionasync%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas