Styles.Render in MVC4 - c#

In a .NET MVC4 project how does #Styles.Render works?
I mean, in #Styles.Render("~/Content/css") which file is it calling?
I dont have a file or a folder called "css" inside my Content folder.

It's calling the files included in that particular bundle which is declared inside the BundleConfig class in the App_Start folder.
In that particular case The call to #Styles.Render("~/Content/css") is calling "~/Content/site.css".
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

Watch out for case sensitivity. If you have a file
/Content/bootstrap.css
and you redirect in your Bundle.config to
.Include("~/Content/Bootstrap.css")
it will not load the css.

A bit late to the party. But it seems like no one has mentioned
bundling & minification of StyleBundle, so..
#Styles.Render("~/Content/css")
calls in Application_Start():
BundleConfig.RegisterBundles(BundleTable.Bundles);
which in turn calls
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/Site.css"));
}
RegisterBundles() effectively combines & minifies bootstrap.css & Site.css
into a single file,
<link href="/Content/css?v=omEnf6XKhDfHpwdllcEwzSIFQajQQLOQweh_aX9VVWY1" rel="stylesheet">
But..
<system.web>
<compilation debug="false" targetFramework="4.6.1" />
</system.web>
only when debug is set to false in Web.config.
Otherwise bootstrap.css & Site.css will be served individually.
Not bundled, nor minified:
<link href="/Content/bootstrap.css" rel="stylesheet">
<link href="/Content/Site.css" rel="stylesheet">

src="#url.content("~/Folderpath/*.css")" should render styles

As defined in App_start.BundleConfig, it's just calling
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
Nothing happens even if you remove that section.

Polo I wouldn't use Bundles in MVC for multiple reasons. It doesn't work in your case because you have to set up a custom BundleConfig class in your Apps_Start folder. This makes no sense when you can simple add a style in the head of your html like so:
<link rel="stylesheet" href="~/Content/bootstrap.css" />
<link rel="stylesheet" href="~/Content/bootstrap.theme.css" />
You can also add these to a Layout.cshtml or partial class that's called from all your views and dropped into each page. If your styles change, you can easily change the name and path without having to recompile.
Adding hard-coded links to CSS in a class breaks with the whole purpose of separation of the UI and design from the application model, as well. You also don't want hard coded style sheet paths managed in c# because you can no longer build "skins" or separate style models for say different devices, themes, etc. like so:
<link rel="stylesheet" href="~/UI/Skins/skin1/base.css" />
<link rel="stylesheet" href="~/UI/Skins/skin2/base.css" />
Using this system and Razor you can now switch out the Skin Path from a database or user setting and change the whole design of your website by just changing the path dynamically.
The whole purpose of CSS 15 years ago was to develop both user-controlled and application-controlled style sheet "skins" for sites so you could switch out the UI look and feel separate from the application and repurpose the content independent of the data structure.....for example a printable version, mobile, audio version, raw xml, etc.
By moving back now to this "old-fashioned", hard-coded path system using C# classes, rigid styles like Bootstrap, and merging the themes of sites with application code, we have gone backwards again to how websites were built in 1998.

I did all things necessary to add bundling to an MVC 3 web (I'm new to the existing solution). Styles.Render didn't work for me. I finally discovered I was simply missing a colon. In a master page: <%: Styles.Render("~/Content/Css") %> I'm still confused about why (on the same page) <% Html.RenderPartial("LogOnUserControl"); %> works without the colon.

Set this to False on your web.config
<compilation debug="false" targetFramework="4.6.1" />

Related

What is the explanation for a Blazor reference starting with a tilde? "~/(...)"

What is the explanation for a Blazor reference starting with a tilde? "~/(...)"
This can be found in _Hosts.cshtml file:
<link href="~/css/Chart.css" rel="stylesheet" />
<link href="~/css/site.css" rel="stylesheet" />
What is the exact meaning of "~"? What is the difference to just "/" or without?
Is it important to (not) use "~" if you refer to an external library?
I could not find an explanation for this.
In Razor .cshtml files, tilde-slash (~/) points to the web root.
The default web root is wwwroot.
A path beginning with ~/ is referred to as a virtual path.
The Tilde Slash is a feature of Razor, which converts a relative path to an absolute. Thus, the path for the href in <link href="~/css/Chart.css" rel="stylesheet" /> will be converted to an absolute path like this:
<link href="/css/Chart.css" rel="stylesheet" />
Note that the _Hosts.cshtml file contains the the base element like this:
<base href="~/" />
The "~/" is a relative path, and it is converted to <base href="/" />
The base URL is used to resolve relative URLs when the NavigationManager needs to obtain an absolute URL.
Is it important to (not) use "~" if you refer to an external library?
If by external you mean external to the Blazor App space, I guess you shouldn't use the tilde-slash feature at all. You should provide an absolute url, as for instance:
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
Note: As far as I know, the only place the tilde-slash is used in the context of Blazor is with the base element in the _Host.cshtml file
Hope this helps...
What is the difference to just "/" or without?
Imagine your app lived in a folder, and you d onto know the name or it differs between installs.
/dev/
/prod/
YOu can not use / and you do not want to know the name at compile time.
ASP.NET MVC (NOT (!) Blazor, that happens earlier) puts in the root origin of the application folder as seen in the request and determiend at runtime.

Porting HTML 5 page to a webBrowser object in winforms

I'm trying to port THIS pasge and its HTML5 animation to a winform.
What I did is copied and added the index.html and all the css and js file to my project. This is how my project directory looks like
And then I'm trying to load the html file into my webbrowser object using this code snippet
private void Form1_Load(object sender, EventArgs e)
{
string curDir = Directory.GetCurrentDirectory();
this.webBrowser2.Url = new Uri(String.Format("file:///{0}/index.html", curDir));
}
And I have set properties of all the file to Copy Always
Now when I run the project I get a script error message and upon pressing Yes
it loads the page but only background is displayed and the animation is lost!
please help!
PS: I've changed
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script src="js/ThreeWebGL.js"></script>
<script src="js/ThreeExtras.js"></script>
To
<link href="main.css" rel="stylesheet" type="text/css" />
<script src="ThreeWebGL.js"></script>
<script src="ThreeExtras.js"></script>
in index.html file
Screenshots of two script error that I'm getting--
It works in IE10
The *.js files need to be in a js folder. Either create the folder and move them or edit the html file.
EDIT1:
Does it work if you point it to "http://www.script-tutorials.com/demos/177/index.html"?
EDIT2:
Add:
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
in the html head section.
EDIT3:
I tried on my machine and it's the same behavior. But the page does run fine in Internet Explorer 11. Maybe the WinForms control uses IE engine in some compatibility mode.
I would use http://www.awesomium.com/
The webbrowser control is just an instance of IE - that page doesn't work for me in Internet Explorer so it's unlikely to work in the embedded browser. Search for webkit net - might give you better results. I'm tempted to ask why you want to do this but I'm sure you've got a good reason.
Got it working! Basically the set up works fine. Just had to open main.css file and comment out all the footer properties to get rid of the footer and found another script ref which was pointed to js/script.js had to change it to script.js
Now how can i increase the framerate so that it doesnt stutter? Theres a lag at the moment

ASP.NET MVC 4 bundles, paths, and the leading slash

First I define a bundle:
var bootstrapBundle = new Bundle("~/bundles/css/styles").Include(
"~/assets/css/bootstrap.css");
And then, in my layout file:
#Styles.Render("~/bundles/css/styles")
which renders to:
<link href="/assets/css/bootstrap.css" rel="stylesheet" />
My problem is that I'm sharing my assets between 2 projects and I need the following:
<link href="assets/css/bootstrap.css" rel="stylesheet" />
(note the absense of leading spash in the stylesheet path)
Any way I can achieve that while still referencing the bundle? Thank you in advance.
I dont think it is a good idea to share the assests in development, but in production however there are buildin features to use so that you can share the known assets like bootstrap.css.
example:
bundles.UseCdn = true;
bundles.Add(new ScriptBundle("~/bundles/jquery", "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js").Include(
"~/Scripts/jquery-{version}.js"));
to test it make sure debug="false" in web.config.
source
http://www.dotnetjalps.com/2014/07/cdn-in-aspnet-mvc-bundling.html

How to turn on IE9 Compatibility View programmatically in Javascript

I need to turn on IE compatibility programmatically.
I know this works in C# :
Page.Header.Controls.AddAt(0, new HtmlMeta { HttpEquiv = "X-UA-Compatible", Content = "IE=EmulateIE7" });
My problem is all my windows are displayed in a JS function: for instance:
function openRadWin(idAgir) {
radopen("DemandesEnAttente_Estimate.aspx?id=" + idAgir, "RadWindow1");
}
So my question is : is there any ways to do the same thing in JS?
Thanks in advance
AFAIK, this is not possible. You can detect the compatibility mode from JS but setting it is not possible to my knowledge.
As for as your problem goes, typically you can use few solutions:
If you are using Master pages in your site, add the meta header (<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">) in the master page.
Similar to #1, if you are using a common base page class (a good and recommended practice) then you can infuse the meta header from the common base page to all your pages.
Lastly, you can use IIS configuration to add the http header to all your response.
For example, for IIS7/IIS7.5, you can use web.config
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="X-UA-Compatible" />
<add name="X-UA-Compatible" value="IE=EmulateIE7" />
</customHeaders>
</httpProtocol>
</system.webServer>
I would suggest #1 or #2 - in case you don't have master page or base page class then perhaps its a good time to introduce the both.
What your C# example does, is add the following meta tag to the html page:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
If you do this manually on the page that runs the JS code, it should work.
You could try adding this HTML tag to the top of any page that requires compatibility:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
That way you shouldn't need a JavaScript solution. Of course, if you have the ability to change the HTML, then the best solution would be to fix the page so it doesn't require backwards compatibility.
I believe that the only way to influence headers from the client side is when you request a page using an XmlHttpRequest. It doesn't make sense to me to talk about modifying headers after the page has loaded, which is in effect what you are asking.

Style Sheet is not working in one web page

Style sheet in master page is not working for one web page of asp.net application but it works for another web page.
If you are referencing a css file from a master page you should ensure it has an absolute path, that way it will work everywhere. For example:
<head runat="server">
<link type="text/css" rel="stylesheet" href="~/_styles/mystylesheet.css" />
</head>
The important thing to note here is that the head tag has the runar="server" attribute and that i am specifying the full virtual path using a tilde ("~").
Are none of its style elements being included? Is it being over ridden( they are Cascading Style Sheets)? Does it have the correct CSS include statement?
Are your pages in different levels of folders ?
For example,
..\main.css
..\folder1\MasterPage.master
..\folder1\css_working.aspx
..\folder1\folder2\css_not_working.aspx
in this scenario you should define your css in masterpage as :
<link rel="stylesheet" type="text/css" href="../main.css" />
And take your pages to same level, like that :
..\main.css
..\folder1\MasterPage.master
..\folder1\css_working.aspx
..\folder2\css_not_working.aspx
If you are using update panels there are some cases where the styling may be lost for AJAX toolkit controls. To fix this you need to put hte full name of hte class items into the stylesheet instead of letting hte toolkit handle this.
Also be sure to use a relative url where possible so that if a file moves it won't loose it's mapping.
Use Firebug or Debug Bar, these tools will show you all the styles being employed on each element, so you can see what stylesheets it is using and which ones it is not.
Also, when you build check for any warnings about stylesheets that it can't reference etc.
it could be a permission issue on the folder... if you have deny users="?" in your web config.. make sure you have an allow users on the folder where you have your style sheets

Categories