How Do I Compile ASP.NET Views After Deployment - c#

I have a complex site running Sitecore composed of both ASP.NET Webforms (.aspx, .ascx) views and Razor (.cshtml) views, and would like to get them all compiled after deployment so there's no runtime compilation performance hit the first request for anything.
I've tried running aspnet_compiler against the site, but
It's telling me it can't find an assembly which is 100% in the bin folder
Failure to compile one view shouldn't stop it from compiling others
I'm now looking into how views are compiled at runtime on demand, and have found System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath) so would I be able to scan my website folder for all views and call that method for each one?
I'm not looking to precompile views, but just compile them after deployment as if there had been requests for pages requiring them. How can this be done?

I ended up using System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath), and it works really well. As that method takes virtual paths, I found the full paths of views I want compiled, then subtracted Server.MapPath to get the virtual path.

Related

Could not load the assembly. Make sure that it is compiled before accessing the page

I looked into all the articles and solutions (most of them coming from this site or the ASP.NET forums archives, and could not find a solution to my situation.
Basically, I have a project for which the master pages depend on another (compiled, .dll) project of Master pages. I installed it fine on my machine, and it runs fine, but when we did the same thing on a colleague's machine, it always gives us that 500 Server Error:
Could not load the assembly 'NAME_OF_ASSEMBLY'*. Make sure that it is
compiled before accessing the page.
An interesting point: it does not say "Could not load the assembly" with a name followed by and underscore and random letters, it just mentions the whole, proper name of the assembly.
Also weird point: The master pages (in the dependant project) all use some .dll files of compiled code. They have master pages that have no codebehind files, but do have the reference for the .dll included in that project. We tried copying all of those same files over, but to no avail.
We're simply at a point where we no longer know where to look or what to do. We reinstalled countless times, copied over my files, re-registered IIS, still nothing. Any pointers would be greatly appreciated.
Thank you all for your time!

ASP.NET Website to Web Application conversion issue - Could Not Load Type 'ClassName' AND Set Namespaces for all existing webpages technique

I have converted one huge Website Project into a Web Application Project as we want to have Compiled code in form of DLL for security purposes and followed below MSDN articles:
http://blogs.msdn.com/b/webdev/archive/2009/10/29/converting-a-web-site-project-to-a-web-application-project.aspx
http://msdn.microsoft.com/en-us/library/aa983476%28v=vs.100%29.aspx
The website is having 1000s of web pages/files. I have resolved all the issues of duplicate references, class name colisions and some compilation errors faced at on Build Solution stage. Due to the earlier website pattern, class names are in the following pattern:
for e.g. if UserMaster is in admin/Operations/Masters folder so the class name will be like: admin_Operations_Masters_UserMaster.
Now, Web Application is Building Successfully and when I try to run the application, it shows "Could Not Load Type 'ClassName'" so I found out that the issue of because Namespaces are absent due to earlier Website Pattern.
Found out in the MSDN:
By default, pages and classes that are built by using the Visual Studio Web site project model do not automatically include a code namespace. However, pages, controls, and classes that are built by using the Visual Studio Web application project model automatically include a code namespace. When converting the Web site project to a Web application project, you will have to add the namespaces to the code.
I have DEFAULT NAMESPACE in the project properties.
So I tried to add the namespaces to whole of the project via ReSharper -> Refactor -> Adjust Namespaces but its for .cs files only and not for .aspx.cs etc AND so its not working.
I thought I need to make a utility to add the namespaces automatically in pages as per their folder path structure but its a time consuming process.
So need a workaround to add the namespaces automatically in pages as per their folder path structure.
Secondly,
What I did for testing is, I have wrapped the default namespace in login page of the project where I faced 'could not load type issue', added in Inherits attribute also. Then did a Clean Solution then Rebuild Solution. But still getting the same issue.
I don't why DLL is not getting updated with the login page's class.
The strange thing is when I change the Codebehind to CodeFile (attribute of Page Directive) then the page is working very fine. Its due to source file is present at the time of running the project BUT i want to use Codebehind attribute as it will tell the compiler to look for the class in DLL not in Source file.
Finally, Summary is :
I have two major issues:
Could not load type 'className'
How to add the namespaces automatically in pages as per their folder path structure.
I have already searched here and came around different topics but nothing works for me. one have suggested to use "Surround With" feature of VS but I need to go one by one on each file which is NOT possible right now.
So Tried:
Set path of DLL to "bin" instead of "bin\debug"
Checked the Build Configuration and its of x86. also the DLL is present in bin
Checked all project properties and all are fine. Cleaned and rebuild solution hundreds of times. Cleaned Temporary ASP.NET files also.
Set Build Action to "Compile" for .cs class files
Cannot set Build action to Compile for aspx pages as they are static html and they should be set to "Content" which is default Build Action.
Resharper solution for adding namespaces but not working
Please help....
Any help will be greatly appreciated. Thanks.
Solved the Issue:
Earlier, I have tried to Set Build Action to "Compile" for .cs class files Only and for all the files sitewide to Compile from .csproj file but it was not working due to aspx page itself set to Compile which was not required.
Now, I have changed the Buid Action to Compile which was Content for .aspx.cs and .aspx.designer.cs files.
and it solved the issue.
Like for e.g.:
Solution 'TestWebApp1'
Project 'TestWebApp1' (ASP.NET Web Application)
Properties
References
App_Data
Scripts
Default.aspx (Build Action: Content)
Default.aspx.cs (Build Action: Compile)
SiteLayout.Master (Build Action: Content)
SiteLayout.Master.cs (Build Action: Compile)
Web.config
Source: None of my "code behind" code is being called

Debugging shared embedded (?) views in MVC3/4

I did some research about this but had no luck finding any solution.
This is a two-aspect problem:
Sharing views between multiple projects. This is easily done by i.e. embedding them in a common project and providing by virtual path provider.
The problem is that they can't be stepped into by a VS debugger, since it has no way of correlating physical cshtml files with these JITted.
Debugging common views. This is the part where I stuck in. I have no idea how to do this.
What I have done:
I created a project with common views (with a "content" type, not emvedded) and a symbolic link in every project, which leads to the appropriate folder. I also added this path to a RazorViewEngine in application_start.
This works fine, I even get a line number when there's an exception thrown in a view, except that VS debugger still can't step into this view :/
In order to debug views one have to have them in a project, but it can't really be done - we obviously use TFS, so they would be multiplied in tfs.
To make the story short: is there any way to step into a shared view while debugging in VS2010?
This is quite a major problem for us, any ideas are appreciated

Dynamic ASP.NET application compilation

My web application (ASP.NET v3.5 / C#) dynamically creates other asp.net applications (creates aspx, aspx.cs, classes, web.config, project.csproj file etc.).
The thing is even after I configure the 'new' application folder as APPLICATION in the IIS, When I try to browse it, I get the following parser error: Could not load type 'Template48.Template48' (where Template48 is my application).
When I manually browse to the new application folder, and re-build it, all works perfect.
How can I re-compile this WHOLE application, but in it's folder ?
Thanks in advance,
Gal.
A web application needs to be compiled before it works; this is different from a web site.
Just providing the sources isn't enough; in fact, ASP.NET doesn't need the sources (aspx.cs) to run the application, just the .aspx, web.config, and .dll files ("assemblies" in .NET lingo). It is even considered good practice to remove the sources from the production system to avoid accidentally leaking the sources and giving attackers information about the site's inner workings.
To make this work, you need to somehow invoke the command-line C# compiler to build the project on the fly; this will generate the needed .dll files.
A different, but much harder, route would be to use .NETs ability to emit IL at runtime; using this facility, you could produce assemblies directly without going through the compiler. This is pretty difficult though, and you need thorough understanding of the IL to pull this off.

Spark compiled views locating

I have a problem with Spark.
I have compiled assembly with views, located in bin subfolder of website, that i created like below
engine.BatchCompilation(targetFolder, Global.AllKnownDescriptors());
On start of my app, a try to load compiled views:
svf.Engine.LoadBatchCompilation(Assembly.LoadFrom(Path.Combine(basePath, "SharedViews.dll")));
When debugging, i can see that this was successfull.
But ViewEngine doesn't find that views. It even doesn'n look for them in CompiledViewHolder where they are located.
May that problem be caused ny wrong IViewFolder?
Or i should do something more to use compiled views?
Thanks
Not sure if this is helpful, but in the documentation there's the following paragraph:
There is no dynamic recompilation of classes that are loaded in this way and changes to the spark files will have no effect. However the files must be available on disk at runtime. Sorry about that - but the different frameworks still needs to test for the view files' existence to know what view descriptor it should instantiate.
Do you have the view files in your directory structure?
I found out the solution - I compiled views not on app startup, but using different tool. So changes made to engine (registration of entries etc.) during compilation were vain.
So now I compile view on app startup and everything ok.

Categories