asked on
Inserting Javascript on VIEWS globally with exclusions
I have an MVC layout page.
I am looking to do the following and am not quite sure how to do this. I am hoping someone has had to do this before.
So I have a 3rd party I am integrating to our website that will have a javascript file that will need to go on many of our views but not all. I would like to setup something at the main layout page that will check a config file or database table for the views it should exclude then Render some script on the views not exluded that is the same on every page.
For now the limit to my question is MVC but my website has a Master page from ASPX to that I wlll need to add to my solution.
It also has an angular spa. Currently for the Angular spa I am trying to determine if we would just care about the spa or that actual flows within it. I am leaning to the flows.
Eitherway If I can get something working on MVC as a POC that would help alot.
I was thinking you might have something on the layout like so
@If(Global.Current.Page(ViewName)
@ScriptRender("~/location")
Obviously that needs work but just my general idea.
ASKER
Thus the reason I indicated maybe a config or db call that only sais to test what is an exclusion.
Also would bottom of page be ok...in this case this javascript that is loaded is a script that adds content to the header of any page its loaded on.
ASKER
I think due to the massive website this is I would need controller name in the route..
for example you could have many index pages.
I also have to do this same implementation on aspx pages in the site...so the master page.
and angular.
We have some of all in the site.
Ultimately, you'll just need to debug the Context.Request.RouteValues collection to see what/how the controller name is provided, and then update your check to include it. For the sake of discussion let's assume that RouteValues includes an entry for "controller".
My suggested updates (again, you'll need to debug this and adjust it based on the version of .NET MVC you're using) are below.
var routeIsNotExcluded = true; // default value
var controller = (string)this.Context.Request.RouteValues["controller"];
var action = (string)this.Context.Request.RouteValues["page"];
var config = Configuration.GetSection("ExcludedRoutes")?.GetChildren();
if (config != null)
{
// we assume that the Controller entry does not include a leading slash.
// we know from testing that the Page entry includes a leading slash.
routeIsNotExcluded = config.All(x => x.Key != $"/{controller}{action}");
}
ASKER
Any idea how to do that.
1. Transport the configuration over to the spa as a JavaScript property. Basically, write an extension method that returns the configuration entry (ExcludedRoutes) as an enumerable, then set that on the page which loads the spa. Pull that in as a global variable (terrible, I know), and then use that for your checks.
2. Create a webservice call that does the same check and returns a boolean. Use that in the spa. This one is typically the easiest and cheapest in terms of application "weight".
ASKER
But How do we do this with a MasterPage. So again similar but very different that the MVC. I will use the same appsettings.config to store all this for all page types.
However I want to be able to control the masterpage from config and so far I am only seeing how to do this progromattically on every content page. Any ideas?
Thanks again for the great help!
I hope what I just said is clear.
When I get into a situation like what you're attempting to address, I create a centralized component and add that component to my pages. One way or the other, I think you're going to have to touch each of your associated Angular pages at least once.
ASKER
Sorry I couldn't be more help; good luck!
The key for your scenario is to use the IsSectionDefined feature (in your master page) to provide the 3rd party content. This gives you the default global inclusion.
Then -- on a given page where you don't want the content to be included -- add the @section call and put something there that will override the master page content. I can't remember if you can simply leave it empty or not. Regardless, that will exclude the 3rd party content from the view in question.
In the examples below I'm calling the section specific_3rd_party_script so that it's easier to see and understand what I'm doing with it.
Master Page -- add the following where you want it to render in your HTML (probably at the bottom of the page):
Open in new window
View Page where you want to exclude the specific_3rd_party_script content -- add the following:
Open in new window