Rotativa gets 401.2 code status using windows authentication - asp.net-mvc-5

I have an MVC 5 project which uses Windows authentication.
In order of adding pdf export support, I try to set up Rotativa lib.
The first try was Controller method
public ActionResult PrintViewToPdf()
{
return new ActionAsPdf("myAction");
}
which results as error 401.2 page in pdf file.
As i undestand (please let me know if there is another solution!!!), there is the only workaround:
public ActionResult PrintViewToPdf()
{
return new ViewAsPdf("myView");
}
In this case, I get a pdf file, but it contents a web page withount javascript scripts completed!
So, the question: is any another solution instead of replace ActionAsPdf to ViewAsPdf? If no, is it a solution to make ViewAsPdf include javascript execution results?

Related

Blazor Component (in library) and JSInterop

I've created a Blazor Component within a full Blazor project and all works well.
However, when I move this component to it's own Razor Class Library project, I am now getting an error that I cannot use JSInterop until a connection with the server is made. I am running my code in the OnAfterRenderAsync() method.
I had to alter the code a little when I made the change.
In a full Blazor project, JSInterop is provided for you with DI in the Startup class. But this is not the case with a calss library.
So instead of "#inject JSInterop js" in the page, I had to set it up like this -
private IJSRuntime js { get; set; }
protected override void OnInitialized()
{
js = ScopedServices.GetRequiredService<IJSRuntime>();
}
From the sketchy details available on the web, I'm assuming this gets the service from the Parent project.
Using debugging, I can see that js is NOT null. It does seem to have been set to a valid object.
Can anyone point me in the right direction?
The server pre-renders, so your code will run before there is a client connection to the server. When rendering in OnAfterRenderAsync you should only use IJSRuntime when the firstRender parameter is true, or any point after that, but never before.
Found the solution to my problem and it has rendered my initial question irrelevant.
When I copied my component to it's own class library project, it would not compile. It gave me an error on the line #inject JSInterop js.
This led me to believe that it didn't know how to inject this as it is not set during the Startup of the project, as it is in a Blazor app.
So I cobbled together the code to get a reference via ScopedServices.GetRequiredService().
This did create an object but did not have _clientProxy set which contains the connection to the server.
So digging round I managed to find a complete component library example project at BlazorHelp Website
This did have the JSInterop injected in the Blazor file. So I reverted my code back to the original code that worked in the full project and tried to compile. It gave me the same error. So I deleted the #inject JSInterop js line and typed it in again.
IT RECOGNIZED IT!!!
It still failed to compile, not recognizing a custom type (Pivot) and asking whether I had included a reference to it.
[CascadingParameter] public Pivot OwnerPivot { get; set; }
I deleted the word Pivot and retyped it and, voila, it compiled.
So it looks like there some sort of error in VS2019 or the razor compiler, where deleting code in the source file and re-entering caused it to recognize and compile.

Purpose of Error method in asp.net Core 2.0 MVC project template

I'm new to asp.net Core 2.0.
When you Create a new ASP.net Core 2.0 MVC Project (Web Application (Model-View-Controller)), you get a HomeController with Index(), About() and Contact() methods as in MVC 5.
However you also get this:
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
I can't find any documentation in Google or in any books as to what this is for and what the purpose of the single line of code is.
Can somebody please explain it? Is it some kind of best practice - should we be using this?
Thanks.
If time permit, I'll try to update my answer. But for now, the brief explanation is that every public method in your controller is callable as an http endpoint. For instance, the index method in your HomeController renders your index.cshtml view, the About renders the about.cshtml view and so on. In particular the public IActionResult Error() method is called if you redirect the user to the action when an error occurred. Consequently, it returns an Error View containing the information about the error, such as the RequestId and other values bonded with the ErrorViewModel.
I hope this help for now.

Why am I getting "A route named 'swagger_docs' is already in the route collection" after I publish my API App?

After publishing my API App I'm getting the yellow error screen of ASP.NET. The error message says "A route named 'swagger_docs' is already in the route collection".
How can I fix this?
This is not related to API Apps per se but more around Web API. What triggers the error is pretty simple:
You publish the API App which is based on Web API.
You discard your project and start working on a new API App based on Web API
You want to publish the new API App instead of the old API App you created at step 1.
You select the API App during "Publish.." and you get the publishing profile of the existing API App we deployed at step 1.
You deploy using Web Deploy and the publishing profile, the new API App on top of the old one.
That will trigger the issue I've explained before. That happens because there are two routes being registered by Swashbuckle when you try to start the app. One of the old one and one of the new one. That's because the old files are still present at the destination.
To solve this, during Web Deploy, click on the Settings tab and then expand the "File Publish Options". There is a checkbox there, called "Remove additional files from destination". This will fix the issue as it will only leave the files you deploy at the destination and not the old ones as well.
Hope it helps.
What if it happens when trying to debug the app locally ?
This happened for me, and the reason was, I renamed my assembly name. So the bin folder had two dlls for the same project with different names which caused this error. Once I deleted the old named dll all is well. Hope this helps.
This happens because You probally are configuring you route in your WebApiConfig class and SwaggerConfig class, as explained below:
WebApiConfig file:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
SwaggerConfig.Register();
}
}
SwaggerConfig file:
using Swashbuckle.Application;
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
namespace NEOH.Api
{
public class SwaggerConfig
{
public static void Register()
{
What you should do is remove the assembly call on SwaggerConfig file.
It should work.
My Solution & Cause:
I had the same problem when I renamed NamesSpaces,Refactored,etc.
After reading what everyone else did here's what I tried:
Cleaned the Solution in Visual Studio
Cleaned the Bin folder manually
Checked the nameSpace in the Project Properties (copied it just in case) >> Build tab >> Scrolldown to Output and ensure the XML documentation file is correct. You will need this name later.
Opened up: SwaggerConfig.cs >> fixed the name space in here (copy,paste) c.SingleApiVersion("vX","NameSpace")
Scrolled down until I found: GetXmlCommentsPath() copied and pasted the correct name space in the .xml file path.
Ran, smoke tested, finished this post.
My issue was that I was referencing another project that had the Swashbuckle extension.
Here is how I kept both projects without changing the anything in project that was referenced:
Remove the routes created by the project referenced under SwaggerConfig.cs > Register right before GlobalConfiguration.Configuration.EnableSwagger(...).EnableSwaggerUi(...);:
// Clears the previous routes as this solution references another Swagger ASP.NET project which adds the swagger routes.
// Trying to add the Swagger routes more than once will prevent the application from starting
GlobalConfiguration.Configuration.Routes.Clear();
Then, the application will be able to start, but you will see the operations/functions that are in both projects. To remove the operations from the project being referenced...
Create the following class
using Swashbuckle.Swagger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Description;
namespace yournamespace.Models
{
/// <summary>
/// This class allows to manage the Swagger document filters.
/// </summary>
public class SwaggerCustomOperationsFilter : IDocumentFilter
{
/// <summary>
/// Applies the Swagger operation filter to exclude the Swagger operations/functions
/// that are inherited by the other Swagger projects referenced.
/// </summary>
///
/// <param name="p_swaggerDoc">Swagger document</param>
/// <param name="p_schemaRegistry">Swagger schema registry</param>
/// <param name="p_apiExplorer">Api description collection</param>
public void Apply(SwaggerDocument p_swaggerDoc, SchemaRegistry p_schemaRegistry, IApiExplorer p_apiExplorer)
{
IEnumerable<ApiDescription> externalApiDescriptions = p_apiExplorer.ApiDescriptions
.Where(d => d.ActionDescriptor.ControllerDescriptor.ControllerType.Module.Name != GetType().Module.Name);
IEnumerable<int> externalApiDescriptionIndexes = externalApiDescriptions
.Select(d => p_apiExplorer.ApiDescriptions.IndexOf(d))
.OrderByDescending(i => i);
IEnumerable<string> externalPaths = externalApiDescriptions.Select(d => $"/{d.RelativePathSansQueryString()}");
foreach (string path in externalPaths)
{
p_swaggerDoc.paths.Remove(path);
}
foreach (int apiDescriptionIndex in externalApiDescriptionIndexes)
{
p_apiExplorer.ApiDescriptions.RemoveAt(apiDescriptionIndex);
}
}
}
}
And add the following in SwaggerConfig.cs > Register > GlobalConfiguration.Configuration.EnableSwagger(...)
c.DocumentFilter<SwaggerCustomOperationsFilter>();
Alternative cause of this problem:
Seems like a lot of people have this issue resolved by deleting their "bin" and "obj" folders as per the other answers.
However the cause of the issue might be that you are configuring your Swagger Config in a referenced project, as per this comment: https://github.com/domaindrivendev/Swashbuckle/issues/364#issuecomment-226013593
I received this error when one project with Swagger referenced another
project with Swagger. Removing the reference fixed the problem.
This caused me to split some core functionality out into a Third project that both of my API's could reference, rather than them referencing each other.

Using hooks to trigger a process

I am trying to work out how to use the Hooks and just can't seem to get the syntax correct.
I have built a site using PirahnaCMS that has a blog component and am extending it to call some social plugins and auto post to FB, Twitter etc.
I just can't seem to get the syntax correct though. My app is MVC and I have looked at this section
1.2 ASP.NET MVC
If you're using ASP.NET MVC hooks should be attached in you Global.asax.cs in the Application_Start method, or any other place where you keep you startup code. You attach you hooks with the followin syntax:
protected void Application_Start() {
Piranha.WebPages.Hooks.Menu.RenderItemLink = (ui, str, title, url) => {
str.Append(String.Format("<span>{1}</span>", url, title)) ;
} ;
}
The Hook I believe I want to use is Piranha.WebPages.Hooks.Manager.PostEditModelAfterSave but for the life of me I can't seem to work it out.
All of the hooks are just static delegates that you can attach methods to. In the above example an anonymous method has been assigned to the hook with the syntax:
delegate += (parameters) => { method body }
You could also assign a previously declared method.
delegate += MyMethod
Example skeletons for attaching hooks should be available in the Docs at the official site. If not you can find the hooks in the file:
~/WebPages/Hooks.cs
And all delegates in:
~/Delegates.cs
I hope these URL:s are correct as I'm typing from memory :)
Regards

Cakephp 2.2 and SWFUploader black-holed

I am having a typical file upload method (inside a plugin) in my app and I am using for this Uploadify that uses SFWUpload.
I used this for an application that is written in CakePHP 1.3 (and it worked OK). Now I am updating the app to CakePHP 2.2.2. The problem is that when I am trying to upload the file I am getting a Security black-holed error (400 error).
I have disabled the Security for the uploader action inside the beforeFilter() callback but no success.
if($this->request->action == 'add_profile_picture'){
$this->Security->enabled = false;
}
I have also disabled the Auth for that function so I don't have any problems on upload regarding the passing of Session and Flash...
...
$this->Auth->allow('add_profile_picture');
...
If anyone had similar problems and solve it please give me a hint or two.
Thanks
Is this flash uploader using hidden fields via post that are NOT generated by the CakePHP FormHelper? If yes you'll need to whitelist these fields in the security component to make them pass.
From what I can tell, 'enabled' isn't actually a property of the Security component. You can instead use the validatePost attribute to achieve the same.
public function beforeFilter() {
if($this->request->action == 'your_action_name'){
$this->Security->validatePost = false;
}
parent::beforeFilter();
}

Resources