How to setup Rotativa HTML to PDF in ASP.NET Core 3? - asp.net-core-3.0

It seems RotativaConfiguration.Setup(env); is not working anymore in Core 3.

RotativaConfiguration.Setup(env.WebRootPath, "Rotativa");
"Rotativa" is the folder name housing the wkhtmltoimage.exe and wkhtmltopdf.exe in the root folder "wwwroot"

Work for me in ASP.NET Core 3.1 Project:
Install-Package Rotativa.AspNetCore -Version 1.1.1
Then in Startup.cs:
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
And in public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
RotativaConfiguration.Setup((IHostingEnvironment)env);

For Below ASP.NET Core 3 Use:
RotativaConfiguration.Setup(env);
For ASP.NET Core 3 Use:
RotativaConfiguration.Setup(env.WebRootPath, "Rotativa");
Add a new folder inside "wwwroot" with name “Rotativa” and inside this folder keep the wkhtmltoimage.exe and wkhtmltopdf.exe files.
Otherwise you will get error
cannot convert from 'Microsoft.AspNetCore.Hosting.IWebHostEnvironment' to 'Microsoft.AspNetCore.Hosting.IHostingEnvironment'

Related

Using Ikvm.dll's trying to do xmldocode.readobject() , but it throw indexoutofboundarray in web application and Console it works fine .net fram4.5

`<?xml version="1.0" encoding="UTF-8"?><java version="1.6.0" class="java.beans.XMLDecoder"><object class="cli.class.MyClass.WSRequest"><void method="set_idescription"><string>accountChangePswdProcessDesc</string></void><void method="set_iprocessState"><string>R</string></void><void method="set_iprocessStateString"><string>Running</string></void></object></java> in Dotnet framework 4.5
This the java xml fomat which, we need to decode using IKVM,obj = (TDotNetType)decoder.readObject();
Using IKVM Version 7.4.5.x and tried in above versions.
It works fine and can resolve the class name in Console application(Windows application). But while using the same code over the Web application it throw "Indexoutofboundarray".
stuck as it is something unexpected happing, tried different ver of dll's of ikvm in for .net 4.5 `

IApplication Builder does not contain a definition for UseWebpackDevMiddleware

I am following a tutorial to become familiar with server side rendering involving asp.net core and react, but I am hitting a roadblock on the HelloWorld. I am doing this alongside another co-worker and he is not getting this same issue. I have uninstalled and reinstalled all dependencies with npm to make sure there were no installation issues.
I have tried to find some answer as to what I am missing, an import etc, but cannot seem to find anyone with the same exact issue. I had looked at similar IApplicationBuilder exceptions but the solutions they usually have is try this import statement etc etc. I have uninstalled and reinstalled all my dependencies with npm. I have recreated the project three times
Below is the Configure method from my Startup.cs file.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware();
} else {
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
}
app.UseWebpackDevMiddleware(); is throwing the following error:
"Severity Code Description Project File Line Suppression State
Error CS1061 'IApplicationBuilder' does not contain a definition for 'UseWebpackDevMiddleware' and no accessible extension method 'UseWebpackDevMiddleware' accepting a first argument of type 'IApplicationBuilder' could be found"
In Package Manager run Install-Package Microsoft.AspNetCore.SpaServices.Extensions

aspnet core appsettings.json loading

I have ASP.NET Core (2.1) project that has appsettings.json. I use WebHost.CreateDefaultBuilder(). The appsettings.json file has following configuration in File Properties:
Build Action: Content
Copy to Output Directory: Do not copy
After build the appsettings.json ends up in bin\Debug\netcoreapp2.1\MyProj.runtimeconfig.json.
The ASP.NET Core runtime loads it fine.
I created WebJobs (for .Net Core 2.1) and wanted to do the same - set Build Action to Content and let it loaded. In the Main() of Program.cs I have code like
var builder = new HostBuilder()
...
.ConfigureAppConfiguration(b =>
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
b.SetBasePath(Directory.GetCurrentDirectory());
b.AddJsonFile("appsettings.json", false, true);
b.AddJsonFile($"appsettings.{environment}.json", true, true);
b.AddEnvironmentVariables();
// Adding command line as a configuration source
if (args != null)
{
b.AddCommandLine(args);
}
}
But the runtime tries to load appsettings.json (instead of MyWebJobProj.runtimeconfig.json). So I had to set Build Action to None and Copy to Output Directory to Always.
However I would prefer the same approach like in ASP.NET Core - it handles somehow the file name transformation. Although in WebHost.CreateDefaultBuilder() is basically the same code like I have in my WebJob. What does the magic file name transformation in the configuration and why it works only in one type of project?
The file [ProjName].runtimeconfig.json has a completely different meaning than appsettings.json. Ensure that appsettings.json was copied to output (set 'Copy to output' to 'always' or 'newer').

Entry point not found in .NET Core 2.0 DLL

I couldn't find anything that explains this -- for some reason my .NET Core 2.0 ASP.NET application does not run as a DLL via:
dotnet MyProject.Web.dll
And instead I get the exception:
Unhandled Exception: System.MissingMethodException: Entry point not found in assembly 'MyProject.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
namespace MyProject.Web
{
public class Program
{
public static void Main(string[] args)
{
LoadDependencies();
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
private static void LoadDependencies()
{
DependencyLocator.Instance.DefineIfUndefined<IDataProvider, DataProvider>();
}
}
}
It runs fine as a standalone executable (when targeting a "Console Application" in the project's config), but now that I'm trying to deploy to a server that needs it to run via the dotnet command (as a DLL, i.e. "dotnet .\MyProject.Web.dll"), it seems to be having issues. I get the above exception on both my server and my local development box.
I'm kind of blown away that it cannot locate the Main method -- it's declared as static and in Program.cs. Am I missing something?
(EDIT: To clarify, the DLL I'm trying to run against the "dotnet" command is from the target compiling as a "Console Library," since my server is explicitly asking for a DLL, since they will not run executables).
OK, so this is annoying and will hopefully help someone else out.
My host wants to specifically run DLL's thru .NET Core ONLY. They do not allow for executables to be run.
Because DLL's are frequently built as "Class Library" output types on the project, I assumed that this was the workflow necessary to build it. However, I found out that whenever you build your project as a "Console Application," it builds a DLL in addition to an EXE. So, in the above example, MyProject.Web.exe and MyProject.Web.dll are both built when the output type is "Console Application."
MyProject.Web.dll that comes from "Console Application" is different than MyProject.Web.Dll that comes from "Class Library." The one that comes from "Class Library" will NOT have an entry point that can be discovered on it, which will lead to the problem above.
So, if you're getting this error, look for the DLL that ships with your EXE of the same name -- that's the actual DLL you'll want to run in your dotnet console (i.e. dotnet MyProject.Web.dll)

How to step inside/debugging the code source of web.api 2 and System.Web dll

I wanted to step inside/debugging the web-api 2 (version 5.2.3) code source to understand it.
I've created a simple web api application, and in the WebApi.config, i've setted a break point at this line :
config.Routes.MapHttpRoute(
name: "ExcludedRoute",
routeTemplate: "api/{controller}/{action}",
defaults: new { id = RouteParameter.Optional },
constraints: new { Controller = "healthcheck", action = "check" }
);
I could step inside the MapHttpRoute method and the other classes, but the debugger couldn't step inside the Route class from the System.Web dll, below the image:
I tried to use this symbol file locations:
I've also used the dotpeek tool for generating the pdb files from the bin folder, by setting to true the "copy local" feature of the referenced assemblies, but with no success. The path of system.web dll is
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System.Web.dll
I'm wondering Why the debugger couldn't step inside the Route class wich belongs to the system.Web dll, its version is 4.0 and the version of web.api is the 5.2.3.
In my searching, i've downloaded the code source of the asp.net and i found that the project System.Web.Http.WebHost which contains the HttpWebRoute class.
Any idea?
Thanks!
Assembly System.Web.pdb was in the .NET Framework source code. You need to configure Visual Studio for debugging .NET framework in the Tools -> Options -> Debugging -> General menu.
Reference:
How do I debug .NET 4.6 framework source code in Visual Studio 2017?

Resources