ASP.net MVC bundles https - asp.net-mvc

asp.net mvc bundles do not render for https.
here is my code:
bundles.Add(new StyleBundle("~/Content/css/css").Include("~/Content/css/custom.css"));
View
#Styles.Render("~/Content/css/css")
And the error code I get is:
Mixed Content: The page at 'https://domain/' was loaded over HTTPS, but requested an insecure stylesheet 'http://domain:443/Content/css/custom.css'. This request has been blocked; the content must be served over HTTPS.

Do you have ~/Content/css/css as a real path in your app?
IIS is probably trying to handle the request.
Rename your bundle or the folder in your app and it should work.
So try this:
bundles.Add(new StyleBundle("~/Content/core.css").Include("~/Content/css/custom.css"));
Then in your view:
#Styles.Render("~/Content/core.css")
The style bundle name can be anything you want, it doesn't have to be related to the real location of your CSS file.
Update
If you are getting mixed content errors, that's because some of your content is coming over HTTPS and other content is coming over HTTP.
The best way to overcome this problem is use // on all your content.
So say for example you have an image like this
<img src="http://website.com/images/smiley.gif">
change it too
<img src="//website.com/images/smiley.gif">
That should stop your mixed content warnings.

Use a different bundle folder
Like, Update
bundles.Add(new StyleBundle("~/Content/css/css").Include("~/Content/css/custom.css"));
To
bundles.Add(new StyleBundle("~/CssBundles/css/css").Include("~/Content/css/custom.css"));
This issue happens if you use an existing project folder for the bundle location.

Related

How replace '~' symbol in MVC with physical path

In Mvc application if the Layout is added with
#Styles.Render("~/Content/themes/base/css")
#Scripts.Render("~/bundles/jquery") .
Need a solution to replace the
~
symbol .So that Output should not be like
/Content/themes/base/jquery.ui.core.css in render page .
Instead
localhost:xxxxx/Content/themes/base/jquery.ui.core.css .
This Localhost need to be added when mvc application is run under IISExpress wherein It get modified to hosted application path when hosted into the server
As far as I know, the path used in the .Render() calls is just an identifier or key that will produce the paths for the files that were added to it earlier.
This is what you'd normally find in BundleConfig.cs:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
The ~/bundles/ part is never sent to the output when .Render() is called. What is sent are the actual (translated) paths from the .Include() call.
You should always make those paths start with "~/" so they will be output correctly. I have never found the need to include the server name, because the browser will by default request the file from the same server that delivered the web page.

Loading an MVC JS bundle externally

I have a small JS library in my MVC 5 project that I want to be available for external users to load into their apps. At the moment I'm bundling it like so:
bundles.Add(new ScriptBundle("~/clientApi")
.IncludeDirectory("~/Api/clientapps/", "*.js"));
I can then access the bundled library via browser at the path /clientApi.
However, it's always minified, even though I've set my web.config debug=true, and other bundles in my own app are included as non-minified.
How can I make the file/s in the bundle available as a non-minified bundle file?
If you access /clientApi directly then yes it will be the bundled/minified version.
The debug=true option effects your script reference in your own .cshtml file. When debug=true, references to the individual script files are rendered to the client (so the client doesn't use /clientApi at all).
When debug=false, then a reference to /clientApi (with the version query string) is rendered to the client instead, so they get the bundled/minified version... If you give that link to these external users, then that is what is going to get rendered.
That path doesn't care if it is debug or not. It's not like /clientApi is going to bundle but not minify the files depending on your compilation settings... it's just either your app is going to render the bundled/minified path or the individual script paths.
If you want to do debugging/testing in external apps, then they will just have to use the individual script paths.
Even if you do give these external apps the /clientApi reference once testing is done and they are ready to use the bundled/minified version, it doesn't explain how you are going to handle versioning. If you update a script, how will they know to stop caching?
Actually you can serve the bundle unminified if you disable the transforms of the Bundles
protected void Application_Start() {
BundleTable.EnableOptimizations = true; // Force bundling to occur
// If the compilation node in web.config indicates debugging mode is enabled
// then clear all transforms. I.e. disable Js and CSS minification.
if (HttpContext.Current.IsDebuggingEnabled) {
BundleTable.Bundles.ToList().ForEach(b => b.Transforms.Clear());
}
}

ASP.NET MVC 4 Bundling and Minification: wrong paths in Debug Mode with Url Rewrite

I want to use the ASP.NET MVC 4 Bundling and Minification feature, please assume that we run the debug mode so nothing is bundled, but each reference is simply rendered to single tags. Locally everything works fine:
Example
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
}
When calling #Scripts.Render("~/bundles/jquery") from the view now, a tag similar to this is generated:
<script src="/Scripts/jquery-1.8.2.js"></script>
The result is the same if I would just write:
<script src="#Url.Content("~/Scripts/jquery-1.8.2.js")"></script>
But when I deploy everything to a test server in the internet (still using the debug mode), there is some Url rewriting enabled, where I route specific domains or subdomains to specific folders.
For example, if the domain is sub1.example.com then forward to example.com/__sub1__ and in case of sub2.example.com forward to example.com/__sub2__ and so on. When I open sub1.example.com in the browser, there is usually no clue that this forward happens, the Url doesn't change, all Urls remain working and so do references generated using the Url.Content(...) method.
But for some strange reason, the call to #Scripts.Render("~/bundles/jquery") now renders something like this:
<script src="/__sub1__/Scripts/jquery-1.8.2.js"></script>
Please note the "/sub1" part which is the part which shouldn't be generated and never appear somewhere in the rendered html code and leads to a 404 because the url rewrite forward fails.
While...
<script src="#Url.Content("~/Scripts/jquery-1.8.2.js")"></script>
...still renders the correct relative Path to "/Scripts/jquery-1.8.2.js". And i never had other problems with rewriting the Urls like this. And I don't want to get rid of this rewriting.
Obviously, the same applies to style sheets.
Any ideas what I could try?
This is a bug with the current implementation of the script/style helpers, this should be fixed with the 1.1-alpha1 release that should be available shortly(early Oct) as the support for using the VirtualPathProvider registered with ASP.NET has fixed this general class of issue (resolving resource paths correctly).

MVC Bundling with HTTPS IE7

I have successfully implemented MVC bundling for my MVC application. There is one problem with the run time which runs under HTTPS.
I am sure there is a problem because when I switch the debug field to false the user gets the warning message "This page contains secure and nonsecure items. Do you wish to proceed?
I know that I can turn this prompt off using the security setting in IE. I would like to know if there is something I can do to the application so that bundled scripts and styles come through the secure pipe.
If you use the Scripts.Render helper to include the bundle it will use the same HTTP scheme as the one used to request the main page. So if the main request was done over HTTPS then this helper will generate a <script> element using HTTPS. You could use the Net tab of FireBug to see which resources are served through HTTP and HTTPS and be able to isolate the problem.
Thank you for this suggestion. I figured out that the problem was coming from modernizr-1.7.js
The strange thing was that this problem only occurs when modernizr is bundled. I removed modernizr because we don't really need it.

asp.net mvc using HREF in application running on IIS

There is a partial view representing pager control (very similar to this) for blog content. Code generates HTML with references and href like, "/Blog/Posts/Page/1", "/Blog/Posts/Page/2" etc.
It worked absolutely fine on Cassini, but after I switched to IIS problems appeared.
IIS application running in virtual folder, so URL is
http://localhost/tracky
and blog area located,
http://localhost/tracky/blog
As I press on pager button, I recieve 404, because the URL would be
http://localhost/blog/page/3
Instead of
http://localhost/tracky/blog/page/3
My question is, how to handle such situation? how to change code generation to provide correct URL? how to make it work same - as root applicaton or application in virtual folder?
Source code is here
You need to generate your urls either by using ActionLink in your view, or using an UrlHelper in your href as follows: <a href="<%=Url.Content("~/blog/page/3")%>" ..>bla</a>. This will generate Urls that are adjusted accoring to your application root.
You should be using the following:
UrlHelper.GenerateContentUrl("~/Blog/Posts/Page/1");
The ~ allows the url to be resolved relative to the application path and should produce correct results in both cassini and IIS.

Resources