When does ASP.NET MVC recompile views after you modify them? - asp.net-mvc

I know, that views are compiled on the fly, but when exactly does it happen? First compilation occurs at first request, if I'm not mistaken. But what about when I modify aspx/cshtml file, what happens then?
Is it possible to somehow control this behavior without using aspnet_compiler.exe?
The problem I'm having is that I modify my controller/viewmodel and load them as a separate assembly. When I refresh page I get this error:
The model item passed into the dictionary is of type 'Test.Controllers.AController+IndexViewModel', but this dictionary requires a model item of type 'Test.Controllers.AController+IndexViewModel'.
The difference between these models is the assembly name (not shown in this error), so I need to recompile views against my updated viewmodel.

It happens on the next request after the modification.
If your ASPX Views have codebehind then there's 2 step compilation happening. First the codebehind class is compiled as part of the project, next the ASP.NET runtime creates another class that inherits from the precompiled class. If you change the controller to pass a different model class but don't recompile your project it won't work because the precompiled class still references the class from the old assembly.
So, if you use codebehind, the project compiles a class that inherits from ViewPage<TModel>, and if you want to change TModel you have to recompile the project. If you don't use codebehind this is not an issue, because TModel is determined at runtime.

Related

How do I utilise Assembly from within WebAPI Controller

I have a VS2017 WebAPI solution that I am working on at the moment. I need to make a call to an assembly (included in the solution) from within the Controller. Much to my frustration it appears that I cannot make a call to methods within the assembly.
The graphic below I believe should supply sufficient detail for the right advice. Likely I am trying to do something with the WebAPI project that it will not allow (but I hope not) :-)
Update
On reflection the graphic doesn't appear to be all that legible. Within the PIInterfaceController I have added the following code:
Imports System.Net.Http
Imports System.Web.Http
Imports SPEN.PIInterface
Namespace Controllers
Public Class PIInterfaceController
Inherits ApiController
Dim piServerSetting As String
'Instantiate the SPEN PI Interface object so we can supply necessary data elements as required to extract the correct data from PI
Dim piIntfc As New SPEN.PIInterface(piServerSetting)
piIntfc.
End Class
End Namespace
My problem is that I am unable to access any of the methods from the PIInterface instance 'piIntfc'.

What its the first, the annotated class (egg) or used class (chicken)?

certainly I have not read something fundamental, and it seems very strange, but I wonder.
Suppose you use
#SharedPref
public interface SharedPreferencesInterface {
#DefaultBoolean(true)
boolean showDeviceName();
I have the IDE (idea) configured with Gradle, and I generated the SharedPreferencesInterface_ class that I can use in another class as
#Pref
SharedPreferencesInterface_ prefs;
But suppose someone now download the project, how can the use? Because the class where used SharedPreferencesInterface_ not compile because the class does not exist, and the class does not exist because compilation errors ...
How it's made? Surely there is a way ... configured to compile certain classes first?
Help is appreciated.
A greeting.
But suppose someone now download the project, how can the use? Because
the class where used SharedPreferencesInterface_ not compile because
the class does not exist, and the class does not exist because
compilation errors ...
This is the same situation when you compile a project in a full build (when no classes are generated yet). Actually Gradle always does a full build currently in Android projects. No configuration is needed at all in addition to the standard AndroidAnnotaions config.
Actually this works because the compiler does not fully compiles your class before passing it to annotations processing. It is clear it should not to, because the class may reference generated classes, which are only available after the processing. So first the compiler creates a model of the classes, only parses the structure of the them (fields, methods, return types, parameter types, etc), but not the implementations. Also it allows missing types even on fields. If it finds a missing type, it assigns to TypeKind.ERROR, but the name of the type is still available for the annotation processor. After the processor is done, it generates the missing class, so the kind of the class is no longer TypeKind.ERROR, and the compilation can succeed.

ZF2 override framework classes via autoloader classmap

Is it possible to override the class file location of a framework class via classmap and autoloader? If yes, then how?
Example: I want to override Zend\Form\Fieldset, so that everywhere in the framework where Zend\Form\Fieldset is referenced, I want it to use my own class file instead of the original.
Motivation: When updating the framework, I want to keep my modifications safe from getting overwritten.
Known alternative: Modify the code in the framework.
Disadvantage: Modification gets lost when updating the framework.
writing the same class (FQCN) at another location is generally a bad idea. This causes two classes which are equally named to live in two separate locations. It's a much better idea to create your own Fielset in your own namespace. Say, Application\Form\Fieldset.
You can extend the ZF2 fieldset by your own. Then reference this new fieldset class and its all much more maintainable.
The downside of this method is you don't automatically use the new fieldset class. You have to reference the Application\Form namespace in every form you use. On the other hand, this makes it much more clear to other users of you code what exactly happens: there are no unexpected consequences using ZF2 code.
The only remark I have to make here is, for what do you need another fieldset? If you think you need that for view helpers, that's not true. You can modify the view helper to render fieldsets without modifying the Fieldset form class itself.

Error generating <DbContext>

I am trying stuff out on MVC4 Beta. Project is in VS11, EF 5.0-Beta, using Code First approach.
Created simple Model poco and DbContext derived class with single IDbSet<> property for model class. Connection string with LocalDb.
I noticed when context's Entity Set property is type of IDbSet<> then I get message from VS when I try to create new controller with “Controller with read/write actions and views, using Entity Framework” Scaffolding template:
"There was an error generating 'MyProject.MyNamespace.MyContext'. Try rebuilding your project."
Even Clean Solution and building again doesn’t help.
If I changed Entity Set property to type DbSet<> then controller scaffolding succeeds.
Is it a bug or is it expectable?
Br,
Lauri
It is not a bug you cant instantiate an instance of an Interface. The I in front of IDbSet denotes the type as an interface (By convention). So you were trying to create an instance of that interface. Instead you need to delcare it with DBSet<> which is an implementation of the IDBSet<> interface. I hope this clarifies your issue.

Unable to open designer on form in C# 2010

I have generated a solution in C# 2010 by way of the Artinsoft conversion from VB6.
When I open the new solution in C# I right-click on the forms but there is no "View Designer" option. I assume this is because something is failing to compile. What could be the problem and how should I fix it?
Try a clean and rebuild
Make sure you have proper designer.cs file
Make sure the initialize method is called from the constructor of your Forms.
Make sure if there are any custom dlls they are in right places.
Better create a new app in Windows forms and check how the things are arranged. so that you can compare the conversion.
I would not expect that from a failure to compile, but rather a failure to convert properly. Even if the class doesn't compile, if it's a form you shouldbe able to see and edit the form portion.
The reason should be marked in the error list if it's failing to compile. It's more likely that it's created it as just a class (that doesn't inherit from Form).
Check that there's a MyForm.Designer.cs (where your form name is MyForm) and also check that the declaration of the class in MyForm.cs inherits from form
public partial class MyForm : Form
If this isn't the case, it hasn't converted properly, so you may have to inherit from form, drop on the UI controls again and hook them up to any events
Have you tried running Visual Studio as Administrator (elevated rights)?
Can you try Shift+F7 key combination?

Resources