Adding new Bundle to Bundle Config - c#

In my _Layout page I have these scripts:
#Scripts.Render("~/Scripts/Scripts.js")
#Scripts.Render("~/Scripts/moment-with-locales.js")
#Scripts.Render("~/Scripts/moment.js")
#Scripts.Render("~/Scripts/bootstrap-datetimepicker.js")
#Scripts.Render("~/Scripts/DayPilot/daypilot-all.min.js")
So instead of writing all of those out, I want to create a bundle:
bundles.Add(new ScriptBundle("~/bundles/otherScripts").Include(
"~/Scripts/Scripts.js",
"~/Scripts/moment-with-locales.js",
"~/Scripts/moment.js",
"~/Scripts/bootstrap-datetimepicker.js",
"~/Scripts/DayPilot/daypilot-all.min.js",
"~/Scripts/DayPilot/daypilot-common.src.js"));
So now in my _Layout page I have this:
#Scripts.Render("~/bundles/otherScripts")
Now in my bootstrap-datetimepicker.js I have edited 1 line.. and the change shows visually when I test this app locally.. but when I publish.. that line that I changed does not work..
The only way it works is if I exclude the bootstrap-datetimepicker.jsfile from the bundle and reference it in the _Layout page specifically.
How do I just combine all of those scripts into the bundle and it work properly when published?

Related

#Scripts.Render() <script> tag links to a Html source code page, and scripts are not detected

Here is where i include the ScriptBundle:
bundles.Add(new ScriptBundle("~/scripts").Include(
"~/Scripts/jquery.js", "~/Scripts/popper.js",
"~/Scripts/bootstrap.js", "~/Scripts/Index.js"));
When i debbug the application the script tags are rendered separately, and it works well. The problem is when i publish the project, the scripts are rendered in a single tag; i know that AspNet compress all the scripts in a single tag, but the web page does not detect the scripts and when i click on the link of the rendered tag, it directs me to a source code page (HTML) in which the scripts should be.
This is the tag:
<script src="/scripts?v=r8NL2JoYrRk4WG4L8aUu1037yymdncGQgFxOAcbEyw41"></script>
And this is the link: script_tag_link
Here is how the page of the link looks:
Here should be the compressed scripts
The ScriptBundle name, in this case "~/scripts", shouldn't match an existing folder in your application. Try changing it to "~/bundles".

Using Gentella bootstrap theme in ASP.NET MVC

I'm trying to use the Gentelella theme with MVC but I can't get it to work right, and I feel like I'm defining the bundles incorrectly. I copied the HTML code exactly as it was in the sample and I get this:
Bad layout
There shouldn't be any margins - I want it to appear how it does on the GitHub page. I define the bundle like so (admin being what I renamed the build files as):
bundles.Add(new ScriptBundle("~/bundles/admin").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/admin.js"));
bundles.Add(new StyleBundle("~/Content/admin").Include(
"~/Content/bootstrap.css",
"~/Content/admin.css",
"~/Content/font-awesome.css"));

Rendering JavaScript file in _Layout.cshtml

I have a problem with rendering JavaScript file in _Layout.cshtml.
#section Scripts {
<script src="#Url.Content("~/Scripts/Custom/productsSuggests.js")"></script>
}
When I paste it to Index.cshtml (Home) it works, but only on this page. I need this script to work globally. I have partial view SearchBox in HomeViews catalog, and Controller Action in HomeController.
Because you are in the _Layout.cshtml view, it is likely the top level view. A section is a placeholder in a parent view.
Instead of your current code, try
#Scripts.Render("~/Scripts/Custom/productsSuggests.js")
In the Layout.cshtml you can use: #Scripts.Render("YOUR BUNDLES")
When will be add layout to another page this bundle will be global work.

MVC - How to use another updated _Layout and _ViewStart files for specific folder?

As you can see below my MVC site contain _ViewStart.cshtml file in the root of my Views folder that use the _Layout.cshtml from Shared folder for sharing the layout of Header and Footer. But the Main Content is different for each page.
But now I have same Main Content layout for my Category folder pages. So my question is, how can I use another updated _Layout and _ViewStart files for Category folder pages that also use the Header and Footer by my _Layout.cshtml and _ViewStart.cshtml files or I need to specify the same Section for each page in Category folder? I want to use something like _ViewStart.cshtml file in the Root of Category folder that can share Main Content layout for Category pages.
Perhaps it seems a bit confusing, but I hope you can understand what I'm trying to ask.
You make a _CategoryLayout.cshtml within the category folder and put the following in the category page(s):
#{
Layout = "~/Views/Category/_CategoryLayout.cshtml";
}
Then you can use this layout to add footers and headers to your page, in turn this layout page (_CategoryLayout) can have its own layout. So you can add the following code in that layout:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
This way it makes use of both layouts. You can ofcourse get more out of it by applying the right Sections, don't forget to fill a section in the page itself you need to define the section in every layer of layout till you display it. So if you for example have a CSS section which you want to use in the category page, you also would need to define it in the categorylayout.

How do I add scripts to the bundle in a CSHTML?

I have a form, that form has it's own script, I want to add a script to the bundle if that form is included somewhere. I want to do this from the CSHTML of this form.
Basically I want this:
<form>
... code here
</form>
#includemyscript formscript.js
That is not how bundles work. You can create a new bundle containing all scripts plus your form script and render that one on this specific page, or just use the default bundle and additionally reference your script by a <script></script> tag.

Categories