Umbraco 4.11 MVC Mode - Children Url? - asp.net-mvc

What is the correct way to obtain the URLs of children of the current node while using Umbraco 4.11 in MVC mode? This is what I tried and it keeps returning a blank URL:
#if (CurrentPage.Children().Count() > 0) {
<ul>
#foreach (var child in CurrentPage.Children()) {
<li>#child.Name</li>
}
</ul>
}
When that didn't work I resorted to the following:
#Umbraco.NiceUrl(child.Id)
This returns a URL, but it has .aspx extension on it. So my next hack is...
#Umbraco.NiceUrl(child.Id).Replace(".aspx", "")
That's not completely terrible, but I'm wondering if I'm missing something?

You need the NiceUrl() method to generate a valid Url.
To get rid of the .aspx extension, you need to set the setting "umbracoUseDirectoryUrls" to true in your web.config file:
<add key="umbracoUseDirectoryUrls" value="true" />

I can't add this as a comment, but there is a way to remove trailing slashes, in umbracoSettings.config:
<addTrailingSlash>true</addTrailingSlash>

Other way to get Only Node Name (Without .aspx) is
#Model.NodeById(#child.Id).urlName
But it will just give Node name without "/"

Related

Access MVC application settings from layout page(s)

I am trying to further help my organization's users know that the environment they are on is our test environment. I have putting a setting in the application settings section of both the web.config and then transform(replace) that setting in the web.release.config. All I want to do is switch our logo from it's normal color to a red variant. Below is what I've tried and it always goes to the else statements option.
#if (System.Configuration.ConfigurationManager.AppSettings["Environment"] == "PROD")
{
<img src="~/Content/Img/default/sru_logo.png" class="sruLogo" alt="" />
}
else if (System.Configuration.ConfigurationManager.AppSettings["Environment"] == "PPRD")
{
<img src="~/Content/Img/default/sru_logo_test.png" class="sruLogo" alt="" />
}
else
{
<p>Not Working</p>
}
I've tried using Context.Application["Environment"], System.Web.Configuration.WebConfigurationManager.AppSettings["Environment"]
None of the above has worked and as I stated all I am trying to do is use a different image on my _Layout.cshtml file based on an application setting.
try using round brackets instead of suqare
System.Configuration.ConfigurationManager.AppSettings("Environment");

Image file path in Razor .cshtml view file

In the website I'm creating, I'm trying to store a URL to an uploaded image into my model and calling to it in the view. So when I type out the file path in the source for the HTML, it works fine.
<dd><img src="~/uploads/image.jpg" alt="ASP.NET" style="width:350px;height:260px" />
</dd>
But if I try to call from the model, the URL gets messed up.
#string imagePath = "~/uploads/" + Model.Picture;
<dd><img src=#imagePath alt="ASP.NET" style="width:350px;height:260px" />
</dd>
That code links to "http://localhost:60847/Controller/Details/~/uploads/image.jpg" . Could someone explain to me why it's working differently? Thank you.
You are missing quotes around the src property value. Also make sure you use the Url.Content helper method to get the correct path. You may pass "~" to this method to get the correct path to your app root.
#{ string imagePath = Url.Content("~/uploads/" + Model.Picture); };
<img src="#imagePath" alt="ASP.NET" style="width:350px;height:260px" />
Or even a single liner without the variable
<img src="#Url.Content("~/uploads/" + Model.Picture)" />

Getting issue to generate Absolute Url's in MVC

Hi I have created a mvc website which is working fine with localhost if i am coding something like this:
<base href="http://localhost:5400/" />
<li><a class="home" href="/Home/Index/">Home</a></li>
<li class="wish"><a class="wishlist" href="/Products/Index/" id="wishlist-total">Products</a></li>
<li><a class="account" href="/Home/Contact/">Contact Us</a></li>
But now to run it on live if i am trying to changing this:
<base href="http://localhost:5400/" />
with this:
<base href="HttpContext.Current.Request.Url" />
then its actually taking full root url everytime.So whenever i am clicking on any menu and moves to next menu it regain previous menu path also.
For this issue i tried below code which is also not working.
<li>Home</li>
<li>Products</li>
<li> Contact</li>
According me this code will work,But if i am trying to pass "null" as third parameter then i gives error:
ERROR: 'null' is not declared. 'Null' constant is no longer supported; use 'System.DBNull' instead.
Can someone please suggest what i should need to change?
Thanks
Quick fix...
#Url.Action("Index", "Home")
There's no need to enter null as a parameter... if you want to indicate that a parameter can be null, then you need to define that in the route its self, and if the parameter is missing, then it will know that the value is null implicitly...
// http://yoursite/Example/{id}
[Route("~/Example/{id}"] // Can't be null
public ActionResult Example(string id){ return View(); }
// http://yoursite/ExampleTwo/
[Route("~/ExampleTwo/{id?} // Can be null
public ActionResult ExampleTwo(string id) { return View(); }
TLDR if you want more errors...
Your issue is actually indicating a much larger issue... it looks as if you're accepting a query from a URI directly into a SQL query... if you are doing that, you're opening yourself up to injection attacks and as much as a part of me feels that anyone who does that pretty much has it coming to them... I can't stand by and just say nothing... sanitize any data that you receive from the user, no matter where it's coming from, example
Remove all those magic strings... you shouldn't have any strings such as the one you just displayed...
// Bad...
<a class="home" href="/Home/Index/">
// Better...
<a class="home" href="#Url.Action("Index", "Home")">Home</a>
// Good
#Html.ActionLink("Home", "Index", "Home")

Mapping URL in main menu to every page

I am using in my grails 2.3.4 project with spring security and spring securit ui.
I am having scaffolde my domain contact to the view. I have also a .gsp page which is not scarfolded.
My links from my main menue looks like that:
<li>Pricing</li>
<li>Contact</li>
Thats my URLMappings
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.${format})?"{
constraints {
// apply constraints here
}
}
"/private/$controller/$action?/$id?(.${format})?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"/pricing"(view:"/pricing")
"/private/dashboard"(view:"/private/dashboard")
"/contact/create"(view:"/contact/create")
"500"(view:'/error')
}
}
My problem is when I am using this two links from my mainpage / then everythings works fine. However using them from 5432:localhost/TestApp/pricing I am getting the link 5432:localhost/TestApp/pricing/contact/create
which goes is not available. If I am using <li>Contact</li>, I am going to 5432:localhost/contact/create, which is also not available. How to go to contact/create from every page?
I appreciate your reply!
The simplest and safest approach would be
<li><g:link uri="/contact/create">Contact</g:link></li>
Other attributes on the g:link tag will pass through to the generated a tag, with the exception of id - you need to use elementId instead, as id is treated as a parameter to the link generation (controller/action/id)
<li><g:link uri="/contact/create" class="nav" elementId="contactlink">Contact</g:link></li>
would become
<li>Contact</li>
(where /TestApp is the application context path - if you deploy the app at a different context path then then link will change to match).

Grails: checkbox not being set back to false

I am developing a Grails (1.0.4) app where I want to edit a collection of collections on a single page in a grid view. I got it to work quite well depending only on the indexed parameter handling of Spring MVC, except for one thing:
boolean (or, for that matter, Boolean) values in the grid can be set via checkbox, but not unset, i.e. when I check the checkbox and update, the value is set to true, but afterwards when I edit again, uncheck the checkbox and update, it remains true.
This is the GSP code of the checkbox:
<g:checkBox name="tage[${indexTag}].zuweisungen[${indexMitarb}].fixiert" value="${z.fixiert}" />
And this is the HTML that is generated:
<input type="hidden" name="tage[0].zuweisungen[0]._fixiert" />
<input type="checkbox" name="tage[0].zuweisungen[0].fixiert" checked="checked" id="tage[0].zuweisungen[0].fixiert" />
I've found a Grails bug that describes exactly this effect, but it's marked as fixed in 1.0.2, and the problem mechanism described there (underscore in hidden field name is put in the wrong place) is not present in my case.
Any ideas what could be the reason?
This is the solution a guy named Julius Huang proposed on the grails-user mailing list. It's reusable but relies on JavaScript to populate a hidden field with the "false" response for an unchecked checkbox that HTML unfortunately does not send.
I hack GSP to send "false" when
uncheck the box (true -> false) with
custom TagLib.
By default checkBox send nothing when
uncheck, so I use the checkBox as
event handler but send hidden field
instead.
"params" in Controller can handle
"false" -> "true" without any
modification. eg. Everything remain
same in Controller.
The Custom Tag Usage in GSP (sample usedfunc_F is "true"),
<jh:checkBox name="surveyList[${i}].usedfunc_F" value="${survey.usedfunc_F}"></jh:checkBox>
Here is what the Tag generate,
<input type="hidden" name="surveyList[#{i}].usedfunc_F" id="surveyList[#{i}].usedfunc_F" value="false" />
<input type="checkbox" onclick="jhtoggle('surveyList[#{i}].usedfunc_F')" checked="checked" />
The Javascript
<script type="text/javascript">
function jhtoggle(obj) {
var jht = document.getElementById(obj);
jht.value = (jht.value !='true' ? 'true' : 'false');
}
</script>
This is my own solution, basically a workaround that manually does what the grails data binding should be doing (but doesn't):
Map<String,String> checkboxes = params.findAll{def i = it.key.endsWith("._fixiert")} // all checkboxes
checkboxes.each{
String key = it.key.substring(0, it.key.indexOf("._fixiert"))
int tagIdx = Integer.parseInt(key.substring(key.indexOf('[')+1, key.indexOf(']')))
int zuwIdx = Integer.parseInt(key.substring(key.lastIndexOf('[')+1, key.lastIndexOf(']')))
if(params.get(key+".fixiert"))
{
dienstplanInstance.tage[tagIdx].zuweisungen[zuwIdx].fixiert = true
}
else
{
dienstplanInstance.tage[tagIdx].zuweisungen[zuwIdx].fixiert = false
}
}
Works, requires no change in grails itself, but isn't reusable (probably could be made so with some extra work).
I think that the simplest workaround would be to attach a debugger and see why Grails is failing to populate the value. Considering Grails is open source you'll be able to access the source code and once you figure out the solution for it you can patch your version.
I have also found this other bug GRAILS-2861 which mentions the issue related to binding to booleans (see Marc's comment in the thread). I guess that is exactly the problem you are describing.
I would create a small sample app that demonstrates the problem and attach it to the Grails bug (or create a new one). Someone here may be able to debug your sample app or you'll have shown the bug isn't really fixed.
Try this out, set the logs to DEBUG, frist try the first 3 if they don't show the problem up, flip them all to DEBUG:
codehaus.groovy.grails.web.servlet="error" // controllers
codehaus.groovy.grails.web.pages="error" // GSP
codehaus.groovy.grails.web.sitemesh="error" // layouts
codehaus.groovy.grails."web.mapping.filter"="error" // URL mapping
codehaus.groovy.grails."web.mapping"="error" // URL mapping
codehaus.groovy.grails.commons="info" // core / classloading
codehaus.groovy.grails.plugins="error" // plugins
codehaus.groovy.grails.orm.hibernate="error" // hibernate integration
This should allow you to see exactly when and how the parameters setting is failing and probably figure out a work around.

Resources