I'm following this link, compile razor files into separate dll, and can get it to work except I can't figure out how to access JavaScript, CSS, and Images embedded in the dll.
Has anyone used this approach? It seems very promising. Thanks.
The technique presented in this article is for embedding razor views into separate assemblies. It doesn't allow you to do so with static resources such as images, js and CSS. For those type of resources you will have to implement a custom solution. For example you could write a controller which, given a resource name, could read it from the assembly (GetManifestResourceStream) and write the stream to the response. Then you could simply generate links to this controller action in your views (Url.Action) and pass the resource name.
You can use my EmbeddedResourceVirtualPathProvider which can be installed via Nuget to load all kinds of resources from referenced assemblies, and also can be set to take dependencies on the source files during development so you can update views without needing a recompile.
Related
I want to reuse some types, views and scripts from another NET Core Web Application. I have tried to add a reference to the project, and everything seems fine. The types from the other projects get syntax highlighting and everything compiles. But at runtime, when the types are accessed I get exception: FileNotFoundException: Could not load file or assembly {referenced assembly name}.
EDIT: I checked in my projects bin folder and the DLL for the referenced project is there in \bin\Debug\netcoreapp2.0{referenced dll}
What could be wrong here?
Maybe you could use a shared project. And put all classes there.
I am not sure if content like javascript file can be shared using this type of projects but I think it could be possible.
More references can find here:
https://www.c-sharpcorner.com/UploadFile/7ca517/shared-project-an-impressive-features-of-visual-studio-201/
http://rion.io/2017/03/22/sharing-is-caring-using-shared-projects-in-asp-net/
In ASP.NET Webform, App_Code is the standard folder for putting code and using it at run-time. But I think this folder is kind of different in ASP.NET MVC, my question is:
where should I put my code ( Extension methods, Helpers, ... ) in ASP.NET MVC. When I store code in App_Code folder, I can't use them in controller but they work fine in views.
About Entity Framework, the same question, where should I put edmx and tt files? I'm not using Code-First
Update:
After some search, finally I created a new Class Library project in my solution, code is available in all controllers and views. I still don't know why the code in App_Code is not available in the controller
I had the same issue, my class Utility.cs was not recognized inside my MVC project. I changed the Build Action "Content" to "Compile" and that solved my problem.
Hope that helps.
App_Code is necessary in Web Site projects because it has a special meaning. It means "Don't serve these files to a web browser". In ASP.NET MVC, files are not directly served to the browser in most cases, so App_Code is not necessary. You can place code files whereever you want, in any folder you want because these files are compiled into a DLL and not typically published to the website itself.
Using a stand-alone library is also a fine solution.
No one really explains why App_Code exists in the first place. It's the place where you put code files to get dynamic compilation (at runtime). That's the number one reason for App_Code and why it is special. You can put code in any folder you want but you will have to mark it with Compile action to compile while in App_Code that is not necessary, in fact, most files in App_Code aren't even a part of a project.
It does work in ASP.NET MVC projects too you just have to name your code files with .cshtml extension.
I'd recommend starting with this tutorial. It uses EF code first, but you can simply replace the code first DbContext with a normal edmx ObjectContext if you wish (hint: Models folder).
App_Code is specific to Web Site Projects. IMO, its a terrible way to organize all but the simplest of web sites. An MVC project is a Web Application Project, so there is no App_Code and your classes can be defined pretty much anywhere.
Well, try to download some already existing opensource projects. One of them is rather complex, but very cool if you'll get understanding of it - orchard
The best practice is moving this logic to DAL (data access layer) or BLL(business logics layers). Depends on complexity of your application.
For those who don’t want a second project/class library and just want a simple folder for a few c# classes, I propose “App_Classes”.
I have a question about organizing .resx files in mvc3 application. So far I have managed to do the following:
Creating new class library in a solution for resources and adding resources for two languages. Everything works fine when I have for example:
Resources.resx //default language
Resources.en.resx //english language
But I want to have such organization in my class library:
DefaultLanguage foder
- Resources.resx
en folder
- Resources.en.resx
I want to organize resources by folders. I think it is more appropriate for people who will translate the site and it is also more maintainable. When I organize resources in such a way the web page returns an error...cannot find error. Am I missing something? I know that you should not have folder name in namespace because that would mean that referencing resource in View is dependent on folder name and this is not correct.
The point is I would like to have the same behaviour of resources wheter they are organized in folders or not...
By the way: I check on the web and could not find any appropriate solution.
Thank you for your answers!
MVC3 uses ASP.NET resource management. See ASP.NET documentation for details. If you want to rely on ASP.NET behaviour and generated strongly typed classes, then you must keep resource file from default language and other language specific resource files together.
To have the same tree structure that you suggested, then you have to implement your own resource management. You probably don't want to do that, because (at least some) resource editing tools rely on that behaviour.
On our projects we have one resource (and his translations) for each view. They are in folder Localization within MVC3 project. Inside we keep the same tree structure as is within Views folder. This is more or less the same as ASP.NET local resources are organized. Common strings are kept in one .resx file on root of Localization folder. This ensures maintainability.
To keep your people that will do translation happy you have to pick good .resx editing tool.
So, I'm making a CMS at the mo, and using the modular extensions HMVC CI 2.0. It's lovely. I'm wondering the correct method for keeping my assets (js, css, img) related to a module within the module directory.
The problem being, how do I link to these assets? Let's say I'm using a template engine and passing the js files to load for a specific page:
$js[] = 'assets/js/my_js.js';
I suppose I'm asking this all wrong, but is there an easy way to link using the current module's directory?
Thanks in advance.
There's nothing currently built in to CI or HMVC for this.
I prefer not to reference files that are allowed direct access, like images/js/css, from within the application directory. Mainly because:
I don't want anyone to know what the guts of my app look like. By referencing files from directly within a module, you expose your application's directory structure.
I would never do this if I weren't using HMVC
You must now allow direct access to (certain) directories within the application via .htaccess. For security reasons, I prefer to simply disallow the entire thing.
I understand the desire to be as modular as possible, but to me it's not worth this hassle. I prefer to keep a separate directory in a public folder called "modules" (duplicating my application/modules structure), that has nothing but "assets" (css, js, images...).
I'd offer some code but I have no clue how you are adding js/css to your views - it's probably much different than the way I do it. It would be easy enough to write a function to detect the current module, controller, or method and change the asset folder automatically, but this may interfere with other shared assets. I'd suggest writing an entirely separate function for loading assets from modules.
Examples given are from Kohana 3, but the question is not limited to the framework.
I've always put my assets in a file structure like this
application
- classes
- views
assets
- css
- js
- swf
- images
-- layout
-- content
system
- classes
I've started reading a few forums where people always mention their assets files are placed in the views folder. This makes a lot of sense to me as these files are tied to the views quite closely (being included by markup). Plus, it will unclutter the docroot.
What is the preferred location of a site's assets? If I do place them under an views/assets folder, where should the actual template files go... under a separate folder like views/templates ?
For ASP.NET MVC
The project template gives you the folders ~\Content and ~\Scripts with the latter containing JQuery.
Why not follow this model, adding additional folders below these if they would otherwise get too many items.
I would stick to the project template unless there is a very good reason to override it (and also override the logic to find views and controllers).
I tend to place mine in the web root (wherever that's defined by the framework) so that I can reference /assets/img/myimage.jpg (or an analogous CSS/JS file, etc.). As Richard suggests, though, I do this because I'm sticking to the framework convention. I'm not familiar with Kohana, but the frameworks I've used extensively all place assets in the web root by convention.