We have a middleware extension named DeveloperExceptionPageMiddleware, which returns a razor view. According to source code on github it returns a ErrorPage.cshtml page using preCompiled source code ErrorPage.Designer.cs.
My Question : How ErrorPage.Designer.cs was generated from ErrorPage.cshtml? I want to do the same thing for my .cshtml page.
What I have tried so far : I looked at RazorGenerator but its not generating the same view as I see in github source code. To be precise ExecuteAsync method is not present in Generated file and it looks different than ErrorPage.Designer.cs for same ErrorPage.cshtml.
This is not duplicate of aspnetcore: how to return a view from middleware. I am sure solution mentioned in this question was valid on the time when it was accepted but all the links in answers are broken as of today and RazorGenerator which was referred in answer is also changed a lot.
Update: Just to be clear, I don't want to override or implement exception handling in middlewares. I want to return a UI page from Web API for diagnostic purpose which will be used across all web APIs in our ecosystem.I saw DeveloperExceptionPageMiddleware as inspiration on how to return razor page from Web API without referencing the "View()", and just want to understand how do they do it. The missing piece here is how to generate Designer.cs from .cshtml, that's all I want to know.
Example: https://{host}/deployedlibs => Provide all the information of deployed libraries https://{host}/settings => Provide all information of appSetting.json
Endpoints are protected from Authentication hence not worried about security and these are internal APIs
Implementation for DevelopmentExceptionPage:
Its returning an html using ErrorPage.Designer.cs
See https://github.com/aspnet/Diagnostics/blob/master/src/Microsoft.AspNetCore.Diagnostics/DeveloperExceptionPage/DeveloperExceptionPageMiddleware.cs#L197
var model = new ErrorPageModel
{
Options = _options,
ErrorDetails = _exceptionDetailsProvider.GetDetails(ex),
Query = request.Query,
Cookies = request.Cookies,
Headers = request.Headers
};
var errorPage = new ErrorPage(model);
return errorPage.ExecuteAsync(context);
ErrorPage class is present in ErrorPage.Designer.cs which is generated from ErrorPage.cshtml.
So model is passed into a class and then response was written into stream. Can you tell me how to create "x.Designer.cs" from "x.cshtml"?