Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to find view (After publish) #54

Closed
aleksandaryanakiev opened this issue Nov 21, 2022 · 14 comments
Closed

Unable to find view (After publish) #54

aleksandaryanakiev opened this issue Nov 21, 2022 · 14 comments

Comments

@aleksandaryanakiev
Copy link

aleksandaryanakiev commented Nov 21, 2022

When I run locally my code everything works as expected, but after I publish it in docker I get this error:

`An Exception has occured when generating PDF Document: Unable to find view '~/Views/CashBookTemplate.cshtml'. The following locations were searched:
~/Views/CashBookTemplate.cshtml
Hint:

  • Check whether you have added reference to the Razor Class Library that contains the view files.`

Can you help?

@soundaranbu
Copy link
Owner

Hi @aleksandaryanakiev, thanks for reporting the issue. Is it possible for you to provide a minimal sample that reproduces the issue?

@gabs247
Copy link

gabs247 commented Dec 22, 2022

Hi, I've spent around an hour this morning trying to get a simple Unit Test working.

` [Test]
public async Task SendSmtpEmail()
{
// https://code.soundaranbu.com/send-emails-with-razor-templating-in-asp-net-core/

    WelcomeEmailModel? model = new WelcomeEmailModel
    {
        Name = "Test",
        Email = "test@test.com",
        CompanyName = "Razor.Templating.Core",
        SenderName = "Soundar"
    };

    string? body = await RazorTemplateEngine.RenderAsync("~/Views/WelcomeEmailTemplate.cshtml", model);

    SmtpClient? client = new SmtpClient
    {

`

I'd created a local Views folder in the NUnit Test project, but that wouldn't work. I've tried following your examples here: https://code.soundaranbu.com/send-emails-with-razor-templating-in-asp-net-core/ more closely by creating a separate class library for the EmailTemplates. Just continue to get:

_System.InvalidOperationException : Unable to find view '/Views/WelcomeEmailTemplate.cshtml'. The following locations were searched:

/Views/WelcomeEmailTemplate.cshtml
Hint:

  • Check whether you have added reference to the Razor Class Library that contains the view files.
  • Check whether the view file name is correct or exists at the given path.
  • Refer documentation or file issue here: https://github.com/soundaranbu/RazorTemplating_

Am I missing something here? I just wanted to use Razor rendering for email templates, and your library looked like a simple solution.

@soundaranbu
Copy link
Owner

Hi @gabs247, it's unfortunate that you are facing these issues.

I would like to know if you have followed all these steps here:
https://soundaranbu.medium.com/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79

You can also clone this repository and navigate to the examples folder or test cases to see if it's working for you. If it works then you can compare it to yours.

If still you are facing the issue, I am happy to take a look at a simple reproducible sample if you can provide one.

Awaiting your response. Thanks.

@gabs247
Copy link

gabs247 commented Dec 22, 2022

Thanks @soundaranbu, will try cloning when I have some time and see if I can get the samples to work.

I tried following those steps and the other examples to see if I'd missed something, I'm using .NET 6, but kept getting the error indicating that the View couldn't be found.

cheers

@soundaranbu
Copy link
Owner

I'm not able to reproduce this issue. Hence closing. Feel free to reopen if you can provide any sample project to reproduce the issue.
Thanks :)

@nsbgit
Copy link

nsbgit commented Feb 23, 2023

I am getting the same issue. Request to reopen.

@soundaranbu
Copy link
Owner

Hi @nsbgit, thanks for reporting your issue. As mentioned earlier, this can be reopened with a minimal sample that reproduces the issue.

I look forward to the sample. Thanks

@creativesuspects
Copy link

Not sure if this was the issue for the other users, but I was running into the same issue with the latest version (1.9.0). I got the same error after copy/pasting pieces of code from one project (where everything worked fine) to another. In the end I found out that I forgot to add the dependency in Startup.cs as described in the documentation:

In ASP.NET Core, add dependency like below in Startup.cs -> ConfigureServices

...
services.AddTransient<ExampleService>();
//add after registering all the dependencies
services.AddRazorTemplating();

@prabhpahul
Copy link

Hi, getting same issue, is it necessary to create RazorLibrary, what if we want to keep razor template in same project as web api project?

@soundaranbu
Copy link
Owner

Hey @prabhpahul, you don't need to. But you'll have to make your API's .csproj look similar to this https://github.com/soundaranbu/Razor.Templating.Core?tab=readme-ov-file#razor-views-in-library

@prabhpahul
Copy link

But I do have another project which works perfectly fine with out this. Also, how does only complaints in docker works fine if you run on IIS or dotnet run command locally through vs code

@soundaranbu
Copy link
Owner

Have you tried to publish in your local & run the build output? Did that work?

@prabhpahul
Copy link

prabhpahul commented Mar 19, 2024

Yes, it works as expected. Only when I run on it docker complains.

Also, I'm currently using Microsoft.NET.Sdk.Web changing to Razor Sdk is triggering lot of other changes.

I am adding manually reference to razor pages to make it work

  // builder.Services.AddRazorPages()
    //     .AddRazorRuntimeCompilation();

app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
            endpoints.MapControllers();
        });

@prabhpahul
Copy link

I was able to fix it, use was Template folder not mounting in docker after publish, had to add a itemgroup in cs proj,

<ItemGroup> <EmbeddedResource Include="PDFTemplates\KaizenPDFTemplate.cshtml"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </EmbeddedResource> </ItemGroup>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants