Should I place a script partial on EVERY page, or in the Layout.cshtml partial file? - asp.net-core

My organization is currently working on a new ASP.Net Core web app. We have a couple of common Javascript functions that are used on all pages. At first we put the scripts in a partial file and started adding the following to the Scripts section on every page.
#{await Html.RenderPartialAsync("_CompanyScriptsPartial"); }
At this point we have only added it to a small handful of pages, but have around or so left to add it to.
Is it better to add that on every single page, or can I just put the same code in the layout.cshtml (it works when I do it, I just don't know if I should do it)? What is the 'best practice' method for this scenario?

If the scripts are used on all of your pages, or even if they are used on most of the application pages, then it is better to add the script link to the layout.
This way you avoid duplicating code by not adding RenderPartialAsync to every page.
Best practice is not copy and paste code and to have a single location for common code.

Related

Test cafe - Page Object - Consume Modules

I've been working mainly with selenium (java) and espresso as automation tools. I'm pretty new to test cafe and liking it so far.
I came across this specific situation and was wondering what would be the best way to solve it using javascript or test cafe.
I am using Page Object design on my suite. In addition, I would like those pages to be able to consume many modules that can also be consumed from many other pages.
As an example, let's say I have a Home page that has a Header and Footer modules and I also have a listing page that has a Header, a Footer, and a carousel that shows items (This carrousel also appears on other pages).
I was thinking of creating something like a generic page but not sure if it was the best solution. Any thoughts?
For generic items, create Page object class separately. For example in your case create page object for header/ footer, carousel area and place them in the generic package for better classification.
Invoke them where ever required (i.e. invoke header/ footer in homepage as well as in listing page).

Svg-edit usage reference where i can find it?

I found this tool(https://code.google.com/p/svg-edit/) very useful, but there is any reference for this project allow to integrate properly in your applicatione instead of simply add it to an iframe?
For example i want to retrieve the svg code for save it in a variables or something like this
It's possible but might be a little tricky to strip out the required resources and load them in your own application while making sure that there aren't any conflicts. This is actually something I plan on doing for one of my own projects.
Do you need to do this though? You can talk to the iframe from your application pretty easily. For example, to get the SVG content you would use this (assuming you only have 1 iframe on the page) ->
var svgedit = window.frames[0];
svgedit.svgCanvas.svgCanvasToString();

Efficient way to create pages from multiple similar type links

I have a page where there are about 30 links and those links would have similar page except for a few contents changed(there is also pictuures). Now is there an efficient way to do that without repeating the codes and repeated nestings of the codes. thank you.
Using plain HTML you won't be able to do this.
The most straightforward way to do it, I think, is using server-side scripting to implement a rendering template. You could then have a default "main" template with everything those 30 pages have in common and then in each of those pages use the main template and load the custom content.
So if you want to modify something in the main template you'd only have to modify the main.html (or whatever you called it) page and not each of the 30 pages.
See this.

SEO: Can dynamically generated links be crawled?

I have a page containing <div> tags with onclick="" code that calls an ajax request to get json data, and then iterates through the results to form links (<a />) to append to the page. These links do not exist in any other place on my website. How can I make these dynamically generated links crawlable?
My initial thought was to turn the <div> tags into <a> tags with a href="#", but with my limited knowledge of how typical crawlers work, i don't think this would solve my problem since the "#" would be what's recognized by the crawler, and not necessarily the dynamically generated output. This is besides the point that i don't want the scroll positioning to be altered at all, which would also rule out giving the <a> tag an id and having it reference itself.
Do I have any options aside from making a new page containing all of the links i need to be crawled? Thanks.
As a general rule, content that is created or made available through JavaScript cannot be found or indexed by search engines. Google does support crawlable Ajax but using it as the only means of accessing your content is bad for accessibility. Also, other search engines can't get to that content which is also not a good thing. Basically crawable ajax is a bad thing.
You should always make your content available without requiring JavaScript to get it. Then you can improve your site by adding JavaScript to make getting the content faster or easier. This is called Progressive Enhancement and is how good websites are built.

Need to access the Page object in Global.asax in the PreRequestHandlerExecute

I have a huge website (containing around 5000+) pages. There is a theme functionality in the website where user can choose different colors for their profile. Now i want to use the ASP.net theme feature and put different CSS (for different colors) in the theme folder and in Global.asax i want check the user theme and render appropriate link element with the css. But my problem is, i am not able to access the Page element for adding the link in the page.
Here is my code
Dim page As System.Web.UI.Page = TryCast(System.Web.HttpContext.Current.Handler,System.Web.UI.Page)
page.StyleSheetTheme = "Black"
But when i run this code I get a Null reference error.
P.s : My application is very huge so its not possible to have a master page or a base class and inherit it in every page.
Please suggest.
The page is not available in PreRequestExecute. This function is called before asp.net steps in to handle things, and asp.net is responsible for the page. Think of PreRequestExecute as being earlier in the scheme of things, like when IIS is first trying to figure out what to do with this thing it has, the thing is not even a page yet.
You might want to look into some of the other events that you can hook, there are events that would take place after the page has loaded that may allow you to do what you are suggesting.
Rather than going into global.asax for this, consider using master pages. One possibility is to have nested master pages, where the first master page sets up overall layout, and the nested master handles the theme. (Or one of several nested master pages, all referencing the same top-level master page). If necessary, you can use the PreInit event in the page to change master pages, and select the master that matches your theme selection.
You can centralize this function by having your own class that inherits System.Web.UI.Page, and have all your own pages inherit this new class. Handle the PreInit event there. (As well as other useful functions, like page-level handling of unhandled exceptions, general security issues, etc.
EDITED TO ADD: As #aepheus correctly notes, the page hasn't been instantiated at the PreRequestHandlerExecute event. So there's no page class you can access.