How to use a variable set at build time in asp files - asp.net

My application has to use a CDN in production. We don't want to use the same CDN in development and production. How and where can I define a property "CDN_URL" for each build profile so that it is substituted at buid time or retrieved dynamically at runtime.
I'd like to write something like:
<link rel="stylesheet" type="text/css" href="${CDN_URL}/styles/base.css" />

You will want to store the CDN URL in the app settings in the web.config.
For example:
<appSettings>
<add key="Live_CDNURL" value="http://live.cdn.com"/>
<add key="Development_CDNURL" value="http://dev.cdn.com"/>
</appSettings>
Then in the application you can use:
WebConfigurationManager.AppSettings["Live_CDNURL"];
Then simply add the stylesheet using asp following something like Adding StyleSheets Programmatically in Asp.Net
Or do:
<link rel="stylesheet" type="text/css" href="<%=ConfigurationManager.AppSettings("Live_CDNURL")%>/styles/base.css" />

Related

How to alter CSS with web.config file

I have been asked to create a visual contrast for an HTML form to distinguish it from a production environment and a development environment, using a web.config file.
My exposure to ASP.NET is very limited. Although I've been researching web.config files since yesterday, due to my lack of experience in this technology, I still don't think I can provide a solid solution with much confidence.
If this job could be handled directly with HTML, CSS, PHP or JS I could do it myself, but using web.config is a requirement.
The goal is to create an unmistakable visual change (let's say, lightgreen background) when a user is on the development form.
There is already a CSS stylesheet in play, so I'm thinking the change would have to go inline in the HTML to override the external styles.
So here's the question, if it makes any sense:
If this is in the production web.config: <add key="CurEnv" value="Production"/> and this is in dev: <add key="CurEnv" value="Development"/>, how can I set the form's background color based on the value in the config file?
This is how I do it:
For DEV, for example, I have the following key in my appSettings:
<appSettings>
<add key="skin" value="skin-gray"/>
...
</appSettings>
And then in my Master page (if using Web Forms) or on my _Layout.cshtml (if using MVC), I have the following HTML:
<body class="#System.Configuration.ConfigurationManager.AppSettings["skin"]">
The syntax for WebForms would be:
<body class="<%=System.Configuration.ConfigurationManager.AppSettings["skin"]%>">
For Production, I have a "skin-blue" setting in my appSettings section and that way, when I deploy the app, depending on the environment, the skin is automatically changed.
I actually do this automatically, as part of the Build process using Slow Cheetah
So that depending on which Configuration is chosen to build the application, the right value is used. This is my transform for Web.Production.Config:
<appSettings>
<add key="skin" value="skin-blue" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
I think you can read those link. It's may help you:
http://forums.asp.net/t/1349824.aspx?How+to+set+CSS+color+settings+from+web+config
Dynamically setting CSS values using ASP.NET

Integrating Twitter Bootstrap within a SpringMVC application

I'm starting out with building basic applications in SpringMVC. At the same time, I wanted to use some easy to setup UI frameworks like Twitter Bootstrap. But, No clue on how to set it up.
Question:
Where do I place the downloaded bootstrap folder?
What I have so far.
I would put these in src/main/resources NOT under WEB-INF. These don't need to be protected.
Also, make sure you tell Spring where they are in your dispatcher servlet config file as per the documentation.
<mvc:resources mapping="/resources/**" location="/resources/" />
If you're using Spring security as well you'll need to make sure that the resources are not protected.
<http pattern="/resources/**" security="none"/>
You don't need the .less files unless you plan to compile custom css. Maven projects, you typically place them in the resources folder. resources/assets/css and resources/assets/js
In the JSP:
<spring:url scope="page" var="bootstrapStylesheetUrl" value="/resources/assets/css/bootstrap.css"/>
<spring:url scope="page" var="bootstrapResponsiveStylesheetUrl" value="/resources/assets/css/bootstrap-responsive.css"/>
<spring:url scope="page" var="bootstrapJavascriptUrl" value="/resources/assets/js/bootstrap.js"/>
And then in the head tag
<script src="${pageScope.bootstrapJavascriptUrl}"></script>
<link rel="stylesheet" href="${pageScope.bootstrapStylesheetUrl}"/>
<link rel="stylesheet" href="${pageScope.bootstrapResponsiveStylesheetUrl}"/>
Also, don't forget to add the spring taglib to the top of your jsp
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
In your spring servlet context config(productmgmt-servlet.xml) add the line:
<mvc:resources mapping="/resources/**" location="classpath:/"/>

Resources not automatically loaded from Https - SecuritySwitch

I am upgrading from the old SecureWebPages that automates the switching between Http and Https content via web.config.
For some strange reason, having contents like:
<link type="text/css" href="assets/css/style.css" rel="stylesheet" />
no longer automatically loads from the appropriate https location. The console in Google Chrome shows me this:
The page at https://website.com/UserAccess.aspx ran insecure content from http://website.com/assets/css/style.css.
This behavior didn't exist when I was using the older SecureWebPages. In the past the above CSS statement works fine without any errors.
My web.config:
<securitySwitch mode="RemoteOnly">
<paths>
<add path="~/Register.aspx"/>
<add path="~/SSL.Master"/>
</paths>
Is there anything wrong with my configurations? Please advise. Thanks!
You need to tell SecuritySwitch to ignore your CSS folder, or even your entire Assets folder if it contains images and the like as well. Here is a path you can add to the securitySwitch section for the assets folder.
<securitySwitch mode="RemoteOnly">
<paths>
<add path="~/Register.aspx"/>
<add path="~/assets/" security="Ignore"/>
</paths>
</securitySwitch>
This will tell SecuritySwitch to ignore the assets folder, and everything under it. Also, your path for the master file does nothing, since .master files are never served to a browser.
I hope this helps!

Spring 3 doesn't return css

I just started with Spring 3 MVC today. Running into a dilemma...
web.xml maps everything ("/") to Spring. But as a result, when I put something like:
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/navigation.css" />
It is not returned by the container...
Perhaps someone could suggest how to handle this?
Thanks.
Use mvc:resources, as explained in the documentation. This allows service static resources from the web app, but also from the classpath.
How are you trying to serve it? If you are trying to serve it from the webapp itself (ie WEB-INF/static/css) You would need to include a servlet to do that for you. In the spring context you can include something like
<mvc:resources mapping="/resources/**" location="/resources/" />
You can see more here
How to handle static content in Spring MVC?
As suggested by others, use mvc:resource to serve your static resources.
<mvc:resources mapping="/resources/**" location="/resources/" />
It is also recommended to avoid using scriptlets in your JSP code if possible. You should instead use JSTL to build the correct path to your CSS file.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
...
<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/navigation.css" />"/>

MVC: I have deployed my application, but CSS only works when I log in

I have deployed my MVC app but when I browse from IIS I find that the forms authentication seems to block the CSS.
This does not happen on my dev server.
Why would this happen?
My master page look like this;
<head id="Head1" runat="server">
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.js" type="text/javascript" language="javascript"></script>
<script src="http://nje.github.com/jquery-tmpl/jquery.tmpl.js" type="text/javascript"></script>
<script src="<%=Url.Content("~/Scripts/jquery.form.js")%>" type="text/javascript"></script>
<!-- note that the order scripts are included in is important -->
<script src="<%=Url.Content("~/Scripts/jquery.Validate.js")%>" type="text/javascript"></script>
<script src="<%=Url.Content("~/Scripts/MicrosoftMvcJQueryValidation.js")%>" type="text/javascript"></script>
<script src="<%=Url.Content("~/Scripts/ConfusedValidation.js")%>" type="text/javascript"></script>
<script src="<%=Url.Content("~/Scripts/Awesome.js")%>" type="text/javascript"></script>
<script src="<%=Url.Content("~/Scripts/MicrosoftAjax.debug.js") %>" type="text/javascript"></script>
<script src="<%=Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js") %>" type="text/javascript"></script>
<script src="<%=Url.Content("~/Scripts/jquery.jcacher-1.0.0.min.js")%>" type="text/javascript"></script>
<link href="<%=Url.Content("~/Content/jquery.ui.autocomplete.css")%>" rel="stylesheet" type="text/css" />
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/overcast/jquery-ui.css"
type="text/css" rel="Stylesheet" class="ui-theme" />
<link href="<%=Url.Content("~/Scripts/Awesome.css")%>" rel="stylesheet" type="text/css" />
* EDIT *
In Firebug I get the message;
Failed to load source for: http://192.168.100.999/Content/Site.css
In fact I get this for all the local files.
I have also faced the same kind of problem. My local server IIS configuration was not working but Development server worked properly. That time had to add "User Permission" to my Hosed folder by right click on hoted site in IIS.
Just follow the step.
and then add uesr called IUSER by Add User panel.
Are you on shared host? Does yours script *.js load? If it does it means youre relative path is not good, you probably could refer to your css file the same way you do to your .js using Url.Content.
<link href="<%=Url.Content("~/Content/Site.css)%>" rel="stylesheet" type="text/css" />
I suggest you to take a look at SquishIt : http://www.codethinked.com/squishit-the-friendly-aspnet-javascript-and-css-squisher it will combine and minimize your CSS and javascript for browser optimization.
Hope it helps!
The answer was to do with assigning permissions to the user IIS_IUSR to the folders in which the application is deployed. A colleague discovered this for me. I have to admit I am not sure why this is necessary but it fixed the problem.
You should explicitly enable anonymous access to the resources you want publicly available. The way to do this is to add a web.config in the folder containing the public content with the following settings:
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</configuration>
It probably works on the dev server becuase windows authentication means you are not anonymous.
You need to simply change your application pool identity property. It is under advanced settings --> Identity. Change identity value to "ApplicationPoolIdentity" from the drop down. It should fix the issue.
The reason here is that you are not logged in while accessing contents of the website e.g. style sheet or script. So site is not loading those contents. If you are authenticated, it will load all the contents. The identity value is usually Network service or local service.
this is a permission issue: Please add BOTH: IIS_USR and IUSR permissions to the folder structure

Resources