Secure ASP.NET Core 6 application back-end code - asp.net-mvc

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

Related

Using Razor Templates Stored in Razor Class Library 3.0 (RCL) Without AspNetCore Web Application

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.

following iterative and agile in asp.net MVC

ok, i know there are a lot of posts online that specify how to do iterations with MVC.
my question is slightly different. when i used to do iterations using WebForms, i was creating one thing only and finishing that one thing till the end which was including the deployment on production.
for example, i was creating a webpage and deploying it, then i create the second page and deploy it. so .dll files were added to my bin folder while the previous dlls remain untouched. at the other hand, when i was making a change latter on, there was this one file that needed to be replaced on production.
now here is the question, how can i acheive the same thing in mvc? beause it just doesn't deploy each page into an individual dll. each time that i add something i have to redeploy the application dll which is not really wise! i played around with deployment options in visual studio but no luck!
There is nothing preventing you from putting controllers and other code in separate assemblies and dropping them in an existing application. Like any ASP.NET based application an MVC application will automatically restart if you add or modify any file in the bin folder or modify web.config.
If you're using Razor you can use RazorGenerator to generate code for your views and compile them into the same assembly.
You may need to write some additional logic though to get routes, model binders etc. wired up correctly.
For a more structures approach to compose the application of separate modules, you may want to look into portable areas. This is an extension to ASP.NET MVC that allows you to package the entire module (including views, css, js etc.) into a single assembly.
First thing, you have to work on the title of the post, it does not match the content of the post.
In asp.net mvc u can choose to deploy only what changed. I.e. If you only changed the .cshtml file, then you can just replace it with the file in production. However if you change any controller class (C#/Vb code), then you will have to upload the web project dll file too so that this new changes are available in the production env

How to seperate MVC project to several smaller MVC projects

I have an MVC 3 app that has some core functionality (most important is autorisation) but mainly serves as a portal to different areas or modules. I want to organize thit to different modules that with minor changes also can be deployed as their own website.
The project consists of a Forum, Blog engine, Messaging between users + 4-5 upcoming modules.
I looked at ScottGu's blog about MVC 2 and found something that seemed perfect:
Depending og what the customer need I want to only give them the exact modules they can use. It is also easier from a maintainence view to be able to work and update referencd assemblies in each project and just do a full update for the customers that have that spesific module on their server.
But in MVC 3 there is no apparent way to use Areas this way, do you know how?
Status
I will try to add MVCContrib Portable areas to my existing solution and convert my areas ower and will post back the results. If it works I will mark it as the accepted solution.
MVCContrib has portable areas.
http://mvccontrib.codeplex.com/wikipage?title=Creating%20a%20Portable%20Area&referringTitle=Documentation
This is possible in MVC3:
From:
http://bob.archer.net/content/aspnet-mvc3-areas-separate-projects
Right click on the shell project and "Add Area...". Type in the area name. This will create an Areas folder with your area in it. (This is not 100% needed but you do need the "Areas" folder and you can steal the XXXXAreaRegistration class for your application.)
Create a new MVC3 empty project in your solution to match your area. Move the XXXXAreaRegistration.cs file from the shell mvc project to the new project and adjust the namespace as applicable. (Or you can manually create an area registration class, it's a pretty simple class. Just use the Add area template generated one as an example.)
Edit the routes in the AreaRegistration folder as needed.
Delete the folder under the areas folder that the template wizard added.
Modify the web.config of the new project and take out the connection strings and the authentication, membership, profile, rolemanger sections. You will not need to deploy this web.config but the razor intellisense doesn't work without it during dev time.
Delete the global.asax file from the area's project or you will get extra default routes.
Create a virtual directory in the "Areas" folder of the shell project with the name of your area as the alias and point it to your "area" project. You will need to use IIS or IIS Express for this. I use IIS. For IIS Express you can use the appcmd.exe in the IIS Express folder or you can edit the applciationhost.config file.

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