Using directives in new files - asp.net

Is it possible to have additional "using" directives automatically added to my new aspx.cs files so that I do not have to keep typing the same ones over and over again (i.e. custom namespace using directives)

You can edit the files that are used by the template. Better yet, create your own. File + Export Template.

You don't have to type them in. When writing the code, type in the name of the class (without the namespace). Then hit CTRL+.. That will open up the resolve type intellisense helper. It will add the using statement to the top of the file. No scrolling necessary.

Related

How to use subfolders in the Pages and Shared folders

I've got a Blazor Server-Side application. It has the folders Pages and Shared out-of-the-box. Since I have a lot of components in these folders, I wanted to distribute the components into multiple subfolders for a clearer structure. But if I do this, the components are not found and are not displayed in the GUI.
What am I missing? Do I have to register the routes to the new subfolders somewhere?
You can have whatever folder structure you wish. But you will need to update your _Imports.razor with the new namespaces.
For example if you have a structure of
MyProject/Components/Forms/MyInput.razor
You would need the following in your _Imports.razor:
#using MyProject.Components.Forms;
The other option is to reference components using their fully qualified namespace:
<MyProject.Components.Forms.MyInput/>
I am using partial classes for my code and not embedding code in the razor page, so I was a bit confused by the advice from Chris above and kept getting compile errors on my overrides in my partial classes after moving them into a new sub-directory.
Here is how I got it to work:
I had a page called EmployeeList.razor and a partial class called EmployeeList.razor.cs located in a project named Web.Client in the Pages folder. That means the following:
The namespace of the EmployeeList.razor page by default is Web.Client.Pages
My partial class is using the same namespace
If I move EmployeeList.razor and EmployeeList.razor.cs files to a sub-folder under Pages called EmployeePages, the EmployeeList.razor page will now have a new namespace of Web.Client.Pages.EmployeePages. That means that I need to also change my partial class' namespace to match or it will not be able to override methods.
The only other issue I ran into was not paying attention to the the folder name I used. I initially used a sub-folder named Employee (as in Pages\Employee\EmployeeList.razor) and this created a namespace for the razor page of Web.Client.Pages.Employee and I started having issues with an Employee.cs class I was using in my partial class since it thinks I'm trying to use a namespace rather than a class name. Since the path of the page is determined by the #Page directive on the razor page, the folder names really only determine the namespace of the razor page so I changed the sub-folder name to EmployeePages.
I did not find that I needed to update the _Imports.razor page with the new namespace, but I'll keep it in mind in case things stop compiling properly.

Drupal 8 add javascript and pass data in a hook with custom module

I am building a custom module with Drupal 8. One of the requirements of the module is that it sets up a javascript file based on the configuration settings, which the user of the module sets up in module configuration.
The javascript which needs to be added to the page depends on the configuration settings. Hence I can not add it using Libraries as mentioned in this article: Adding assets to Drupal module.
I first implemented it using a block. I use Twig templates to pass the configuration variables in PHP to the twig file, and in the twig file, I have a tag to add the javascript based on the config variables. Refer Using twig templates.
The problem with this approach is that user needs to add the block on the page, and there is no UI facing element on that block. I also find it very messy.
Is there a cleaner way to add my javascript using hook and pass variables to it? I looked around and found hook_install and hook_page_attachments. So I can add Javascript, but don't know how I can pass any php variables to it.
I am new to Drupal development. Any help with this is really appreciated.
TL;DR I need to find a way to add Javascript using Drupal hook and pass some PHP variables to it.
Use in hook_page_attachments:
$attachments['#attached']['drupalSettings']['myvar'] = $myVariable;
Then in your js (I assume you already know how to attach js library):
(function ($, Drupal, drupalSettings) {
Drupal.behaviors.my_custom_behavior = {
attach: function (context, settings) {
var strings = drupalSettings.myvar;
...
}
}
})(jQuery, Drupal, drupalSettings);
See nice Acquia tutorial for more examples.

How to include a css stylesheet in Orchard module?

I am working on a module for Orchard, and I just want to know how to include a css file.
I have done the following with no result:
Added a folder "Styles" to the root of my module and included a stylesheet and a Web.config file like in this question.
And I have seen this but that's not what I am looking for.
EDIT:
Ok, solution:
When I started working on Orchard I created a new module by just creating a new project in Orchard.Web/Modules folder, but as I read here, my Views/Web.config file has to include some orchard base things what mine didn't because I did not create the module using codegen. I fixed it now by replacing my Web.config file for a Web.config file from another Orchard module, now it works.
Next time I will use codegen to create a new module.
Thanks to endorphin for helping me!
If you want to include your stylesheet in a view, you need to specify this at the top of your view. Where the name of your stylesheet is the filename of the .css file in the Styles folder.
So in your .cshtml file for your view..
#{
Style.Include("your-stylesheet.css").AtHead();
}
I have used AtHead() in the past, but there are other methods to include your stylesheet in different locations (such as AtFoot())
If your stylesheet depends on other stylesheets you can do something a little more interesting by creating a Resource Manifest which is detailed here https://stackoverflow.com/a/6500121/580101

Ruby on Rails - Using helpers inside css erb file gives undefined method

I have a css.erb file which contains the styling of one of my pages. I have a helper which preformats a URL of a given image based on the location of that image.
When I call the helper function from the view, the output is expected (a string containing the URL for of the image). However, calling it in the css.erb file gives me an undefined method error even though I copy and paste the same function into my css file.
It's as if the helper is not included in the css file and is ignored.
Helpers are not available by default to templated .css files. They are intended to help in view construction only. However, you can try the workaround mentioned here using an initializer:
Rails.application.assets.context_class.instance_eval do
include YourHelperModule
end
Alternatively, if you only need this for one or a few files, you can use the solution mentioned here, by adding this code to the top of the .css.erb file:
<% environment.context_class.instance_eval { include YourHelperModule } %>

Getting a directory's path, that is inside an assembly

I have a custom asp.net control. That control will also render to the page a piece of javascript. This javascript piece of code is actually properties that have to be initialized. One of this property value must be a path. This path is a directory that is inside this assembly. If it is was a file i would use GetWebResourceUrl but now I don't know what commands to use.
Also I would like to know, if I get the dir path the files inside it will be available for the javascript to use them or not?
Try this: System.Reflection.Assembly.GetExecutingAssembly().Location

Resources