Using Razor Templates Stored in Razor Class Library 3.0 (RCL) Without AspNetCore Web Application - asp.net-mvc

I am using Razor templates (cshtml) to build HTML for use in emails. While I can get this all to work and I can send email from a ASPNetCore 3.0 web application I want to actually send email from another process which is NOT a web application. Ideally I would like to queue email, which can be triggered from any number of backend processes, and then send those from a timer like service which runs as a windows service or possibly a back-end worker running in Azure via something like Functions.
Is there any way to leverage razor templates from within a RCL in a .NetCore console or other application which is NOT hosted in AspNetCore??

I think there isn't a way to do this i am sry. :(

If I understand your question correctly, you can use RazorEngine, which I used in .NET Framework 4.6.2 Class Library. There is a .Net Core Nuget as well.
I referenced such Class Library projects from Console Applications, Windows Services and Azure Cloud Services (Worker Roles).
In my setup, I had to mark Razor files as Content so they are copied over to the bin folder. Also, it got more complicated if library is referenced by another library and then that library is used; content did not get copied over or it got flattened out (all razor views ended up in the root of the bin folder vs following directory structures), for which there is a fix by editing project file and adding your own copy build tasks. Or I had to embed razor views into DLL.
There might be improvements to this in recent years, as I said I used RazorEngine few years back. And it might be different in .Net Core RCL as well, but I believe it is possible.
Nowadays, I am using 3rd party services like SendGrid or MailJet. For me, using them avoids majority of the above issues and offers better insight into emails/templates/campaigns to Marketing and other non-tech users. They can edit and modify templates to their liking without (m)any code deployments.

You have to run the code through ASP.net core engine so that Razor code get converted into html.
Otherwise use simple html template.

Related

Secure ASP.NET Core 6 application back-end code

Is it possible to generate a DLL only from the *.cs files of the project and then remove them and deliver the DLL + rest of the files including the .cshtml files (views) for the client to have autonomy to edit the front? What kind of changes would I need to make to the project, would I need to keep 1 separate full version to be able to generate this DLL and a second project where I would have the DLL + views .cshtml? Do I need to reference this DLL somewhere? Would it be in the *.csproj file?
I recently decided to study ASP.NET Core 6 and ended up creating a very simple system that has several functions such as login, logout, user registration, customer registration, company registration, product registration, all of this is working perfectly, I used the scaffold of aspnet code generator + Entity Framework to generate Create, Delete, Details, Edit, Index and Migrations pages from Models (code first), I tested it with Sqlite initially and then with SqlServer, the system is OK, I can compile it in debug and release/publish, I can put it in production and send it to the client and it runs there without problems, this would be the default scenario where I would sell the solution without it having access to any source code (I won't go into details like reverse engineering , security, protection, etc..., as it is not the purpose of this question), what I need is for me to be able to edit the project for him so that he can edit only the *.cshtml files by modifying the i information from the front as I see fit and generate a new executable for the project, when he needs changes to the backend I would do it for him and charge for upgrades or not charge depending on the type of change.
It sounds like you are looking to deploy your application with Razor Runtime Compilation Enabled.
If you enable this, the .cshtml files that are deployed would be compiled at runtime, meaning that you could manipulate them on the fly without a visual studio recompile.
This is not enabled by default, so you will need to do a few things. First you will need to install the NuGet package for this
Install-Package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
Then you will need to add a call to AddRazorRuntimeCompilation() in your builder when you add either MVC or Razor Pages to your project.
For Razor Pages it would be similar to the following
builder.Services.AddRazorPages()
.AddRazorRuntimeCompilation();
You cannot do this if you are using Blazor

Is it possible to remove all .Net MVC components in a .Net Web Api application so that I can use AngularJS as the front end?

I'm creating an application with a .Net Web Api project wanting to use pure AngularJS as the client side. Since Web Api is built on top of MVC, it creates MVC specific and default items that I feel is not needed. These items include the HomeController, _ViewStart.cshtml, _layout.cshtml, etc. I tried removing them but it comes up with errors. Has anyone tried to remove the MVC stuff out of the web api project and used separate client side front-end? Is it even possible to remove the MVC items without errors?
Remove RouteConfig.cs from App_Start, remove the Views directory and all sub-directories including the Views internal web.config file. Comment out or delete all the lines in the Global.asax.cs Application_Start method except GlobalConfiguration.Configure(WebApiConfig.Register). Remove the HomeController, add an index.html and any needed Angular scripts and go at it. I also added solution folders to organize my views as reusing the existing Views folders did not work. I'm using VS 2015 but is should work for 2013 also. PWE
Web API is not built on top of MVC.
The default templates bring in MVC for the sake of supporting a help page, but you don't need to use it.
You can start with an empty web project and just check Web API.
The routing piece is server routing and it's part of what maps the URL to Controllers+Actions, it has nothing to do with Angular routing.
As Mike Cheel alluded to, there are no dependencies between MVC and Web API. However, if you use the built-in templates, it's easy to get the impression that the 2 are linked. They include a lot of stuff in these templates because they can't anticipate where you want to go with your project... so they try to cover all the bases.
For your purposes, you would probably be better off to start with an empty project and add only the components that you actually need. For this approach, some of the best tutorials and starter projects are from Taiseer Joudeh's "Bit of Technology" blog. His tutorials helped me to build an "MVC Free" web application from scratch that uses JSON Web Tokens and AngularJS Interceptors for security and Web API 2 and Entity Framework to serve up the data.
He has many tutorials on his website... but you might want to start with "AngularJS Token Authentication using ASP.NET Web API 2, Owin, and Identity". What what.. you didn't ask about security? Well... security is an issue that you will need to confront at some point anyway... and Taiseer presents a nice solution for securing an Angular/Web API application.

Report Wizard in an MVC Application

After some research, I understand that we can use sql server reporting in an mvc application as long as the view engine is web form instead of razor.
The tutorial I have been trying to follow is:
Creating an ASP.net reporting using Visual Studio 2010
The problem is in Part 2. I cannot find the local classes listed as Data Source. The only choice is to set up a new xsd file to connect to the database. How can I have my reports to use my data repositories as their data source?
The credit goes to TNCodeMonkey!
Not only the project has to be compiled first, we must have an index.aspx file in the root folder of the MVC Web Application.
I don't know why. But the Data source is populated with all my dll's once that magic file is in place.

ASP.NET MVC 3 File Structure

I apologize for the vague title - I am new to ASP.NET MVC coming from PHP, and I have teamed up with a .NET developer that has a webforms background.
When working with MVC in PHP, all of my files are visible on the server - that is to say, I can go into any of my model, view and controller files and edit any of the code remotely without a problem.
In .NET MVC, I couldn't find the controller or model files, so I asked the developer how the webpage is being put together, and he told me about how .NET compiles code into .DLL files and that I am unable to access this. I wanted to view a controller to see how it was pulling together my views, but didn't even see a controller folder on the server.
To me, something about this doesn't sound right, but I am not sure because of my lack of experience in .NET. Can someone provide input as to if this is the typical .NET scenario, and if not, what is he doing wrong? Should I take this as a red flag? Keep in mind that .NET MVC is new to both of us.
Thanks!
There is no code files because these code files was compiled into DLL files during Publish process. It's not strange - it's even better because of performance reasons. It's common scenario for all ASP.NET and ASP.NET MVC applications.
You can deploy yor application via XCopy deployment (to have all .CS files etc.): simply copy all files to destination folder. Then your site will be compiled (into App_temp destination) by runtime on first run. Your .CS files will still be in place.
You can read more about Publishing procees here:
Walkthrough: Publishing a Web Site
Publishing Web Sites

Maintaining .NET web application

I am considering using .NET MVC for my next web app but one of the requirements is that there should be minimum work involved from the clients side (who will be maintaining the site).
They are used to simple HTML sites where all they have to do in order to make a minor change is to modify an html in notepad and upload it.
What parts of an .NET web app needs to be compiled? Is it only the .cs parts of it? Can all the rest be updated freely by modifying files with e.g. notepad?
Also, in an MVC environment, is more of the view related code in compiled files?
How is this kind of maintenance usually done in such cases where the client will take over the site on delivery (and are not interested in needing VS installed and needing to compile!)?
If you really need a web application, then in order to make changes to the 'application' part, they're going to need to be able to recompile.
If they're going to make visual changes, then your best bet is to provide a method for them to edit the HTML of the site. You can make changes to the views (.aspx files) in ASP.NET MVC without having to recompile. If you make changes to your controllers or your Model, then you'll have to recompile.
If this is a major requirement for your client, you can build the site using ASP.Net Web Forms instead of ASP.NET MVC in which case changes to the .cs files will be compiled on the fly when the page is first accessed. Note that this only applies to the .cs files in your Web Forms project. Any .cs files in referenced assemblies will need to be pre-compiled.
That said, I suspect your client is primarily interested in modifying the look/feel/content of a page, so they would probably be satisfied modifying the .aspx files in either a Web Forms or MVC app.
If they have the budget for it, sounds like the best solution is to build a Content Management System, so they don't have to edit files ever again.

Resources