What does WebActivator do? - asp.net-mvc

This code was generated for me after added entity framework code-first for SQL Server CE using NuGet. They did no changes to any other file. The file SQLCEEntityFramework.cs was created and placed in App_Start folder.
Does this mean it automatically gets executed or something? The same thing happened when I added Ninject for MVC 3. No code was added to the global.ascx file so I have no idea if its plug and play or I have to configure something.
[assembly: WebActivator.PreApplicationStartMethod(typeof(StackTorrents.WebUI.App_Start.SQLCEEntityFramework), "Start")]

According to:
http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx
This new attribute allows you to have
code run way early in the ASP.NET
pipeline as an application starts up.
I mean way early, even before
Application_Start. This happens to
also be before code in your App_code
folder (assuming you have any code in
there) has been compiled. To use this
attribute, create a class library and
add this attribute as an assembly
level attribute. A common place to add
this would be in the AssemblyInfo.cs
class within the Properties folder.

To clarify, it gives you a way of hooking into several application start and application shutdown events WITHOUT having to change any existing code files (previously you had to edit Globals.asax.cs).
This is mostly a big deal when making packages as these events are really useful for bootstrapping Http modules and it is really difficult to write code into existing files.

Related

Namespaces not recognized in ASP.NET 5 MVC6 app

I'm trying out ASP.NET 5 with MVC 6 but have some issues.
Using Visual Studio 2015 Comm
I create a model under namespace MyApp.wwwroot.Models.
Then a service under namespace MyApp.wwwroot.Service. Here I reference my model. It just shows in black, not in blue. It also has no quick actions or squigly line. When I build it says "Build Succeeded". I have the same problem when I add a reference to Microsoft.AspNet.Mvc in my Controller. When I create a new controller and inherit from Controller. I have the same issue..
Is this VS or what could be the issue?
It looks like you're trying to place code under the wwwroot folder. The wwwroot folder in ASP.Net Core (formerly ASP.Net 5) is used for hosting static content and these files are not parsed or compiled as C# code, but rather as static content that you want served (ex. html/css/javascript files). Moving these files out of the wwwroot folder should fix this issue if this is the case.
When working in your FishtankApp.Services namespace, you need to expose your model namespace. this is done by adding the following using declaration at the top of the source code document you wish to use it on:
using FishTankApp.Models ;
The same holds true for anything you wish to use that falls outside the scope of the current namespace you're working in.

How to generate entities to custom folder with jhipster:entity?

I would like to generate my models/entities into a custom folder/package and not to domain. Is this possible with the entity generator command?
it wasn't as easy for me. After refactoring the entity class, I had to change all the classes and .js files generated by jhipster. For example, #RequestMapping in the rest controller has to be changed by adding the new path to the entity (#RequestMapping("/myEntity") becomes #RequestMapping("newpackage/myEntity")).
No.
Of course, refactoring your domain classes is just a matter of dragging/dropping those files to another directory, if you have a decent IDE, so that shouldn't be an issue.

Accessing a LocalDB within an ASP.NET website from another project in solution

I have an asp.net mvc 5 website using EF, LocalDB and Code First Migrations. Requirements have now dictated that I need a need to add a console application into the mix to do some scheduled work. However this console app must call into the database functionality exposed in the web application. Also of note is that we are using LocalDB for development, but will switch to a 'proper' remote DB for production.
As such I have created a new console application within the project and added a reference to the web application so that I can call its repository functions. I know this probably isn't the best way to handle things.
For whatever reason though, when calling Save Changes on the database context from within the console application, nothing is saved to the LocalDB database. The Save function returns a number indicating that a number of rows were inserted.
I get the feeling I am making a schoolboy error somewhere. What could it be?
i`v used this in the past:
<add name="DefaultConnection"
connectionString="Server=(localdb)\v11.0;Database=WebPortalDb" providerName="System.Data.SqlClient"/>
If you want to use the functionality/database from one Project to another project then use the following::
1) Include the 'ConsoleProjectName.dll' file into your 'MVC' project's reference
2) Use that Dll into the namespace of your file.
(eg. using System.Data.Entity)
in the same way you have to use the DLL into your namespace.
3) make an object of the 'console' application's class and use the methods & other properties defined in that class.
May be this much information will be helpful for you.
This we are doing for n-tired architecture, where there are different layers (i.e. projects) in a solution to be linked with each other.

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

Add my own class file in ASP.NET MVC, and it couldn't be referenced?

I am currently stuck trying to add my own class file into ASP.NET MVC project, so it could
be referenced by my controller. But Visual studio 2010 always complain about "The type or name 'Products' could not be found (are your missing a using directive or assembly reference?)
I am not sure where to put my class file and tried add the class to Models or Controllers directory, and it wouldn't work either. My the class is under the right namespace, and I did reference the Models namespace in my Controller. It could be something obvious, but I couldn't get it working:(
Make sure that the "Build Action" property of the file is set to "Compile" in Visual Studio. Perhaps when you initially added the file, you added it to a location or added it as a file type that did not automatically set the build action properly.
The general rule of thumb is that business objects go it the Models folder you're correct. If it isn't working you don't have the project configured correctly. In the controller where you wanted to reference the Products object, did you add a reference to Models.Products?
Are there any other errors in listed? Sometimes multiple errors in a solution can combine to create something like this.
Ensure the namespace of your class is correct.
Based on a comment to a different answer, it appears you're trying to add a utility or helper class. I suggest you put it in a Helpers folder under the root of the project.
So first add the Helpers folder under the root of the project. You should have the following structure when done (there may be other folders as well):
[project]
- Content
- Helpers
- Controllers
- Views
Now, add a new class to the Helpers folder. Call this class ProductsHelper (in the Add New Item dialog make sure to put ProductsHelper.cs, as it's asking for the file name).
Now have a look in the new ProductsHelper.cs file. Copy the namespace found in said file, to be used in the controller.
Now, add a using directive at the top of your controller that looks like the following:
using [copied namespace];
Sometimes projects are set up to use default namespaces that don't match the project name, in which case the namespace in your newly-added class file might be different than what you thought you should add as the using directive. If you're wondering what the default namespace for your project is, you can see it in the project's properties, on the Application tab, in the "Default namespace" textbox (you can change it here as well).
I had the same problem adding some utility class .cs files to my new MVC project.
I was working mostly on VS 2010 Website projects which compile differently from
Web Application Projects ( WAP ) like the MVC 3 Project.
Just to clarify Jacob's answer, In the VS Solution Explorer: right click on the class .cs file and select the Property Dialog and then set the property: Build Action to Compile.
This will force the code to be compiled when you Build the project.
HTH, LA Guy

Resources