MVC Bundling and Minification - Control the token/version? - asp.net

I know that when using the ASP.NET MVC 4 it creates a "bundled" collection of the requested files. I also understand that it adds on a version to the end of the collection URL.
I am wondering if it is possible to change or control the generated token?
<script src="/Scripts/test?v=8HZAB6C8ZnrIPYNFzMQKt0AR4AUsUYBjxPPkbGSRIZo1" type="text/javascript"></script>
to become...
<script src="/Scripts/test?v=1.2" type="text/javascript"></script>
Thanks in advance.

It's not possible from what i have been looking at.

Related

Can I use the binding library of Aurelia, independently, in vanilla JavaScript +ASP.Net Core?

I write vanilla JavaScript objects in my Views in ASP.Net Core (MVC + API), to handle all client-side functionality.
My goal is to build my applications as a PWA (progressive web app).
So I could later add the full Aurelia framework.
For my objects, I would like to use, for the moment,
only the binding library of Aurelia, independently. I know it is quite powerful.
Is it possible and how can I do it?
thanks.
You can build your application as a progressive web app and only use the binding library, but in reality it's just easier to use a few of the key libraries together for a better developer experience. Just because you can do something doesn't always mean you should.
Take a look here in the docs for a more thorough example, but in a nutshell it looks like this -
<!doctype html>
<html>
<head>
<title>My App</title>
</head>
<body>
<my-component message="Enhance Me"></my-component>
<script src="jspm_packages/system.js"></script>
<script src="config.js"></script>
<script>
SystemJS.import('aurelia-bootstrapper').then(bootstrapper => {
bootstrapper.bootstrap(function(aurelia){
aurelia.use
.defaultBindingLanguage()
.defaultResources()
.developmentLogging()
.globalResources('resources/my-component');
aurelia.start().then(() => aurelia.enhance());
});
});
</script>
</body>
</html>

Load jquery Ui Scripts in asp.net applications

is there any component or control exist for asp.net application that load jquery ui scripts and themes?
thanks.
Depend on this sample Can Any body help me and introducing component or control ? :)
hmm - couldn't you just have them included in your masterpage and then they'd be on every page referenced (by that master) on the site??
this is how i'd do it if you wanted to keep it simple. if you're looking for a control to do this, then you could of course create a usercontrol and place that in the head section of your masterpage to effectively do the same thing.
that said, maybe i've missed something here :)
There aren't that I'm aware of...though it wouldn't be too hard to fashion. Do you need one though? Using the Google cdn for example, all you need is this in your master page:
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
As of a few weeks ago (Sep 17, 2010) Microsoft also hosts jQuery UI:
<script src="http://ajax.microsoft.com/ajax/jquery.ui/1.8.5/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.microsoft.com/ajax/jquery.ui/1.8.5/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
this sample source maybe useful :)
its created for jquery datepicker.
http://hasheminezhad.com/sites/default/files/hasheminezhad.com/JQControls-Source.zip

Getting absolute URL to page in code

I am new to ASP.NET and am trying to convert a web application from using hard-coded deployment locations (ie, /base/path/index.aspx) to discovering them at runtime. If I use Response.Redirect(), I can express the path as '~/index.aspx' and, at runtime, ASP.NET will build the correct URL to send the redirect to based on where the web application is deployed.
There are places in the code where Javascript and/or HTML is generated dynamically and sent to the client as part of the response to force a new window. In these cases, I don't know how to get the actual URL that should be opened in the new window. Using ~ doesn't work in this case since the URL is being evaluated by the browser and not the server. Is there a class or method in ASP.NET that will give me the URL I am looking for? I'd look myself, but I don't even know how to properly phrase my question.
The VirtualPathUtility class is what you are looking for.
Specifically you can use these methods:
VirtualPathUtility.ToAbsolute("~/");
VirtualPathUtility.ToAppRelative("~/");
You could do something like this:
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var url = '<%= ResolveUrl("~/path/some_page.aspx") %>';
window.open(url, 'name');
</script>
</head>
<body>
<form id="form1" runat="server"></form>
</body>
</html>
Tilde (~) is basically a shortcut to HttpRuntime.AppDomainAppVirtualPath.
You can use Request.ApplicationPath and build your way to the page.

ASP.Net Master Page and File path issues

I'm trying to add a script reference to jQuery in my master page so that it will work for any page. It currently looks like this
<script type="text/javascript" src="jquery.js"></script>
The problem is that the path is always relative to the executing aspx page so this will only work if the "jquery.js" file is located in the same folder. To make it work I have to change the line to:
<script type="text/javascript" src="../../jquery.js"></script>
This is obviously less than ideal because it will only work for pages that are two levels deep from the root folder. If I try the following, IIS throws an error about an unexpected character.
<script runat="server" type="text/javascript" src="~/jquery.js"></script>
Any ideas?
EDIT: I forgot to mention as well that the script MUST be in the head tag
The current top answer throws a "ASP.NET Ajax client-side framework failed to load." error when I add it to my master page. Its thrown from javascript and not the .Net compiler. If I move the ScriptManager to the head section where it should be I get a compile error about the ScriptManager needing to be inside a form tag.
The third answer throws a "Illegal characters in path." exception from the compiler
EDIT 2: When I add that line to my head tag I get this error from IIS.
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)
SOLVED: I took the edited response from the answer below and put it inside an asp:ContentPlaceHolder element
You could use a ScriptManager:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/jquery.js" />
</Scripts>
</asp:ScriptManager>
EDIT: If you absolutely need this in your <head> section, you could do something like:
<head>
<script type="text/javascript"
src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script>
</head>
EDIT 2: According to the comments, if you are observing that
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)
you may need to change the above to use the data-binding syntax:
<head>
<script type="text/javascript"
src="<%# Page.ResolveClientUrl("~/jquery.js") %>"></script>
</head>
Try <%# instead of <%= in Master page under head section
<script type="text/javascript"
src="<%# ResolveUrl("~/YourScriptFolder/YourJQueryOrJavascript.js") %>">
</script>
Then in Code Behind of Master page under Page_Load Event
Page.Header.DataBind();
Now you are good to go with either jQuery and JavaScript as well as CSS just you need to change your path in ResolveUrl which file you want to handle CSS, JavaScript, jQuery.
If you're not going to us asp:ScriptManager or absolute paths then you can do it like this:
<script runat="server" type="text/javascript"
src='<%= Page.ResolveUrl("~/jquery.js") %>'></script>
I do not know whether you guys found the solution to your problem or not. I was facing the same problem and going nuts to figure out why do I get "jQuery is undefined" error on the plugins i use. I tried all the solutions i get from the internet but no luck at all.
But, suddenly something splash on my mind that may be the script files should be in order. So, I moved the jquery referece to first position and everything start working like charm.
Remember guys, if you're using any plugins with jquery, make sure you use the folloing order of setting reference to those fiels.
reference to the jquery library
reference to the other subsequent plug-in libraries and so on...
e.g.:
"script src="js/jquery-1.3.2.min.js" type="text/javascript"...
"script src="js/jqDnR.min.js" type="text/javascript"...
"script src="js/jquery.jqpopup.min.js" type="text/javascript"...
"script src="js/jquery.bgiframe.min.js" type="text/javascript"...
Always make sure you must put the jquery reference to first and then the subsequent libraries.
Hope, this solves your problem especially when you use with MasterPages. Its very strange that it works no matter what order you use when you don't use MasterPages but when you do, then it somehow requres the proper order.
Good luck and happy coding,
Vincent D'Souza
Look at How to Run a Root “/”. This should fix all your issues regarding unresolved .js file paths. You basically reconfigure the VS Dev server to run your application as localhost:port/ as opposed to the regular localhost:port/application name/ making name resolution work the same way as it does on IIS.
For absolute path of the file for any page use it following:
<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script>
<script type="text/javascript" src="/full/path/to/jquery.js"></script>
If this script tag goes directly to the browser, then you unlikely can substitute your site's root there. At least not on the server. So you can:
Deploy site to the root of domain
name and use absolute paths
(simplest solution).
Insert this
link with server control.
Preprocess resulting HTML before
sending it to the client (with
HttpResponse.Filter).
You can also use <base> HTML tag:
<base href="http://www.domain.com"></base>
and then all the links in header section are relative to base address:
<script type="text/javascript" src="scripts/jquery.js"></script>
It's often useful when you have multiple publishing destinations, like local dev web server, demo server, etc. You just replace that base URL.
<body>
<script language="javascript" src='<%= this.ResolveClientUrl("~/full/path/to/jquery.js") %>' type="text/javascript"></script>
</body>

How do I use ModalPopup from the ASP.NET Script-Only ajax control toolkit?

I am using the ASP.NET Client-Side ajax control toolkit in my asp.net MVC application. I see that there is a .js file named "AjaxControlToolkit.ModalPopup.ModalPopupBehavior.js" in the AjaxControlToolkit folder. However, I cannot find any examples on how to use it.
[Edit] -
As mentioned, I am using the Client-side, Script-only control toolkit. For those unfamiliar, here is the description from CodePlex:
AjaxControlToolkit-ScriptFilesOnly.zip
contains script files, CSS style
sheets and pictures used by the
toolkit. Use this download if you
don't want to use embedded resources
and prefer a file-based model.
I have not been able to find many resources on how to use some of the scripts included with this.
CodePlex link: http://www.codeplex.com/AjaxControlToolkit/Release/ProjectReleases.aspx
Yestarday I had the same need to use the popup extender and here are the js files which you should include first.
<script src="javascripts/MicrosoftAjax.js" type="text/javascript" ></script>
<script src="javascripts/AjaxControlToolkit/AjaxControlToolkit.Compat.Timer.Timer.js" type="text/javascript"></script>
<script src="javascripts/AjaxControlToolkit/AjaxControlToolkit.Common.Common.js" type="text/javascript"></script>
<script src="javascripts/AjaxControlToolkit/AjaxControlToolkit.ExtenderBase.BaseScripts.js" type="text/javascript"></script>
<script src="javascripts/AjaxControlToolkit/AjaxControlToolkit.Animation.Animations.js" type="text/javascript"></script>
<script src="javascripts/AjaxControlToolkit/AjaxControlToolkit.DropShadow.DropShadowBehavior.js" type="text/javascript"></script>
<script src="javascripts/AjaxControlToolkit/AjaxControlToolkit.DynamicPopulate.DynamicPopulateBehavior.js" type="text/javascript"></script>
<script src="javascripts/AjaxControlToolkit/AjaxControlToolkit.PopupExtender.PopupBehavior.js" type="text/javascript"></script>
The next step is to create the PopupBehavior from javascript like that:
$create(AjaxControlToolkit.PopupControlBehavior, {"PopupControlID":"div_to_popup","Position":3}, null, null, $get("textbox_input_id"));
If you need to use another control from the AjaxControlToolkit with scripts only without the burden of asp, here is one easy way to find the necessary include js files.
Go to its demo page here and look at the source of the page. At the bottom you will see how to create the control. To find the include files needed for this control select CombineScriptsHandler.ashx and search for "//START". Every line with "//START" shows a script which is included in the page.
Check out the AJAX Toolkit samples. There are live demos, walkthroughs and documentation.

Resources